cover that the created Text carries the supplied User, that the controller persists the user from the session attribute, and that any userId in the request body is ignored.
104 lines
2.9 KiB
PHP
104 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Text\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Text\Text;
|
|
use App\Text\TextRepository;
|
|
use App\Text\UseCases\CreateText;
|
|
use App\Text\UseCases\CreateTextRequest;
|
|
use App\User\UseCases\CreateUserDto;
|
|
use App\User\User;
|
|
use App\ValueObjects\EmailAddress;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Tests\Fakes\FakeNodeRepository;
|
|
use Tests\Fakes\FakeTextRepository;
|
|
use Tests\Fakes\FakeUserRepository;
|
|
|
|
class CreateTextTest extends TestCase
|
|
{
|
|
private FakeTextRepository $textRepo;
|
|
|
|
private FakeUserRepository $userRepo;
|
|
|
|
private FakeNodeRepository $nodeRepo;
|
|
|
|
private CreateText $useCase;
|
|
|
|
private User $user;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->textRepo = new FakeTextRepository();
|
|
$this->nodeRepo = new FakeNodeRepository();
|
|
$this->userRepo = new FakeUserRepository();
|
|
$this->user = $this->userRepo->create(new CreateUserDto(
|
|
email: new EmailAddress('a@b.com'),
|
|
passwordHash: '',
|
|
isAdmin: false,
|
|
));
|
|
$this->useCase = new CreateText(
|
|
$this->textRepo,
|
|
$this->nodeRepo,
|
|
);
|
|
}
|
|
|
|
public function test_create_text(): void
|
|
{
|
|
$text = $this->useCase->execute(new CreateTextRequest(
|
|
name: 'test',
|
|
user: $this->user,
|
|
));
|
|
$this->assertInstanceOf(TextRepository::class, $this->textRepo);
|
|
$this->assertInstanceOf(Text::class, $text);
|
|
$this->assertEquals('test', $text->getName());
|
|
}
|
|
|
|
public function test_creates_root_node_on_text_creation(): void
|
|
{
|
|
$text = $this->useCase->execute(new CreateTextRequest(
|
|
name: 'my text',
|
|
user: $this->user,
|
|
));
|
|
|
|
$nodes = $this->nodeRepo->findByTextId($text->getId());
|
|
$this->assertCount(1, $nodes);
|
|
|
|
$rootNode = array_values($nodes)[0];
|
|
$this->assertEquals('my text', $rootNode->getTitle());
|
|
$this->assertNull($rootNode->getParentNode());
|
|
}
|
|
|
|
public function test_text_belongs_to_user(): void
|
|
{
|
|
$text = $this->useCase->execute(new CreateTextRequest(
|
|
name: 'my text',
|
|
user: $this->user,
|
|
));
|
|
|
|
$this->assertSame($this->user, $text->getUser());
|
|
$this->assertEquals($this->user->getId(), $text->getUser()->getId());
|
|
}
|
|
|
|
public function test_throws_if_name_is_null(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('name is required');
|
|
|
|
$this->useCase->execute(new CreateTextRequest(
|
|
name: null,
|
|
user: $this->user,
|
|
));
|
|
}
|
|
|
|
public function test_throws_if_user_is_null(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('user is required');
|
|
|
|
$this->useCase->execute(new CreateTextRequest(
|
|
name: 'name',
|
|
user: null,
|
|
));
|
|
}
|
|
}
|