Rabbi_Gerzi/backend/tests/Unit/Controllers/ElementControllerTest.php

117 lines
3.4 KiB
PHP

<?php
namespace Tests\Unit\Controllers;
use App\Controllers\ElementController;
use App\Element\CreateElementDto;
use App\Element\Element;
use App\Element\UseCases\GetElement\GetElement;
use App\Set\Set as DomainSet;
use Tests\Fakes\FakeElementRepository;
use Tests\TestCase;
class ElementControllerTest extends TestCase
{
private ElementController $controller;
private FakeElementRepository $elementRepo;
protected function setUp(): void
{
$this->elementRepo = new FakeElementRepository();
$getElement = new GetElement($this->elementRepo);
$this->controller = new ElementController($getElement);
}
public function testShowReturnsElementPayload(): void
{
$set = $this->createSet(1, 'Baderech');
$element = $this->createElement(
$set,
'Baderech HaAvodah',
'A structured path for growth',
null,
);
$firstChildElement = $this->createElement(
$set,
'Avodah Foundations',
'Foundations for steady avodah',
$element,
);
$secondChildElement = $this->createElement(
$set,
'Daily Practice',
'Daily practices for growth',
$element,
);
$response = $this->controller->show($element->getId());
$this->assertEquals(200, $response->getStatusCode());
$body = json_decode($response->getContent(), true);
$this->assertSame($element->getId(), $body['element']['id']);
$this->assertSame('Baderech HaAvodah', $body['element']['title']);
$this->assertSame(
'A structured path for growth',
$body['element']['description'],
);
$this->assertSame([
[
'id' => $firstChildElement->getId(),
'title' => 'Avodah Foundations',
'description' => 'Foundations for steady avodah',
],
[
'id' => $secondChildElement->getId(),
'title' => 'Daily Practice',
'description' => 'Daily practices for growth',
],
], $body['childElements']);
}
public function testShowReturns400WhenIdMissing(): void
{
$response = $this->controller->show(null);
$this->assertEquals(400, $response->getStatusCode());
$this->assertSame(
['error' => 'id is required'],
json_decode($response->getContent(), true),
);
}
public function testShowReturns404WhenElementDoesNotExist(): void
{
$response = $this->controller->show(999);
$this->assertEquals(404, $response->getStatusCode());
$this->assertSame(
['error' => 'Element not found'],
json_decode($response->getContent(), true),
);
}
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,
?Element $parentElement,
): Element {
return $this->elementRepo->create(new CreateElementDto(
set: $set,
title: $title,
description: $description,
parentElement: $parentElement,
));
}
}