test element persistence

This commit is contained in:
Yisroel Baum 2026-08-08 22:18:58 +03:00
parent dd5f35f507
commit aaab0e5379
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,86 @@
<?php
namespace Tests\Feature\Element;
use App\Element\CreateElementDto;
use App\Element\ElementRepository;
use App\Set\CreateSetDto;
use App\Set\Set;
use App\Set\SetRepository;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
use App\User\UserRepository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class EloquentElementRepositoryTest extends TestCase
{
use RefreshDatabase;
public function testItPersistsNestedElementsInSiblingOrder(): void
{
$set = $this->createSet('Bible');
$repository = app(ElementRepository::class);
$genesisBook = $repository->create(new CreateElementDto(
set: $set,
name: 'Genesis',
kind: 'book',
parentElement: null,
));
$exodusBook = $repository->create(new CreateElementDto(
set: $set,
name: 'Exodus',
kind: 'book',
parentElement: null,
));
$genesisPortion = $repository->create(new CreateElementDto(
set: $set,
name: 'Genesis',
kind: 'portion',
parentElement: $genesisBook,
));
$noahPortion = $repository->create(new CreateElementDto(
set: $set,
name: 'Noah',
kind: 'portion',
parentElement: $genesisBook,
));
$this->assertSame(1, $genesisBook->getPosition());
$this->assertSame(2, $exodusBook->getPosition());
$this->assertSame(1, $genesisPortion->getPosition());
$this->assertSame(2, $noahPortion->getPosition());
$this->assertDatabaseHas('elements', [
'id' => $genesisPortion->getId(),
'set_id' => $set->getId(),
'name' => 'Genesis',
'kind' => 'portion',
'parent_element_id' => $genesisBook->getId(),
'position' => 1,
]);
$foundElement = $repository->find($noahPortion->getId());
$this->assertNotNull($foundElement);
$this->assertSame('Noah', $foundElement->getName());
$this->assertSame('portion', $foundElement->getKind());
$this->assertSame($set->getId(), $foundElement->getSet()->getId());
$this->assertSame(
$genesisBook->getId(),
$foundElement->getParentElement()?->getId(),
);
}
private function createSet(string $name): Set
{
$creator = app(UserRepository::class)->create(new CreateUserDto(
email: new EmailAddress(strtolower($name) . '@example.com'),
passwordHash: 'hashed-password',
));
return app(SetRepository::class)->create(new CreateSetDto(
name: $name,
creator: $creator,
));
}
}