mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Added more Rest module tests
This commit is contained in:
parent
8c446f0f3b
commit
41939b790d
33
module/Rest/test/ConfigProviderTest.php
Normal file
33
module/Rest/test/ConfigProviderTest.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
namespace ShlinkioTest\Shlink\Rest;
|
||||||
|
|
||||||
|
use PHPUnit_Framework_TestCase as TestCase;
|
||||||
|
use Shlinkio\Shlink\Rest\ConfigProvider;
|
||||||
|
|
||||||
|
class ConfigProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ConfigProvider
|
||||||
|
*/
|
||||||
|
protected $configProvider;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->configProvider = new ConfigProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function properConfigIsReturned()
|
||||||
|
{
|
||||||
|
$config = $this->configProvider->__invoke();
|
||||||
|
|
||||||
|
$this->assertArrayHasKey('error_handler', $config);
|
||||||
|
$this->assertArrayHasKey('middleware_pipeline', $config);
|
||||||
|
$this->assertArrayHasKey('rest', $config);
|
||||||
|
$this->assertArrayHasKey('routes', $config);
|
||||||
|
$this->assertArrayHasKey('services', $config);
|
||||||
|
$this->assertArrayHasKey('translator', $config);
|
||||||
|
}
|
||||||
|
}
|
79
module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php
Normal file
79
module/Rest/test/ErrorHandler/JsonErrorHandlerTest.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
namespace ShlinkioTest\Shlink\Rest\ErrorHandler;
|
||||||
|
|
||||||
|
use PHPUnit_Framework_TestCase as TestCase;
|
||||||
|
use Shlinkio\Shlink\Rest\ErrorHandler\JsonErrorHandler;
|
||||||
|
use Zend\Diactoros\Response;
|
||||||
|
use Zend\Diactoros\ServerRequestFactory;
|
||||||
|
use Zend\Expressive\Router\RouteResult;
|
||||||
|
|
||||||
|
class JsonErrorHandlerTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var JsonErrorHandler
|
||||||
|
*/
|
||||||
|
protected $errorHandler;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->errorHandler = new JsonErrorHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function noMatchedRouteReturnsNotFoundResponse()
|
||||||
|
{
|
||||||
|
$response = $this->errorHandler->__invoke(ServerRequestFactory::fromGlobals(), new Response());
|
||||||
|
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||||
|
$this->assertEquals(404, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function matchedRouteWithErrorReturnsMethodNotAllowedResponse()
|
||||||
|
{
|
||||||
|
$response = $this->errorHandler->__invoke(
|
||||||
|
ServerRequestFactory::fromGlobals(),
|
||||||
|
(new Response())->withStatus(405),
|
||||||
|
405
|
||||||
|
);
|
||||||
|
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||||
|
$this->assertEquals(405, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function responseWithErrorKeepsStatus()
|
||||||
|
{
|
||||||
|
$response = $this->errorHandler->__invoke(
|
||||||
|
ServerRequestFactory::fromGlobals()->withAttribute(
|
||||||
|
RouteResult::class,
|
||||||
|
RouteResult::fromRouteMatch('foo', 'bar', [])
|
||||||
|
),
|
||||||
|
(new Response())->withStatus(401),
|
||||||
|
401
|
||||||
|
);
|
||||||
|
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||||
|
$this->assertEquals(401, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function responseWithoutErrorReturnsStatus500()
|
||||||
|
{
|
||||||
|
$response = $this->errorHandler->__invoke(
|
||||||
|
ServerRequestFactory::fromGlobals()->withAttribute(
|
||||||
|
RouteResult::class,
|
||||||
|
RouteResult::fromRouteMatch('foo', 'bar', [])
|
||||||
|
),
|
||||||
|
(new Response())->withStatus(200),
|
||||||
|
'Some error'
|
||||||
|
);
|
||||||
|
$this->assertInstanceOf(Response\JsonResponse::class, $response);
|
||||||
|
$this->assertEquals(500, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user