Replaced old /api endpoint with ApiController

This commit is contained in:
Igor Scheller 2018-11-20 18:55:56 +01:00 committed by msquare
parent 93e578e555
commit e948091066
4 changed files with 58 additions and 5 deletions

View File

@ -10,3 +10,6 @@ $route->get('/credits', 'CreditsController@index');
// Stats
$route->get('/metrics', 'Metrics\\Controller@metrics');
$route->get('/stats', 'Metrics\\Controller@stats');
// API
$route->get('/api[/{resource:.+}]', 'ApiController@index');

View File

@ -0,0 +1,30 @@
<?php
namespace Engelsystem\Controllers;
use Engelsystem\Http\Response;
class ApiController extends BaseController
{
/** @var Response */
protected $response;
/**
* @param Response $response
*/
public function __construct(Response $response)
{
$this->response = $response;
}
/**
* @return Response
*/
public function index()
{
return $this->response
->setStatusCode(501)
->withHeader('content-type', 'application/json')
->withContent(json_encode(['error' => 'Not implemented']));
}
}

View File

@ -17,7 +17,6 @@ class LegacyMiddleware implements MiddlewareInterface
protected $free_pages = [
'admin_event_config',
'angeltypes',
'api',
'atom',
'ical',
'login',
@ -106,10 +105,6 @@ class LegacyMiddleware implements MiddlewareInterface
$title = ucfirst($page);
switch ($page) {
/** @noinspection PhpMissingBreakStatementInspection */
case 'api':
error('Api disabled temporarily.');
redirect(page_link_to());
/** @noinspection PhpMissingBreakStatementInspection */
case 'ical':
require_once realpath(__DIR__ . '/../../includes/pages/user_ical.php');
user_ical();

View File

@ -0,0 +1,25 @@
<?php
namespace Engelsystem\Test\Unit\Controllers;
use Engelsystem\Controllers\ApiController;
use Engelsystem\Http\Response;
use PHPUnit\Framework\TestCase;
class ApiControllerTest extends TestCase
{
/**
* @covers \Engelsystem\Controllers\ApiController::__construct
* @covers \Engelsystem\Controllers\ApiController::index
*/
public function testIndex()
{
$controller = new ApiController(new Response());
$response = $controller->index();
$this->assertEquals(501, $response->getStatusCode());
$this->assertEquals(['application/json'], $response->getHeader('content-type'));
$this->assertJson($response->getContent());
}
}