Rabbi_Gerzi/backend/tests/Unit/Element/UseCases/CreateChildElementTest.php
2026-06-21 21:44:06 +03:00

102 lines
3.2 KiB
PHP

<?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,
));
}
}