mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-19 00:39:55 +03:00
Created Common\ErrorHandler tests
This commit is contained in:
parent
4c6cc9cd11
commit
00db8a7ea5
3 changed files with 155 additions and 0 deletions
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
45
module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php
Normal file
45
module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php
Normal 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');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue