2019-02-19 19:36:02 +01:00
< ? 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 ;
2019-05-30 16:18:10 +02:00
use Infinito\Domain\Layer\LayerActionMap ;
2019-02-19 19:36:02 +01:00
use Infinito\DBAL\Types\ActionType ;
2019-05-30 16:20:42 +02:00
use Infinito\Domain\Map\ActionHttpMethodMap ;
2019-02-19 19:36:02 +01:00
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 ) {
2019-02-25 13:32:37 +01:00
foreach ( RESTResponseType :: getValues () as $format ) {
foreach ( LayerType :: getValues () as $layer ) {
2019-02-19 19:36:02 +01:00
$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 :
2019-02-19 19:47:31 +01:00
$url = " $baseUrl / $uri /execute. $format " ;
2019-02-19 19:36:02 +01:00
$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 );
2019-04-15 01:37:17 +02:00
$this -> assertTrue ( $this -> isResponseValid ( $response ), " Route $url with Method $method sends an 404 response and doesn't throw an EntityNotFoundException! " );
2019-02-19 19:36:02 +01:00
}
/**
* @ param Response $response
*
* @ return bool
*/
private function isResponseValid ( Response $response ) : bool
{
$is404 = 404 === $response -> getStatusCode ();
2019-04-15 01:37:17 +02:00
$isEntityNotFoundHttpException = strpos ( $response -> getContent (), 'EntityNotFoundException' );
2019-02-19 19:36:02 +01:00
return ! $is404 || $isEntityNotFoundHttpException ;
}
}