2023-07-22 20:35:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Api;
|
|
|
|
|
2023-12-22 14:18:12 +01:00
|
|
|
use Engelsystem\Config\Config;
|
2023-07-22 20:35:45 +02:00
|
|
|
use Engelsystem\Controllers\Api\IndexController;
|
|
|
|
use Engelsystem\Http\Response;
|
|
|
|
|
|
|
|
class IndexControllerTest extends ApiBaseControllerTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::__construct
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::index
|
|
|
|
*/
|
|
|
|
public function testIndex(): void
|
|
|
|
{
|
2023-11-15 02:29:58 +01:00
|
|
|
$controller = new IndexController(new Response());
|
2023-07-22 20:35:45 +02:00
|
|
|
$response = $controller->index();
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
|
|
|
|
$this->assertEquals(['*'], $response->getHeader('access-control-allow-origin'));
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
$this->assertArrayHasKey('versions', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::indexV0
|
2023-11-15 22:41:44 +01:00
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::getApiSpecV0
|
2023-07-22 20:35:45 +02:00
|
|
|
*/
|
|
|
|
public function testIndexV0(): void
|
|
|
|
{
|
2023-11-15 02:29:58 +01:00
|
|
|
$controller = new IndexController(new Response());
|
2023-07-22 20:35:45 +02:00
|
|
|
$response = $controller->indexV0();
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
$this->assertArrayHasKey('version', $data);
|
|
|
|
$this->assertArrayHasKey('paths', $data);
|
|
|
|
}
|
|
|
|
|
2023-11-15 22:41:44 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::openApiV0
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::getApiSpecV0
|
|
|
|
*/
|
|
|
|
public function testOpenApiV0(): void
|
|
|
|
{
|
|
|
|
$controller = new IndexController(new Response());
|
|
|
|
|
|
|
|
$response = $controller->openApiV0();
|
|
|
|
$this->validateApiResponse('/openapi', 'get', $response);
|
|
|
|
|
|
|
|
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
$this->assertArrayHasKey('openapi', $data);
|
|
|
|
$this->assertArrayHasKey('info', $data);
|
|
|
|
}
|
|
|
|
|
2023-12-22 14:18:12 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::info
|
|
|
|
*/
|
|
|
|
public function testInfo(): void
|
|
|
|
{
|
|
|
|
$config = new Config(['name' => 'TestEvent', 'app_name' => 'TestSystem', 'timezone' => 'UTC']);
|
|
|
|
$this->app->instance('config', $config);
|
|
|
|
|
|
|
|
$controller = new IndexController(new Response());
|
|
|
|
|
|
|
|
$response = $controller->info();
|
|
|
|
$this->validateApiResponse('/info', 'get', $response);
|
|
|
|
|
|
|
|
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
|
|
|
|
$data = json_decode($response->getContent(), true);
|
|
|
|
$this->assertArrayHasKey('data', $data);
|
|
|
|
$data = $data['data'];
|
|
|
|
$this->assertArrayHasKey('api', $data);
|
|
|
|
$this->assertArrayHasKey('timezone', $data);
|
|
|
|
$this->assertEquals('UTC', $data['timezone']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::info
|
|
|
|
*/
|
|
|
|
public function testInfoNotConfigured(): void
|
|
|
|
{
|
|
|
|
$config = new Config([]);
|
|
|
|
$this->app->instance('config', $config);
|
|
|
|
|
|
|
|
$controller = new IndexController(new Response());
|
|
|
|
|
|
|
|
$response = $controller->info();
|
|
|
|
$this->validateApiResponse('/info', '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->assertArrayHasKey('name', $data['data']);
|
|
|
|
$this->assertEquals('', $data['data']['name']);
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:35:45 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::options
|
|
|
|
*/
|
|
|
|
public function testOptions(): void
|
|
|
|
{
|
2023-11-15 02:29:58 +01:00
|
|
|
$controller = new IndexController(new Response());
|
2023-07-22 20:35:45 +02:00
|
|
|
$response = $controller->options();
|
|
|
|
|
2023-07-31 00:26:19 +02:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
2023-07-22 20:35:45 +02:00
|
|
|
$this->assertNotEmpty($response->getHeader('allow'));
|
|
|
|
$this->assertNotEmpty($response->getHeader('access-control-allow-headers'));
|
|
|
|
}
|
|
|
|
|
2023-07-31 00:26:19 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::notFound
|
|
|
|
*/
|
|
|
|
public function testNotFound(): void
|
|
|
|
{
|
2023-11-15 02:29:58 +01:00
|
|
|
$controller = new IndexController(new Response());
|
2023-07-31 00:26:19 +02:00
|
|
|
$response = $controller->notFound();
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response->getStatusCode());
|
|
|
|
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
}
|
|
|
|
|
2023-07-22 20:35:45 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Controllers\Api\IndexController::notImplemented
|
|
|
|
*/
|
|
|
|
public function testNotImplemented(): void
|
|
|
|
{
|
2023-11-15 02:29:58 +01:00
|
|
|
$controller = new IndexController(new Response());
|
2023-07-22 20:35:45 +02:00
|
|
|
$response = $controller->notImplemented();
|
|
|
|
|
2023-07-31 00:26:19 +02:00
|
|
|
$this->assertEquals(405, $response->getStatusCode());
|
|
|
|
$this->assertEquals(['GET'], $response->getHeader('allow'));
|
2023-07-22 20:35:45 +02:00
|
|
|
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
|
|
|
|
$this->assertJson($response->getContent());
|
|
|
|
}
|
|
|
|
}
|