183 lines
5.5 KiB
PHP
183 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Element\UseCases;
|
|
|
|
use App\Element\CreateElementDto;
|
|
use App\Element\Element;
|
|
use App\Element\UseCases\GetElement\GetElement;
|
|
use App\Element\UseCases\GetElement\GetElementRequest;
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\NotFoundException;
|
|
use App\Set\Set as DomainSet;
|
|
use Tests\Fakes\FakeElementRepository;
|
|
use Tests\TestCase;
|
|
|
|
class GetElementTest extends TestCase
|
|
{
|
|
private FakeElementRepository $elementRepo;
|
|
|
|
private GetElement $getElement;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->elementRepo = new FakeElementRepository();
|
|
$this->getElement = new GetElement($this->elementRepo);
|
|
}
|
|
|
|
public function testReturnsElementWhenFound(): void
|
|
{
|
|
$set = $this->createSet(1, 'Baderech');
|
|
$element = $this->createElement(
|
|
$set,
|
|
'Baderech HaAvodah',
|
|
'A structured path for growth',
|
|
'<p>A structured path for growth</p>',
|
|
'/assets/pdfs/baderech.pdf',
|
|
null,
|
|
);
|
|
|
|
$result = $this->getElement->execute(new GetElementRequest(
|
|
id: $element->getId(),
|
|
));
|
|
$foundElement = $result->getElement();
|
|
|
|
$this->assertInstanceOf(Element::class, $foundElement);
|
|
$this->assertSame($element->getId(), $foundElement->getId());
|
|
$this->assertSame('Baderech HaAvodah', $foundElement->getTitle());
|
|
$this->assertSame(
|
|
'A structured path for growth',
|
|
$foundElement->getDescription(),
|
|
);
|
|
$this->assertSame(
|
|
'<p>A structured path for growth</p>',
|
|
$foundElement->getRichText(),
|
|
);
|
|
$this->assertSame(
|
|
'/assets/pdfs/baderech.pdf',
|
|
$foundElement->getPdfPath(),
|
|
);
|
|
}
|
|
|
|
public function testReturnsDirectChildElements(): void
|
|
{
|
|
$set = $this->createSet(1, 'Baderech');
|
|
$parentElement = $this->createElement(
|
|
$set,
|
|
'Baderech HaAvodah',
|
|
'A structured path for growth',
|
|
'<p>A structured path for growth</p>',
|
|
'/assets/pdfs/baderech.pdf',
|
|
null,
|
|
);
|
|
$firstChildElement = $this->createElement(
|
|
$set,
|
|
'Avodah Foundations',
|
|
'Foundations for steady avodah',
|
|
'<p>Foundations rich text</p>',
|
|
'/assets/pdfs/foundations.pdf',
|
|
$parentElement,
|
|
);
|
|
$secondChildElement = $this->createElement(
|
|
$set,
|
|
'Daily Practice',
|
|
'Daily practices for growth',
|
|
'<p>Daily practice rich text</p>',
|
|
null,
|
|
$parentElement,
|
|
);
|
|
$this->createElement(
|
|
$set,
|
|
'Nested Practice',
|
|
'Nested description',
|
|
'<p>Nested rich text</p>',
|
|
null,
|
|
$firstChildElement,
|
|
);
|
|
$otherSet = $this->createSet(2, 'Daily Learning');
|
|
$otherParentElement = $this->createElement(
|
|
$otherSet,
|
|
'Other Parent',
|
|
'Other parent description',
|
|
'<p>Other parent rich text</p>',
|
|
null,
|
|
null,
|
|
);
|
|
$this->createElement(
|
|
$otherSet,
|
|
'Other Child',
|
|
'Other child description',
|
|
'<p>Other child rich text</p>',
|
|
null,
|
|
$otherParentElement,
|
|
);
|
|
|
|
$result = $this->getElement->execute(new GetElementRequest(
|
|
id: $parentElement->getId(),
|
|
));
|
|
$childElements = $result->getChildElements();
|
|
|
|
$this->assertCount(2, $childElements);
|
|
$this->assertSame(
|
|
$firstChildElement->getId(),
|
|
$childElements[0]->getId(),
|
|
);
|
|
$this->assertSame('Avodah Foundations', $childElements[0]->getTitle());
|
|
$this->assertSame(
|
|
'Foundations for steady avodah',
|
|
$childElements[0]->getDescription(),
|
|
);
|
|
$this->assertSame(
|
|
$secondChildElement->getId(),
|
|
$childElements[1]->getId(),
|
|
);
|
|
$this->assertSame('Daily Practice', $childElements[1]->getTitle());
|
|
$this->assertSame(
|
|
'Daily practices for growth',
|
|
$childElements[1]->getDescription(),
|
|
);
|
|
}
|
|
|
|
public function testThrowsWhenIdMissing(): void
|
|
{
|
|
$this->expectException(BadRequestException::class);
|
|
$this->expectExceptionMessage('id is required');
|
|
|
|
$this->getElement->execute(new GetElementRequest(id: null));
|
|
}
|
|
|
|
public function testThrowsWhenElementDoesNotExist(): void
|
|
{
|
|
$this->expectException(NotFoundException::class);
|
|
$this->expectExceptionMessage('Element not found');
|
|
|
|
$this->getElement->execute(new GetElementRequest(id: 999));
|
|
}
|
|
|
|
private function createSet(int $id, string $name): DomainSet
|
|
{
|
|
return new DomainSet(
|
|
id: $id,
|
|
name: $name,
|
|
description: "$name description",
|
|
iconImageUrl: '/assets/baderech-icon.png',
|
|
);
|
|
}
|
|
|
|
private function createElement(
|
|
DomainSet $set,
|
|
string $title,
|
|
string $description,
|
|
string $richText,
|
|
?string $pdfPath,
|
|
?Element $parentElement,
|
|
): Element {
|
|
return $this->elementRepo->create(new CreateElementDto(
|
|
set: $set,
|
|
title: $title,
|
|
description: $description,
|
|
richText: $richText,
|
|
pdfPath: $pdfPath,
|
|
parentElement: $parentElement,
|
|
));
|
|
}
|
|
}
|