mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-09-13 05:47:11 +02:00
Optimized REST API and tests
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# API
|
||||
This folder contains the different API controllers.
|
||||
Right now just an REST API is implemented.
|
||||
In the future it would also make sense to implement an [GraphQL](https://graphql.org/) API.
|
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace tests\Integration\Controller\API\Rest;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Infinito\DBAL\Types\Meta\Right\LayerType;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*/
|
||||
class ControllerLayerIntegrationTest extends TestCase
|
||||
{
|
||||
public function testThatControllerForEachLayerExist(): void
|
||||
{
|
||||
foreach (LayerType::getChoices() as $layer) {
|
||||
$className = 'Infinito\\Controller\\API\\Rest\\'.ucfirst($layer).'Controller';
|
||||
$this->assertTrue(class_exists($className));
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Integration\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Infinito\DBAL\Types\Meta\Right\LayerType;
|
||||
use Infinito\DBAL\Types\RESTResponseType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Infinito\Domain\LayerManagement\LayerActionMap;
|
||||
use Infinito\DBAL\Types\ActionType;
|
||||
use Infinito\Domain\MapManagement\ActionHttpMethodMap;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*
|
||||
* @todo Implement more tests for success etc.
|
||||
*/
|
||||
class RestRoutesReachableIntegrationTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \PHPUnit\Framework\TestCase::setUp()
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
public function testAllRoutePossibilities(): void
|
||||
{
|
||||
foreach ([
|
||||
'12314123',
|
||||
'testslug',
|
||||
] as $uri) {
|
||||
foreach (RESTResponseType::getChoices() as $format) {
|
||||
foreach (LayerType::getChoices() as $layer) {
|
||||
$actions = LayerActionMap::getActions($layer);
|
||||
foreach ($actions as $action) {
|
||||
foreach (ActionHttpMethodMap::getHttpMethods($action) as $method) {
|
||||
$baseUrl = "api/rest/$layer";
|
||||
switch ($action) {
|
||||
case ActionType::CREATE:
|
||||
$url = "$baseUrl.$format";
|
||||
$this->routeAssert($url, $method);
|
||||
break;
|
||||
case ActionType::EXECUTE:
|
||||
$url = "$baseUrl/$uri/action/execute.$format";
|
||||
$this->routeAssert($url, $method);
|
||||
break;
|
||||
default:
|
||||
$url = "$baseUrl/$uri.$format";
|
||||
$this->routeAssert($url, $method);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
*/
|
||||
private function routeAssert(string $url, string $method): void
|
||||
{
|
||||
$request = new Request([], [], [], [], [], [
|
||||
'REQUEST_URI' => $url,
|
||||
]);
|
||||
$request->setMethod($method);
|
||||
$response = static::$kernel->handle($request);
|
||||
$this->assertTrue($this->isResponseValid($response), "Route $url with Method $method sends an 404 response and doesn't throw an EntityNotFoundHttpException!");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Response $response
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isResponseValid(Response $response): bool
|
||||
{
|
||||
$is404 = 404 === $response->getStatusCode();
|
||||
$isEntityNotFoundHttpException = strpos($response->getContent(), 'EntityNotFoundHttpException');
|
||||
|
||||
return !$is404 || $isEntityNotFoundHttpException;
|
||||
}
|
||||
}
|
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Integration\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Infinito\DBAL\Types\LanguageType;
|
||||
use Infinito\DBAL\Types\Meta\Right\LayerType;
|
||||
use Infinito\DBAL\Types\RESTResponseType;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @author kevinfrantz
|
||||
*
|
||||
* @todo Implement more tests for success etc.
|
||||
*/
|
||||
class RoutesReachableIntegrationTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \PHPUnit\Framework\TestCase::setUp()
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
public function testAllRoutePossibilities()
|
||||
{
|
||||
foreach (LayerType::getChoices() as $layer) {
|
||||
$this->controller($layer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $entity
|
||||
*/
|
||||
private function controller(string $entity): void
|
||||
{
|
||||
$this->language($entity, Request::METHOD_GET);
|
||||
$this->language($entity, Request::METHOD_POST);
|
||||
$this->language($entity.'s', Request::METHOD_GET);
|
||||
$this->slugAndId($entity, Request::METHOD_PUT);
|
||||
$this->slugAndId($entity, Request::METHOD_GET);
|
||||
$this->slugAndId($entity, Request::METHOD_DELETE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $method
|
||||
*/
|
||||
private function slugAndId(string $route, string $method): void
|
||||
{
|
||||
$this->language("$route/12345", $method);
|
||||
$this->language("$route/asdfg", $method);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Implement routing without i18l part!
|
||||
*
|
||||
* @param string $entity
|
||||
* @param string $method
|
||||
*/
|
||||
private function language(string $entity, string $method): void
|
||||
{
|
||||
//$this->type('api/'.$entity, $method);
|
||||
foreach (LanguageType::getChoices() as $language) {
|
||||
$this->type("$language/api/$entity", $method);
|
||||
$this->type("$language/html/$entity", $method);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $method
|
||||
*/
|
||||
private function type(string $route, string $method): void
|
||||
{
|
||||
$this->routeAssert($route, $method);
|
||||
foreach (RESTResponseType::getChoices() as $restResponseType => $value) {
|
||||
$this->routeAssert("$route.$restResponseType", $method);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
*/
|
||||
private function routeAssert(string $url, string $method): void
|
||||
{
|
||||
$request = new Request([], [], [], [], [], [
|
||||
'REQUEST_URI' => $url,
|
||||
]);
|
||||
$request->setMethod($method);
|
||||
$response = static::$kernel->handle($request);
|
||||
$this->assertNotEquals(404, $response->getStatusCode(), "Route $url with Method $method sends an 404 response!");
|
||||
}
|
||||
}
|
@@ -10,6 +10,11 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
*/
|
||||
class ImprintFixtureSourceTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \PHPUnit\Framework\TestCase::setUp()
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
@@ -18,7 +23,7 @@ class ImprintFixtureSourceTest extends KernelTestCase
|
||||
public function testImprintSourceReachable(): void
|
||||
{
|
||||
$request = new Request([], [], [], [], [], [
|
||||
'REQUEST_URI' => 'source/imprint.html',
|
||||
'REQUEST_URI' => 'api/rest/source/imprint.html',
|
||||
]);
|
||||
$request->setMethod(Request::METHOD_GET);
|
||||
$response = static::$kernel->handle($request);
|
||||
|
@@ -25,7 +25,7 @@ class ActionHttpMethodTest extends TestCase
|
||||
|
||||
public function testCreateActionTrue(): void
|
||||
{
|
||||
$subset = [Request::METHOD_GET, Request::METHOD_POST];
|
||||
$subset = [Request::METHOD_POST, Request::METHOD_HEAD];
|
||||
$action = ActionType::CREATE;
|
||||
$haystack = ActionHttpMethodMap::getHttpMethods($action);
|
||||
$this->assertSubsetInArray($subset, $haystack, true);
|
||||
@@ -34,7 +34,7 @@ class ActionHttpMethodTest extends TestCase
|
||||
|
||||
public function testCreateActionFalse(): void
|
||||
{
|
||||
$subset = [Request::METHOD_GET, Request::METHOD_POST];
|
||||
$subset = [Request::METHOD_POST, Request::METHOD_HEAD];
|
||||
$action = 'wrong value';
|
||||
$haystack = ActionHttpMethodMap::getHttpMethods($action);
|
||||
$this->assertSubsetInArray($subset, $haystack, false);
|
||||
|
Reference in New Issue
Block a user