test child creation

This commit is contained in:
Yisroel Baum 2026-06-21 21:44:06 +03:00
parent 416866a22c
commit 7c84eefe78
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
3 changed files with 213 additions and 0 deletions

View file

@ -0,0 +1,102 @@
<?php
namespace Tests\Unit\Element\UseCases;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\UseCases\CreateChildElement\CreateChildElement;
use App\Element\UseCases\CreateChildElement\CreateChildElementRequest;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
use App\Set\Set as DomainSet;
use Tests\Fakes\FakeElementRepository;
use Tests\TestCase;
class CreateChildElementTest extends TestCase
{
private FakeElementRepository $elementRepository;
private CreateChildElement $createChildElement;
protected function setUp(): void
{
$this->elementRepository = new FakeElementRepository();
$this->createChildElement = new CreateChildElement(
$this->elementRepository,
);
}
public function testCreatesChildElementFromParentSet(): void
{
$parentElement = $this->createParentElement();
$childElement = $this->createChildElement->execute(
new CreateChildElementRequest(
parentElementId: $parentElement->getId(),
title: 'Admin child',
)
);
$this->assertSame('Admin child', $childElement->getTitle());
$this->assertSame('', $childElement->getDescription());
$this->assertNull($childElement->getIconImageUrl());
$this->assertSame('', $childElement->getRichText());
$this->assertNull($childElement->getShortPdfPath());
$this->assertNull($childElement->getLongPdfPath());
$this->assertNull($childElement->getYoutubeUrl());
$this->assertSame(
$parentElement->getSet()->getId(),
$childElement->getSet()->getId(),
);
$this->assertSame(
$parentElement->getId(),
$childElement->getParentElement()->getId(),
);
}
public function testThrowsWhenParentElementIsMissing(): void
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('Parent element not found');
$this->createChildElement->execute(new CreateChildElementRequest(
parentElementId: 999,
title: 'Missing parent child',
));
}
public function testThrowsWhenTitleIsMissing(): void
{
$parentElement = $this->createParentElement();
$this->expectException(BadRequestException::class);
$this->expectExceptionMessage('title is required');
$this->createChildElement->execute(new CreateChildElementRequest(
parentElementId: $parentElement->getId(),
title: '',
));
}
private function createParentElement(): Element
{
$set = new DomainSet(
id: 1,
name: 'Baderech',
description: 'Baderech description',
iconImageUrl: '/assets/baderech-icon.png',
);
return $this->elementRepository->create(new CreateElementDto(
set: $set,
title: 'Parent',
description: 'Parent description',
iconImageUrl: null,
richText: '',
shortPdfPath: null,
longPdfPath: null,
youtubeUrl: null,
parentElement: null,
));
}
}