test element controller mapping

This commit is contained in:
Yisroel Baum 2026-05-26 19:59:55 +03:00
parent 46f5e6138e
commit aa77ccad81
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,75 @@
<?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
{
$element = $this->createElement('Baderech HaAvodah');
$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']);
}
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 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,
));
}
}