test element controller mapping
This commit is contained in:
parent
46f5e6138e
commit
aa77ccad81
1 changed files with 75 additions and 0 deletions
75
backend/tests/Unit/Controllers/ElementControllerTest.php
Normal file
75
backend/tests/Unit/Controllers/ElementControllerTest.php
Normal 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,
|
||||
));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue