Rabbi_Gerzi/backend/tests/Unit/Element/UseCases/GetElementTest.php

71 lines
2 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
{
$element = $this->createElement('Baderech HaAvodah');
$foundElement = $this->getElement->execute(new GetElementRequest(
id: $element->getId(),
));
$this->assertInstanceOf(Element::class, $foundElement);
$this->assertSame($element->getId(), $foundElement->getId());
$this->assertSame('Baderech HaAvodah', $foundElement->getTitle());
}
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 createElement(string $title): Element
{
$set = new DomainSet(
id: 1,
name: 'Baderech',
description: 'Baderech description',
iconImageUrl: '/assets/baderech-icon.png',
);
return $this->elementRepo->create(new CreateElementDto(
set: $set,
title: $title,
parentElement: null,
));
}
}