From 00db8a7ea5e46e91af90c8d8152b9f933fce3d31 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Jul 2016 17:07:35 +0200 Subject: [PATCH] Created Common\ErrorHandler tests --- .../ContentBasedErrorHandlerTest.php | 75 +++++++++++++++++++ .../ErrorHandlerManagerFactoryTest.php | 35 +++++++++ .../ErrorHandler/ErrorHandlerManagerTest.php | 45 +++++++++++ 3 files changed, 155 insertions(+) create mode 100644 module/Common/test/ErrorHandler/ContentBasedErrorHandlerTest.php create mode 100644 module/Common/test/ErrorHandler/ErrorHandlerManagerFactoryTest.php create mode 100644 module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php diff --git a/module/Common/test/ErrorHandler/ContentBasedErrorHandlerTest.php b/module/Common/test/ErrorHandler/ContentBasedErrorHandlerTest.php new file mode 100644 index 00000000..6b480e54 --- /dev/null +++ b/module/Common/test/ErrorHandler/ContentBasedErrorHandlerTest.php @@ -0,0 +1,75 @@ +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()); + } +} diff --git a/module/Common/test/ErrorHandler/ErrorHandlerManagerFactoryTest.php b/module/Common/test/ErrorHandler/ErrorHandlerManagerFactoryTest.php new file mode 100644 index 00000000..be6d4e6d --- /dev/null +++ b/module/Common/test/ErrorHandler/ErrorHandlerManagerFactoryTest.php @@ -0,0 +1,35 @@ +factory = new ErrorHandlerManagerFactory(); + } + + /** + * @test + */ + public function serviceIsCreated() + { + $instance = $this->factory->__invoke(new ServiceManager(['services' => [ + 'config' => [ + 'error_handler' => [ + 'plugins' => [], + ], + ], + ]]), ''); + $this->assertInstanceOf(ErrorHandlerManager::class, $instance); + } +} diff --git a/module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php b/module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php new file mode 100644 index 00000000..4b14f113 --- /dev/null +++ b/module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php @@ -0,0 +1,45 @@ +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'); + } +}