Added more Rest module tests

This commit is contained in:
Alejandro Celaya 2016-07-30 23:17:13 +02:00
parent 8c446f0f3b
commit 41939b790d
2 changed files with 112 additions and 0 deletions

View 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);
}
}

View 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());
}
}