2023-07-22 20:35:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Api;
|
|
|
|
|
|
|
|
use Engelsystem\Controllers\Api\NewsController;
|
|
|
|
use Engelsystem\Http\Response;
|
|
|
|
use Engelsystem\Models\News;
|
|
|
|
|
|
|
|
class NewsControllerTest extends ApiBaseControllerTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\NewsController::index
|
2023-11-15 02:29:58 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Api\Resources\NewsResource::toArray
|
2023-07-22 20:35:45 +02:00
|
|
|
*/
|
|
|
|
public function testIndex(): void
|
|
|
|
{
|
2023-07-31 20:49:17 +02:00
|
|
|
$items = News::factory(3)->create();
|
2023-07-22 20:35:45 +02:00
|
|
|
|
2023-11-15 02:29:58 +01:00
|
|
|
$controller = new NewsController(new Response());
|
2023-07-22 20:35:45 +02:00
|
|
|
|
|
|
|
$response = $controller->index();
|
|
|
|
$this->validateApiResponse('/news', 'get', $response);
|
|
|
|
|
|
|
|
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
$this->assertArrayHasKey('data', $data);
|
|
|
|
$this->assertCount(3, $data['data']);
|
2023-07-31 20:49:17 +02:00
|
|
|
|
|
|
|
$this->assertCount(1, collect($data['data'])->filter(function ($item) use ($items) {
|
|
|
|
return $item['title'] == $items->first()->getAttribute('title');
|
|
|
|
}));
|
2023-07-22 20:35:45 +02:00
|
|
|
}
|
|
|
|
}
|