Created Common\ErrorHandler tests

This commit is contained in:
Alejandro Celaya 2016-07-30 17:07:35 +02:00
parent 4c6cc9cd11
commit 00db8a7ea5
3 changed files with 155 additions and 0 deletions

View file

@ -0,0 +1,75 @@
<?php
namespace ShlinkioTest\Shlink\Common\ErrorHandler;
use PHPUnit_Framework_TestCase as TestCase;
use Shlinkio\Shlink\Common\ErrorHandler\ContentBasedErrorHandler;
use Shlinkio\Shlink\Common\ErrorHandler\ErrorHandlerManager;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\ServiceManager\ServiceManager;
class ContentBasedErrorHandlerTest extends TestCase
{
/**
* @var ContentBasedErrorHandler
*/
protected $errorHandler;
public function setUp()
{
$this->errorHandler = new ContentBasedErrorHandler(new ErrorHandlerManager(new ServiceManager(), [
'factories' => [
'text/html' => [$this, 'factory'],
'application/json' => [$this, 'factory'],
],
]));
}
public function factory($container, $name)
{
return function () use ($name) {
return $name;
};
}
/**
* @test
*/
public function correctAcceptHeaderValueInvokesErrorHandler()
{
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept', 'foo/bar,application/json');
$result = $this->errorHandler->__invoke($request, new Response());
$this->assertEquals('application/json', $result);
}
/**
* @test
*/
public function defaultContentTypeIsUsedWhenNoAcceptHeaderisPresent()
{
$request = ServerRequestFactory::fromGlobals();
$result = $this->errorHandler->__invoke($request, new Response());
$this->assertEquals('text/html', $result);
}
/**
* @test
*/
public function defaultContentTypeIsUsedWhenAcceptedContentIsNotSupported()
{
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept', 'foo/bar,text/xml');
$result = $this->errorHandler->__invoke($request, new Response());
$this->assertEquals('text/html', $result);
}
/**
* @test
* @expectedException \Shlinkio\Shlink\Common\Exception\InvalidArgumentException
*/
public function ifNoErrorHandlerIsFoundAnExceptionIsThrown()
{
$this->errorHandler = new ContentBasedErrorHandler(new ErrorHandlerManager(new ServiceManager(), []));
$request = ServerRequestFactory::fromGlobals()->withHeader('Accept', 'foo/bar,text/xml');
$result = $this->errorHandler->__invoke($request, new Response());
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace ShlinkioTest\Shlink\Common\ErrorHandler;
use PHPUnit_Framework_TestCase as TestCase;
use Shlinkio\Shlink\Common\ErrorHandler\ErrorHandlerManager;
use Shlinkio\Shlink\Common\ErrorHandler\ErrorHandlerManagerFactory;
use Zend\ServiceManager\ServiceManager;
class ErrorHandlerManagerFactoryTest extends TestCase
{
/**
* @var ErrorHandlerManagerFactory
*/
protected $factory;
public function setUp()
{
$this->factory = new ErrorHandlerManagerFactory();
}
/**
* @test
*/
public function serviceIsCreated()
{
$instance = $this->factory->__invoke(new ServiceManager(['services' => [
'config' => [
'error_handler' => [
'plugins' => [],
],
],
]]), '');
$this->assertInstanceOf(ErrorHandlerManager::class, $instance);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace ShlinkioTest\Shlink\Common\ErrorHandler;
use PHPUnit_Framework_TestCase as TestCase;
use Shlinkio\Shlink\Common\ErrorHandler\ErrorHandlerManager;
use Zend\ServiceManager\ServiceManager;
class ErrorHandlerManagerTest extends TestCase
{
/**
* @var ErrorHandlerManager
*/
protected $pluginManager;
public function setUp()
{
$this->pluginManager = new ErrorHandlerManager(new ServiceManager(), [
'services' => [
'foo' => function () {
},
],
'invokables' => [
'invalid' => \stdClass::class,
]
]);
}
/**
* @test
*/
public function callablesAreReturned()
{
$instance = $this->pluginManager->get('foo');
$this->assertInstanceOf(\Closure::class, $instance);
}
/**
* @test
* @expectedException \Zend\ServiceManager\Exception\InvalidServiceException
*/
public function nonCallablesThrowException()
{
$this->pluginManager->get('invalid');
}
}