add tests for text user relationship

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.
This commit is contained in:
Yisroel Baum 2026-05-02 21:27:49 +03:00
parent 6668240126
commit 40fdf25da2
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 134 additions and 2 deletions

View file

@ -7,22 +7,36 @@ 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,
@ -33,6 +47,7 @@ class CreateTextTest extends TestCase
{
$text = $this->useCase->execute(new CreateTextRequest(
name: 'test',
user: $this->user,
));
$this->assertInstanceOf(TextRepository::class, $this->textRepo);
$this->assertInstanceOf(Text::class, $text);
@ -43,6 +58,7 @@ class CreateTextTest extends TestCase
{
$text = $this->useCase->execute(new CreateTextRequest(
name: 'my text',
user: $this->user,
));
$nodes = $this->nodeRepo->findByTextId($text->getId());
@ -53,6 +69,17 @@ class CreateTextTest extends TestCase
$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);
@ -60,6 +87,18 @@ class CreateTextTest extends TestCase
$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,
));
}
}