From abb99bf36c6c09f395c49ca34eb49d2ba6ff224d Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 21:19:56 +0200 Subject: Added Config unit test --- tests/Unit/Config/ConfigTest.php | 115 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/Unit/Config/ConfigTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php new file mode 100644 index 00000000..ce11ebd6 --- /dev/null +++ b/tests/Unit/Config/ConfigTest.php @@ -0,0 +1,115 @@ +set('test', 'FooBar'); + $this->assertEquals(['test' => 'FooBar'], $config->get(null)); + $this->assertEquals('FooBar', $config->get('test')); + + $this->assertEquals('defaultValue', $config->get('notExisting', 'defaultValue')); + + $this->assertNull($config->get('notExisting')); + } + + /** + * @covers \Engelsystem\Config\Config::set + */ + public function testSet() + { + $config = new Config(); + + $config->set('test', 'FooBar'); + $this->assertEquals('FooBar', $config->get('test')); + + $config->set([ + 'name' => 'Engelsystem', + 'mail' => ['user' => 'test'], + ]); + $this->assertEquals('Engelsystem', $config->get('name')); + $this->assertEquals(['user' => 'test'], $config->get('mail')); + } + + /** + * @covers \Engelsystem\Config\Config::has + */ + public function testHas() + { + $config = new Config(); + + $this->assertFalse($config->has('test')); + + $config->set('test', 'FooBar'); + $this->assertTrue($config->has('test')); + } + + /** + * @covers \Engelsystem\Config\Config::remove + */ + public function testRemove() + { + $config = new Config(); + $config->set(['foo' => 'bar', 'test' => '123']); + + $config->remove('foo'); + $this->assertEquals(['test' => '123'], $config->get(null)); + } + + /** + * @covers \Engelsystem\Config\Config::__get + */ + public function testMagicGet() + { + $config = new Config(); + + $config->set('test', 'FooBar'); + $this->assertEquals('FooBar', $config->test); + } + + /** + * @covers \Engelsystem\Config\Config::__set + */ + public function testMagicSet() + { + $config = new Config(); + + $config->test = 'FooBar'; + $this->assertEquals('FooBar', $config->get('test')); + } + + /** + * @covers \Engelsystem\Config\Config::__isset + */ + public function testMagicIsset() + { + $config = new Config(); + + $this->assertFalse(isset($config->test)); + + $config->set('test', 'FooBar'); + $this->assertTrue(isset($config->test)); + } + + /** + * @covers \Engelsystem\Config\Config::__unset + */ + public function testMagicUnset() + { + $config = new Config(); + $config->set(['foo' => 'bar', 'test' => '123']); + + unset($config->foo); + $this->assertEquals(['test' => '123'], $config->get(null)); + } +} -- cgit v1.2.3-70-g09d2 From 2cb636b651c889243919d99eda8fa724d5c08392 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 21:50:22 +0200 Subject: Added Container unit test --- src/Container/Container.php | 2 +- tests/Unit/Container/ContainerTest.php | 98 ++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Container/ContainerTest.php (limited to 'tests/Unit') diff --git a/src/Container/Container.php b/src/Container/Container.php index 9af5c1e6..59a17a04 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -38,7 +38,7 @@ class Container implements ContainerInterface return $this->resolve($id); } - throw new NotFoundException(sprintf('The entry with the id "%s" could not be found')); + throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id)); } /** diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php new file mode 100644 index 00000000..f0ba24e7 --- /dev/null +++ b/tests/Unit/Container/ContainerTest.php @@ -0,0 +1,98 @@ +instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::get + * @expectedException \Engelsystem\Container\NotFoundException + */ + public function testGetException() + { + $container = new Container(); + + $container->get('not.registered.service'); + } + + /** + * @covers \Engelsystem\Container\Container::instance + * @covers \Engelsystem\Container\Container::resolve + */ + public function testInstance() + { + $container = new Container(); + $class = new class + { + }; + + $container->instance('foo', $class); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::has + */ + public function testHas() + { + $container = new Container(); + + $this->assertFalse($container->has('test')); + + $class = new class + { + }; + + $container->instance('test', $class); + $this->assertTrue($container->has('test')); + } + + /** + * @covers \Engelsystem\Container\Container::singleton + */ + public function testSingleton() + { + $container = new Container(); + $class = new class + { + }; + + $container->singleton('foo', $class); + $this->assertSame($class, $container->get('foo')); + $this->assertSame($class, $container->get('foo')); + } + + /** + * @covers \Engelsystem\Container\Container::setInstance + * @covers \Engelsystem\Container\Container::getInstance + */ + public function testContainerSingleton() + { + $container0 = new Container(); + $container = Container::getInstance(); + + $this->assertNotSame($container0, $container); + + $container1 = new Container; + Container::setInstance($container1); + + $this->assertSame($container1, Container::getInstance()); + } +} -- cgit v1.2.3-70-g09d2 From 1e267ce3b133299f82661a37d82c0f50e8575e1e Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 23:55:24 +0200 Subject: Added Renderer unit test --- src/Renderer/HtmlEngine.php | 2 +- src/Renderer/Renderer.php | 9 ++++- tests/Unit/Renderer/HtmlEngineTest.php | 67 ++++++++++++++++++++++++++++++++++ tests/Unit/Renderer/RendererTest.php | 55 ++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Renderer/HtmlEngineTest.php create mode 100644 tests/Unit/Renderer/RendererTest.php (limited to 'tests/Unit') diff --git a/src/Renderer/HtmlEngine.php b/src/Renderer/HtmlEngine.php index 4a48e1f0..75343bbd 100644 --- a/src/Renderer/HtmlEngine.php +++ b/src/Renderer/HtmlEngine.php @@ -29,6 +29,6 @@ class HtmlEngine implements EngineInterface */ public function canRender($path) { - return strpos($path, '.html') && file_exists($path); + return strpos($path, '.htm') && file_exists($path); } } diff --git a/src/Renderer/Renderer.php b/src/Renderer/Renderer.php index 5ed7cf31..de31ca74 100644 --- a/src/Renderer/Renderer.php +++ b/src/Renderer/Renderer.php @@ -2,8 +2,12 @@ namespace Engelsystem\Renderer; +use Psr\Log\LoggerAwareTrait; + class Renderer { + use LoggerAwareTrait; + /** @var EngineInterface[] */ protected $renderer = []; @@ -24,7 +28,10 @@ class Renderer return $renderer->get($template, $data); } - engelsystem_error('Unable to find a renderer for template file "' . $template . '".'); + if ($this->logger) { + $this->logger->error('Unable to find a renderer for template file "{file}"', ['file' => $template]); + } + return ''; } diff --git a/tests/Unit/Renderer/HtmlEngineTest.php b/tests/Unit/Renderer/HtmlEngineTest.php new file mode 100644 index 00000000..0b317b72 --- /dev/null +++ b/tests/Unit/Renderer/HtmlEngineTest.php @@ -0,0 +1,67 @@ +createTempFile('
%main_content%
'); + + $data = $engine->get($file, ['main_content' => 'Lorem ipsum dolor sit']); + $this->assertEquals('
Lorem ipsum dolor sit
', $data); + } + + /** + * @covers \Engelsystem\Renderer\HtmlEngine::canRender + */ + public function testCanRender() + { + $engine = new HtmlEngine(); + + $this->assertFalse($engine->canRender('/dev/null')); + + $file = $this->createTempFile(); + $this->assertTrue($engine->canRender($file)); + + $htmFile = $this->createTempFile('', '.htm'); + $this->assertTrue($engine->canRender($htmFile)); + } + + /** + * @param string $content + * @param string $extension + * @return string + */ + protected function createTempFile($content = '', $extension = '.html') + { + $tmpFileName = tempnam(sys_get_temp_dir(), 'EngelsystemUnitTest'); + + $fileName = $tmpFileName . $extension; + rename($tmpFileName, $fileName); + + file_put_contents($fileName, $content); + + $this->tmpFileNames[] = $fileName; + + return $fileName; + } + + public function tearDown() + { + foreach ($this->tmpFileNames as $fileName) { + unlink($fileName); + } + } +} diff --git a/tests/Unit/Renderer/RendererTest.php b/tests/Unit/Renderer/RendererTest.php new file mode 100644 index 00000000..b0238078 --- /dev/null +++ b/tests/Unit/Renderer/RendererTest.php @@ -0,0 +1,55 @@ +getMockForAbstractClass(EngineInterface::class); + + $nullRenderer->expects($this->atLeastOnce()) + ->method('canRender') + ->willReturn(false); + $renderer->addRenderer($nullRenderer); + + $mockRenderer = $this->getMockForAbstractClass(EngineInterface::class); + + $mockRenderer->expects($this->atLeastOnce()) + ->method('canRender') + ->with('foo.template') + ->willReturn(true); + + $mockRenderer->expects($this->atLeastOnce()) + ->method('get') + ->with('foo.template', ['lorem' => 'ipsum']) + ->willReturn('Rendered content'); + + $renderer->addRenderer($mockRenderer); + $data = $renderer->render('foo.template', ['lorem' => 'ipsum']); + + $this->assertEquals('Rendered content', $data); + } + + public function testError() + { + $renderer = new Renderer(); + + $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); + $loggerMock + ->expects($this->once()) + ->method('error'); + + $renderer->setLogger($loggerMock); + + $data = $renderer->render('testing.template'); + $this->assertEquals('', $data); + } +} -- cgit v1.2.3-70-g09d2 From b8d9ab0acb9c23419e6026d13e9b7dd305f5c919 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 23:55:40 +0200 Subject: Added UrlGenerator unit test --- tests/Unit/Routing/UrlGeneratorTest.php | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/Unit/Routing/UrlGeneratorTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/Routing/UrlGeneratorTest.php b/tests/Unit/Routing/UrlGeneratorTest.php new file mode 100644 index 00000000..5b53a04e --- /dev/null +++ b/tests/Unit/Routing/UrlGeneratorTest.php @@ -0,0 +1,50 @@ + 'abc', 'bla' => 'foo'], 'http://f.b/foo?test=abc&bla=foo'], + ]; + } + + /** + * @dataProvider provideLinksTo + * @covers \Engelsystem\Routing\UrlGenerator::to + * + * @param string $path + * @param string $willReturn + * @param string $urlToPath + * @param string[] $arguments + * @param string $expectedUrl + */ + public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) + { + $app = new Container(); + Application::setInstance($app); + + $request = $this->getMockBuilder(Request::class) + ->getMock(); + + $request->expects($this->once()) + ->method('getUriForPath') + ->with($path) + ->willReturn($willReturn); + + $app->instance('request', $request); + + $url = UrlGenerator::to($urlToPath, $arguments); + $this->assertEquals($expectedUrl, $url); + } +} -- cgit v1.2.3-70-g09d2 From c8d32d23e195d53d6d10fdac5003718b10ffbeaa Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 20 Sep 2017 00:04:59 +0200 Subject: Added Application unit test --- tests/Unit/ApplicationTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/Unit/ApplicationTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php new file mode 100644 index 00000000..77429f44 --- /dev/null +++ b/tests/Unit/ApplicationTest.php @@ -0,0 +1,29 @@ +assertInstanceOf(Container::class, $app); + $this->assertInstanceOf(ContainerInterface::class, $app); + $this->assertSame($app, $app->get('app')); + $this->assertSame($app, $app->get('container')); + $this->assertSame($app, $app->get(Container::class)); + $this->assertSame($app, $app->get(Application::class)); + $this->assertSame($app, $app->get(ContainerInterface::class)); + $this->assertSame($app, Container::getInstance()); + } +} -- cgit v1.2.3-70-g09d2 From 86c0713baa2f616bf1dff6d9dbe0ea68b1c00e91 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 20 Sep 2017 01:09:11 +0200 Subject: Added helpers unit test --- includes/engelsystem_provider.php | 8 ++ src/Routing/UrlGenerator.php | 2 +- src/helpers.php | 14 ++- tests/Unit/HelpersTest.php | 152 ++++++++++++++++++++++++++++++++ tests/Unit/Routing/UrlGeneratorTest.php | 3 +- 5 files changed, 173 insertions(+), 6 deletions(-) create mode 100644 tests/Unit/HelpersTest.php (limited to 'tests/Unit') diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index a9305df5..cd22f6a7 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -8,6 +8,7 @@ use Engelsystem\Http\Request; use Engelsystem\Logger\EngelsystemLogger; use Engelsystem\Renderer\HtmlEngine; use Engelsystem\Renderer\Renderer; +use Engelsystem\Routing\UrlGenerator; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; @@ -59,6 +60,13 @@ if ($config->get('maintenance')) { } +/** + * Register UrlGenerator + */ +$urlGenerator = new UrlGenerator(); +$app->instance('routing.urlGenerator', $urlGenerator); + + /** * Initialize renderer */ diff --git a/src/Routing/UrlGenerator.php b/src/Routing/UrlGenerator.php index 33eef7b0..6df52425 100644 --- a/src/Routing/UrlGenerator.php +++ b/src/Routing/UrlGenerator.php @@ -9,7 +9,7 @@ class UrlGenerator * @param array $parameters * @return string */ - public static function to($path, $parameters = []) + public function to($path, $parameters = []) { $path = '/' . ltrim($path, '/'); $request = app('request'); diff --git a/src/helpers.php b/src/helpers.php index b942068f..de303963 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -69,7 +69,7 @@ function request($key = null, $default = null) */ function session($key = null, $default = null) { - $session = request()->getSession(); + $session = app('session'); if (is_null($key)) { return $session; @@ -97,9 +97,15 @@ function view($template = null, $data = null) /** * @param string $path * @param array $parameters - * @return string + * @return UrlGenerator|string */ -function url($path, $parameters = []) +function url($path = null, $parameters = []) { - return UrlGenerator::to($path, $parameters); + $urlGenerator = app('routing.urlGenerator'); + + if (is_null($path)) { + return $urlGenerator; + } + + return $urlGenerator->to($path, $parameters); } diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php new file mode 100644 index 00000000..d9782888 --- /dev/null +++ b/tests/Unit/HelpersTest.php @@ -0,0 +1,152 @@ +getAppMock('some.name', $class); + + $this->assertEquals($appMock, app()); + $this->assertEquals($class, app('some.name')); + } + + /** + * @covers \config + */ + public function testConfig() + { + $configMock = $this->getMockBuilder(Config::class) + ->getMock(); + + $this->getAppMock('config', $configMock); + $this->assertEquals($configMock, config()); + + $configMock->expects($this->once()) + ->method('set') + ->with(['foo' => 'bar']); + + $this->assertTrue(config(['foo' => 'bar'])); + + $configMock->expects($this->once()) + ->method('get') + ->with('mail') + ->willReturn(['user' => 'FooBar']); + + $this->assertEquals(['user' => 'FooBar'], config('mail')); + } + + /** + * @covers \request + */ + public function testRequest() + { + $requestMock = $this->getMockBuilder(Request::class) + ->getMock(); + + $this->getAppMock('request', $requestMock); + $this->assertEquals($requestMock, request()); + + $requestMock->expects($this->once()) + ->method('input') + ->with('requestKey') + ->willReturn('requestValue'); + + $this->assertEquals('requestValue', request('requestKey')); + } + + /** + * @covers \session + */ + public function testSession() + { + $sessionMock = $this->getMockBuilder(Session::class) + ->getMock(); + + $this->getAppMock('session', $sessionMock); + $this->assertEquals($sessionMock, session()); + + $sessionMock->expects($this->once()) + ->method('get') + ->with('someKey') + ->willReturn('someValue'); + + $this->assertEquals('someValue', session('someKey')); + } + + /** + * @covers \view + */ + public function testView() + { + $rendererMock = $this->getMockBuilder(Renderer::class) + ->getMock(); + + $this->getAppMock('renderer', $rendererMock); + $this->assertEquals($rendererMock, view()); + + $rendererMock->expects($this->once()) + ->method('render') + ->with('template.name', ['template' => 'data']) + ->willReturn('rendered template'); + + $this->assertEquals('rendered template', view('template.name', ['template' => 'data'])); + } + + /** + * @covers \url + */ + public function testUrl() + { + $urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class) + ->getMock(); + + $this->getAppMock('routing.urlGenerator', $urlGeneratorMock); + $this->assertEquals($urlGeneratorMock, url()); + + $urlGeneratorMock->expects($this->once()) + ->method('to') + ->with('foo/bar', ['param' => 'value']) + ->willReturn('http://lorem.ipsum/foo/bar?param=value'); + + $this->assertEquals('http://lorem.ipsum/foo/bar?param=value', url('foo/bar', ['param' => 'value'])); + } + + /** + * @param string $alias + * @param object $object + * @return Application|\PHPUnit_Framework_MockObject_MockObject + */ + protected function getAppMock($alias, $object) + { + $appMock = $this->getMockBuilder(Container::class) + ->getMock(); + + $appMock->expects($this->atLeastOnce()) + ->method('get') + ->with($alias) + ->willReturn($object); + + /** @var $appMock Application */ + Application::setInstance($appMock); + + return $appMock; + } +} diff --git a/tests/Unit/Routing/UrlGeneratorTest.php b/tests/Unit/Routing/UrlGeneratorTest.php index 5b53a04e..fc23520a 100644 --- a/tests/Unit/Routing/UrlGeneratorTest.php +++ b/tests/Unit/Routing/UrlGeneratorTest.php @@ -32,6 +32,7 @@ class UrlGeneratorTest extends TestCase public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) { $app = new Container(); + $urlGenerator = new UrlGenerator(); Application::setInstance($app); $request = $this->getMockBuilder(Request::class) @@ -44,7 +45,7 @@ class UrlGeneratorTest extends TestCase $app->instance('request', $request); - $url = UrlGenerator::to($urlToPath, $arguments); + $url = $urlGenerator->to($urlToPath, $arguments); $this->assertEquals($expectedUrl, $url); } } -- cgit v1.2.3-70-g09d2 From 3141c3f8b5fd1a45d337c1f5540cb715a5a7dbbf Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 20 Sep 2017 01:21:36 +0200 Subject: Fixed Container test --- phpunit.xml | 1 - tests/Unit/Container/ContainerTest.php | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'tests/Unit') diff --git a/phpunit.xml b/phpunit.xml index 92dc5a5b..bdc4b0b6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,7 +14,6 @@ ./include/ - ./public/ ./src/ diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php index f0ba24e7..89c34209 100644 --- a/tests/Unit/Container/ContainerTest.php +++ b/tests/Unit/Container/ContainerTest.php @@ -85,6 +85,12 @@ class ContainerTest extends TestCase */ public function testContainerSingleton() { + // Ensure that no container has been initialized + $reflection = new \ReflectionProperty(Container::class, 'instance'); + $reflection->setAccessible(true); + $reflection->setValue(null, null); + $reflection->setAccessible(false); + $container0 = new Container(); $container = Container::getInstance(); -- cgit v1.2.3-70-g09d2 From 212760d4c93ce14e9ae34ef207bbb8f48a7dd9a7 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 21 Sep 2017 18:37:37 +0200 Subject: Changed Container to Illuminate/Container @see https://laravel.com/docs/5.5/container @see https://davejamesmiller.com/2017/06/15/laravel-illuminate-container-in-depth --- composer.json | 9 +-- includes/engelsystem_provider.php | 4 +- src/Application.php | 4 +- src/Container/Container.php | 110 +-------------------------------- src/Container/ContainerException.php | 11 ---- src/Container/NotFoundException.php | 10 --- tests/Unit/ApplicationTest.php | 1 + tests/Unit/Container/ContainerTest.php | 104 ------------------------------- 8 files changed, 12 insertions(+), 241 deletions(-) delete mode 100644 src/Container/ContainerException.php delete mode 100644 src/Container/NotFoundException.php delete mode 100644 tests/Unit/Container/ContainerTest.php (limited to 'tests/Unit') diff --git a/composer.json b/composer.json index 35956e20..6467fc1c 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,12 @@ ], "require": { "php": ">=7.0.0", - "erusev/parsedown": "1.6.*", - "twbs/bootstrap": "^3.3", - "symfony/http-foundation": "^3.3", + "erusev/parsedown": "^1.6", + "illuminate/container": "^5.*", "psr/container": "^1.0", - "psr/log": "^1.0" + "psr/log": "^1.0", + "symfony/http-foundation": "^3.3", + "twbs/bootstrap": "^3.3" }, "require-dev": { "phpunit/phpunit": "^6.3" diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 0de5e0f5..33422bfc 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -106,8 +106,8 @@ Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); */ $logger = new EngelsystemLogger(); $app->instance('logger', $logger); -$app->instance(LoggerInterface::class, $logger); -$app->instance(EngelsystemLogger::class, $logger); +$app->bind(LoggerInterface::class, 'logger'); +$app->bind(EngelsystemLogger::class, 'logger'); /** diff --git a/src/Application.php b/src/Application.php index 674b3869..fa895d77 100644 --- a/src/Application.php +++ b/src/Application.php @@ -14,12 +14,12 @@ class Application extends Container protected function registerBaseBindings() { - self::setInstance($this); + static::setInstance($this); Container::setInstance($this); $this->instance('app', $this); $this->instance('container', $this); $this->instance(Container::class, $this); $this->instance(Application::class, $this); - $this->instance(ContainerInterface::class, $this); + $this->bind(ContainerInterface::class, Application::class); } } diff --git a/src/Container/Container.php b/src/Container/Container.php index 59a17a04..44c57b6f 100644 --- a/src/Container/Container.php +++ b/src/Container/Container.php @@ -2,115 +2,9 @@ namespace Engelsystem\Container; -use Psr\Container\ContainerExceptionInterface; +use Illuminate\Container\Container as IlluminateContainer; use Psr\Container\ContainerInterface; -use Psr\Container\NotFoundExceptionInterface; -class Container implements ContainerInterface +class Container extends IlluminateContainer implements ContainerInterface { - /** - * The globally available container - * - * @var static - */ - protected static $instance; - - /** - * Contains the shared instances - * - * @var mixed[] - */ - protected $instances = []; - - /** - * Finds an entry of the container by its identifier and returns it - * - * @param string $id Identifier of the entry to look for - * - * @throws NotFoundExceptionInterface No entry was found for **this** identifier - * @throws ContainerExceptionInterface Error while retrieving the entry - * - * @return mixed Entry - */ - public function get($id) - { - if ($this->has($id)) { - return $this->resolve($id); - } - - throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id)); - } - - /** - * Register a shared entry in the container - * - * @param string $abstract Identifier of the entry to set - * @param mixed $instance Entry - */ - public function instance($abstract, $instance) - { - $this->singleton($abstract, $instance); - } - - /** - * Register a shared entry as singleton in the container - * - * @param string $abstract - * @param mixed $instance - */ - public function singleton($abstract, $instance) - { - $this->instances[$abstract] = $instance; - } - - /** - * Returns true if the container can return an entry for the given identifier - * Returns false otherwise - * - * `has($id)` returning true does not mean that `get($id)` will not throw an exception - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface` - * - * @param string $id Identifier of the entry to look for - * - * @return bool - */ - public function has($id) - { - return isset($this->instances[$id]); - } - - /** - * Resolve the requested object - * - * @param string $abstract - * @return mixed - */ - protected function resolve($abstract) - { - return $this->instances[$abstract]; - } - - /** - * Get the globally available instance of the container - * - * @return self - */ - public static function getInstance() - { - if (is_null(static::$instance)) { - static::$instance = new static; - } - - return static::$instance; - } - - /** - * Set the globally available instance of the container - * - * @param Container $container - */ - public static function setInstance(Container $container) - { - static::$instance = $container; - } } diff --git a/src/Container/ContainerException.php b/src/Container/ContainerException.php deleted file mode 100644 index 3cdde506..00000000 --- a/src/Container/ContainerException.php +++ /dev/null @@ -1,11 +0,0 @@ -assertSame($app, $app->get(Container::class)); $this->assertSame($app, $app->get(Application::class)); $this->assertSame($app, $app->get(ContainerInterface::class)); + $this->assertSame($app, Application::getInstance()); $this->assertSame($app, Container::getInstance()); } } diff --git a/tests/Unit/Container/ContainerTest.php b/tests/Unit/Container/ContainerTest.php deleted file mode 100644 index 89c34209..00000000 --- a/tests/Unit/Container/ContainerTest.php +++ /dev/null @@ -1,104 +0,0 @@ -instance('foo', $class); - $this->assertSame($class, $container->get('foo')); - } - - /** - * @covers \Engelsystem\Container\Container::get - * @expectedException \Engelsystem\Container\NotFoundException - */ - public function testGetException() - { - $container = new Container(); - - $container->get('not.registered.service'); - } - - /** - * @covers \Engelsystem\Container\Container::instance - * @covers \Engelsystem\Container\Container::resolve - */ - public function testInstance() - { - $container = new Container(); - $class = new class - { - }; - - $container->instance('foo', $class); - $this->assertSame($class, $container->get('foo')); - } - - /** - * @covers \Engelsystem\Container\Container::has - */ - public function testHas() - { - $container = new Container(); - - $this->assertFalse($container->has('test')); - - $class = new class - { - }; - - $container->instance('test', $class); - $this->assertTrue($container->has('test')); - } - - /** - * @covers \Engelsystem\Container\Container::singleton - */ - public function testSingleton() - { - $container = new Container(); - $class = new class - { - }; - - $container->singleton('foo', $class); - $this->assertSame($class, $container->get('foo')); - $this->assertSame($class, $container->get('foo')); - } - - /** - * @covers \Engelsystem\Container\Container::setInstance - * @covers \Engelsystem\Container\Container::getInstance - */ - public function testContainerSingleton() - { - // Ensure that no container has been initialized - $reflection = new \ReflectionProperty(Container::class, 'instance'); - $reflection->setAccessible(true); - $reflection->setValue(null, null); - $reflection->setAccessible(false); - - $container0 = new Container(); - $container = Container::getInstance(); - - $this->assertNotSame($container0, $container); - - $container1 = new Container; - Container::setInstance($container1); - - $this->assertSame($container1, Container::getInstance()); - } -} -- cgit v1.2.3-70-g09d2 From d49e49c364c1b73e4e4e3b52dc10ee9d0150e447 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 22 Sep 2017 14:02:02 +0200 Subject: Implemented service provider functionality --- config/app.php | 9 ++ includes/engelsystem_provider.php | 13 ++- includes/helper/internationalization_helper.php | 2 +- src/Application.php | 80 ++++++++++++- src/Container/ServiceProvider.php | 31 +++++ src/helpers.php | 50 +++++--- tests/Unit/ApplicationTest.php | 147 +++++++++++++++++++++++- 7 files changed, 310 insertions(+), 22 deletions(-) create mode 100644 config/app.php create mode 100644 src/Container/ServiceProvider.php (limited to 'tests/Unit') diff --git a/config/app.php b/config/app.php new file mode 100644 index 00000000..fe0a97c1 --- /dev/null +++ b/config/app.php @@ -0,0 +1,9 @@ + [ + ], +]; diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index e1669c57..3067ab62 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -26,6 +26,13 @@ require_once __DIR__ . '/autoload.php'; $app = new Application(realpath(__DIR__ . DIRECTORY_SEPARATOR . '..')); +/** + * Bootstrap application + */ +$appConfig = $app->make(Config::class); +$appConfig->set(app('path.config') . '/app.php'); +$app->bootstrap($appConfig); + /** * Load configuration */ @@ -40,6 +47,10 @@ if (file_exists(__DIR__ . '/../config/config.php')) { )); } + +/** + * Configure application + */ date_default_timezone_set($config->get('timezone')); @@ -55,7 +66,7 @@ $app->instance('request', $request); /** * Check for maintenance */ -if ($config->get('maintenance')) { +if ($app->get('config')->get('maintenance')) { echo file_get_contents(__DIR__ . '/../templates/maintenance.html'); die(); } diff --git a/includes/helper/internationalization_helper.php b/includes/helper/internationalization_helper.php index efbe5db5..7fa6518b 100644 --- a/includes/helper/internationalization_helper.php +++ b/includes/helper/internationalization_helper.php @@ -36,7 +36,7 @@ function gettext_init() } gettext_locale(); - bindtextdomain('default', realpath(__DIR__ . '/../../locale')); + bindtextdomain('default', app('path.lang')); bind_textdomain_codeset('default', 'UTF-8'); textdomain('default'); } diff --git a/src/Application.php b/src/Application.php index 80538396..b62b28a9 100644 --- a/src/Application.php +++ b/src/Application.php @@ -2,7 +2,9 @@ namespace Engelsystem; +use Engelsystem\Config\Config; use Engelsystem\Container\Container; +use Engelsystem\Container\ServiceProvider; use Psr\Container\ContainerInterface; class Application extends Container @@ -10,6 +12,16 @@ class Application extends Container /** @var string|null */ protected $appPath = null; + /** @var bool */ + protected $isBootstrapped = false; + + /** + * Registered service providers + * + * @var array + */ + protected $serviceProviders = []; + /** * Application constructor. * @@ -36,15 +48,73 @@ class Application extends Container } /** + * @param string|ServiceProvider $provider + * @return ServiceProvider + */ + public function register($provider) + { + if (is_string($provider)) { + $provider = $this->get($provider); + } + + $this->serviceProviders[] = $provider; + + $provider->register(); + + if ($this->isBootstrapped) { + $this->call([$provider, 'boot']); + } + + return $provider; + } + + /** + * Boot service providers + * + * @param Config|null $config + */ + public function bootstrap(Config $config = null) + { + if ($this->isBootstrapped) { + return; + } + + if ($config instanceof Config) { + foreach ($config->get('providers', []) as $provider) { + $this->register($provider); + } + } + + foreach ($this->serviceProviders as $provider) { + $this->call([$provider, 'boot']); + } + + $this->isBootstrapped = true; + } + + protected function registerPaths() + { + $appPath = $this->appPath; + + $this->instance('path', $appPath); + $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config'); + $this->instance('path.lang', $appPath . DIRECTORY_SEPARATOR . 'locale'); + } + + /** + * Set app base path + * * @param string $appPath * @return static */ public function setAppPath($appPath) { + $appPath = realpath($appPath); $appPath = rtrim($appPath, DIRECTORY_SEPARATOR); $this->appPath = $appPath; - $this->instance('path', $appPath); + + $this->registerPaths(); return $this; } @@ -56,4 +126,12 @@ class Application extends Container { return $this->appPath; } + + /** + * @return bool + */ + public function isBooted() + { + return $this->isBootstrapped; + } } diff --git a/src/Container/ServiceProvider.php b/src/Container/ServiceProvider.php new file mode 100644 index 00000000..2a1bbebf --- /dev/null +++ b/src/Container/ServiceProvider.php @@ -0,0 +1,31 @@ +app = $app; + } + + /** + * Register container bindings + */ + public function register() { } + + /** + * Called after other services had been registered + */ + public function boot() { } +} diff --git a/src/helpers.php b/src/helpers.php index de303963..c3c727ec 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -23,6 +23,15 @@ function app($id = null) return Application::getInstance()->get($id); } +/** + * @param string $path + * @return string + */ +function base_path($path = '') +{ + return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); +} + /** * Get or set config values * @@ -46,6 +55,15 @@ function config($key = null, $default = null) return $config->get($key, $default); } +/** + * @param string $path + * @return string + */ +function config_path($path = '') +{ + return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); +} + /** * @param string $key * @param mixed $default @@ -78,22 +96,6 @@ function session($key = null, $default = null) return $session->get($key, $default); } -/** - * @param string $template - * @param mixed[] $data - * @return Renderer|string - */ -function view($template = null, $data = null) -{ - $renderer = app('renderer'); - - if (is_null($template)) { - return $renderer; - } - - return $renderer->render($template, $data); -} - /** * @param string $path * @param array $parameters @@ -109,3 +111,19 @@ function url($path = null, $parameters = []) return $urlGenerator->to($path, $parameters); } + +/** + * @param string $template + * @param mixed[] $data + * @return Renderer|string + */ +function view($template = null, $data = null) +{ + $renderer = app('renderer'); + + if (is_null($template)) { + return $renderer; + } + + return $renderer->render($template, $data); +} diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php index 53fe3109..78310134 100644 --- a/tests/Unit/ApplicationTest.php +++ b/tests/Unit/ApplicationTest.php @@ -3,19 +3,23 @@ namespace Engelsystem\Test\Config; use Engelsystem\Application; +use Engelsystem\Config\Config; use Engelsystem\Container\Container; +use Engelsystem\Container\ServiceProvider; use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject; use Psr\Container\ContainerInterface; +use ReflectionClass; class ApplicationTest extends TestCase { /** - * @covers \Engelsystem\Application::__construct - * @covers \Engelsystem\Application::registerBaseBindings + * @covers \Engelsystem\Application::__construct + * @covers \Engelsystem\Application::registerBaseBindings */ public function testConstructor() { - $app = new Application(); + $app = new Application('.'); $this->assertInstanceOf(Container::class, $app); $this->assertInstanceOf(ContainerInterface::class, $app); @@ -27,4 +31,141 @@ class ApplicationTest extends TestCase $this->assertSame($app, Application::getInstance()); $this->assertSame($app, Container::getInstance()); } + + /** + * @covers \Engelsystem\Application::setAppPath + * @covers \Engelsystem\Application::registerPaths + * @covers \Engelsystem\Application::path + */ + public function testAppPath() + { + $app = new Application(); + + $this->assertFalse($app->has('path')); + + $app->setAppPath('.'); + $this->assertTrue($app->has('path')); + $this->assertTrue($app->has('path.config')); + $this->assertTrue($app->has('path.lang')); + + $this->assertEquals(realpath('.'), $app->path()); + $this->assertEquals(realpath('.') . '/config', $app->get('path.config')); + + $app->setAppPath('./../'); + $this->assertEquals(realpath('../') . '/config', $app->get('path.config')); + } + + /** + * @covers \Engelsystem\Application::register + */ + public function testRegister() + { + $app = new Application(); + + $serviceProvider = $this->mockServiceProvider($app, ['register']); + $serviceProvider->expects($this->once()) + ->method('register'); + + $app->register($serviceProvider); + + $anotherServiceProvider = $this->mockServiceProvider($app, ['register', 'boot']); + $anotherServiceProvider->expects($this->once()) + ->method('register'); + $anotherServiceProvider->expects($this->once()) + ->method('boot'); + + $app->bootstrap(); + $app->register($anotherServiceProvider); + } + + /** + * @covers \Engelsystem\Application::register + */ + public function testRegisterBoot() + { + $app = new Application(); + $app->bootstrap(); + + $serviceProvider = $this->mockServiceProvider($app, ['register', 'boot']); + $serviceProvider->expects($this->once()) + ->method('register'); + $serviceProvider->expects($this->once()) + ->method('boot'); + + $app->register($serviceProvider); + } + + /** + * @covers \Engelsystem\Application::register + */ + public function testRegisterClassName() + { + $app = new Application(); + + $mockClassName = $this->getMockClass(ServiceProvider::class); + $serviceProvider = $this->getMockBuilder($mockClassName) + ->setConstructorArgs([$app]) + ->setMethods(['register']) + ->getMock(); + + $serviceProvider->expects($this->once()) + ->method('register'); + + $app->instance($mockClassName, $serviceProvider); + $app->register($mockClassName); + } + + /** + * @covers \Engelsystem\Application::bootstrap + * @covers \Engelsystem\Application::isBooted + */ + public function testBootstrap() + { + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['register']) + ->getMock(); + + $serviceProvider = $this->mockServiceProvider($app, ['boot']); + $serviceProvider->expects($this->once()) + ->method('boot'); + + $app->expects($this->once()) + ->method('register') + ->with($serviceProvider); + + $config = $this->getMockBuilder(Config::class) + ->getMock(); + + $config->expects($this->once()) + ->method('get') + ->with('providers') + ->willReturn([$serviceProvider]); + + $property = (new ReflectionClass($app))->getProperty('serviceProviders'); + $property->setAccessible(true); + $property->setValue($app, [$serviceProvider]); + + $app->bootstrap($config); + + $this->assertTrue($app->isBooted()); + + // Run bootstrap another time to ensure that providers are registered only once + $app->bootstrap($config); + } + + /** + * @param Application $app + * @param array $methods + * @return PHPUnit_Framework_MockObject_MockObject|ServiceProvider + */ + protected function mockServiceProvider(Application $app, $methods = []) + { + $serviceProvider = $this->getMockBuilder(ServiceProvider::class) + ->setConstructorArgs([$app]) + ->setMethods($methods) + ->getMockForAbstractClass(); + + return $serviceProvider; + } } -- cgit v1.2.3-70-g09d2 From 449e2cdd00632acff63bb75c5282c3aa2642b59f Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 25 Sep 2017 00:03:22 +0200 Subject: Added env function, added GitLab CI code coverage config --- .gitlab-ci.yml | 47 ++++++++++++++++++++++++++++++++++++++++++++++ config/config.default.php | 8 ++++---- src/helpers.php | 15 +++++++++++++++ tests/Unit/HelpersTest.php | 14 ++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 .gitlab-ci.yml (limited to 'tests/Unit') diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 00000000..c44507b1 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,47 @@ +image: php + +cache: + paths: + - .composer + +services: + - mysql:5.6 + +variables: + MYSQL_DATABASE: engelsystem + MYSQL_USER: engel + MYSQL_PASSWORD: engelsystem + COMPOSER_HOME: .composer + MYSQL_RANDOM_ROOT_PASSWORD: "yes" + +before_script: + # Install required Packages + - apt-get update -yqq + - apt-get install -yqq git unzip mysql-client + - docker-php-ext-install pdo pdo_mysql gettext + # Install xdebug + - pecl install xdebug + - docker-php-ext-enable xdebug + # MySQL DB + - mysql -h mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/install.sql + - mysql -h mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" "$MYSQL_DATABASE" < db/update.sql + # Install Composer + - curl -sS https://getcomposer.org/installer | php -- --no-ansi --install-dir /usr/local/bin/ --filename composer + - /usr/local/bin/composer --no-ansi install + +.test_template: &test_definition + artifacts: + name: "${CI_JOB_NAME}_${CI_PROJECT_ID}_${PHP_VERSION}" + expire_in: 1 week + paths: + - ./coverage/ + coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/' + script: vendor/bin/phpunit --colors=never --coverage-text --coverage-html ./coverage/ + +test:7.0: + image: php:7.0 + <<: *test_definition + +test:7.1: + image: php:7.1 + <<: *test_definition diff --git a/config/config.default.php b/config/config.default.php index c2d742ef..1bad9668 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -5,10 +5,10 @@ return [ // MySQL-Connection Settings 'database' => [ - 'host' => 'localhost', - 'user' => 'root', - 'pw' => '', - 'db' => 'engelsystem', + 'host' => env('MYSQL_HOST', (env('CI', false) ? 'mysql' : 'localhost')), + 'user' => env('MYSQL_USER', 'root'), + 'pw' => env('MYSQL_PASSWORD', ''), + 'db' => env('MYSQL_DATABASE', 'engelsystem'), ], // For accessing stats diff --git a/src/helpers.php b/src/helpers.php index c3c727ec..5a48498a 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -64,6 +64,21 @@ function config_path($path = '') return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); } +/** + * @param string $key + * @param mixed $default + * @return mixed + */ +function env($key, $default = null) +{ + $value = getenv($key); + if ($value === false) { + return $default; + } + + return $value; +} + /** * @param string $key * @param mixed $default diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index d9782888..9ec824af 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -53,6 +53,20 @@ class HelpersTest extends TestCase $this->assertEquals(['user' => 'FooBar'], config('mail')); } + /** + * @covers \env + */ + public function testEnv() + { + putenv('envTestVar=someContent'); + + $env = env('envTestVar'); + $this->assertEquals('someContent', $env); + + $env = env('someRandomEnvVarThatShouldNeverExist', 'someDefaultValue'); + $this->assertEquals('someDefaultValue', $env); + } + /** * @covers \request */ -- cgit v1.2.3-70-g09d2 From 60fd72cd1a1e4e53b9af87e00a8c27687c6b5385 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 31 Oct 2017 13:40:13 +0100 Subject: Added service providers --- config/app.php | 6 ++ includes/autoload.php | 2 +- includes/engelsystem_provider.php | 95 ++++------------------ src/Application.php | 2 +- src/Config/ConfigServiceProvider.php | 26 ++++++ src/Database/DatabaseServiceProvider.php | 31 +++++++ src/Exceptions/ExceptionsServiceProvider.php | 15 ++++ src/Logger/LoggerServiceProvider.php | 18 ++++ src/Renderer/RendererServiceProvider.php | 36 ++++++++ src/Routing/RoutingServiceProvider.php | 14 ++++ .../DatabaseServiceProviderConnectionTest.php | 53 ++++++++++++ tests/Unit/Config/ConfigServiceProviderTest.php | 54 ++++++++++++ .../Unit/Database/DatabaseServiceProviderTest.php | 49 +++++++++++ .../Exceptions/ExceptionsServiceProviderTest.php | 39 +++++++++ tests/Unit/Logger/LoggerServiceProviderTest.php | 47 +++++++++++ .../Unit/Renderer/RendererServiceProviderTest.php | 81 ++++++++++++++++++ tests/Unit/Routing/RoutingServiceProviderTest.php | 39 +++++++++ tests/Unit/ServiceProviderTest.php | 39 +++++++++ tests/autoload.php | 8 ++ 19 files changed, 572 insertions(+), 82 deletions(-) create mode 100644 src/Config/ConfigServiceProvider.php create mode 100644 src/Database/DatabaseServiceProvider.php create mode 100644 src/Exceptions/ExceptionsServiceProvider.php create mode 100644 src/Logger/LoggerServiceProvider.php create mode 100644 src/Renderer/RendererServiceProvider.php create mode 100644 src/Routing/RoutingServiceProvider.php create mode 100644 tests/Feature/Database/DatabaseServiceProviderConnectionTest.php create mode 100644 tests/Unit/Config/ConfigServiceProviderTest.php create mode 100644 tests/Unit/Database/DatabaseServiceProviderTest.php create mode 100644 tests/Unit/Exceptions/ExceptionsServiceProviderTest.php create mode 100644 tests/Unit/Logger/LoggerServiceProviderTest.php create mode 100644 tests/Unit/Renderer/RendererServiceProviderTest.php create mode 100644 tests/Unit/Routing/RoutingServiceProviderTest.php create mode 100644 tests/Unit/ServiceProviderTest.php create mode 100644 tests/autoload.php (limited to 'tests/Unit') diff --git a/config/app.php b/config/app.php index fe0a97c1..8037479b 100644 --- a/config/app.php +++ b/config/app.php @@ -5,5 +5,11 @@ return [ // Service providers 'providers' => [ + \Engelsystem\Logger\LoggerServiceProvider::class, + \Engelsystem\Exceptions\ExceptionsServiceProvider::class, + \Engelsystem\Config\ConfigServiceProvider::class, + \Engelsystem\Routing\RoutingServiceProvider::class, + \Engelsystem\Renderer\RendererServiceProvider::class, + \Engelsystem\Database\DatabaseServiceProvider::class, ], ]; diff --git a/includes/autoload.php b/includes/autoload.php index f51f89e4..0cd9d355 100644 --- a/includes/autoload.php +++ b/includes/autoload.php @@ -6,4 +6,4 @@ if (!is_readable(__DIR__ . '/../vendor/autoload.php')) { } // Include composer autoloader -require_once __DIR__ . '/../vendor/autoload.php'; +$loader = require __DIR__ . '/../vendor/autoload.php'; diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 3067ab62..48206cb6 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -2,14 +2,8 @@ use Engelsystem\Application; use Engelsystem\Config\Config; -use Engelsystem\Database\Db; use Engelsystem\Exceptions\Handler as ExceptionHandler; use Engelsystem\Http\Request; -use Engelsystem\Logger\EngelsystemLogger; -use Engelsystem\Renderer\HtmlEngine; -use Engelsystem\Renderer\Renderer; -use Engelsystem\Routing\UrlGenerator; -use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; @@ -21,78 +15,21 @@ require_once __DIR__ . '/autoload.php'; /** - * Initialize the application + * Initialize and bootstrap the application */ $app = new Application(realpath(__DIR__ . DIRECTORY_SEPARATOR . '..')); - - -/** - * Bootstrap application - */ $appConfig = $app->make(Config::class); -$appConfig->set(app('path.config') . '/app.php'); +$appConfig->set(require config_path('app.php')); $app->bootstrap($appConfig); -/** - * Load configuration - */ -$config = new Config(); -$app->instance('config', $config); -$config->set(require __DIR__ . '/../config/config.default.php'); - -if (file_exists(__DIR__ . '/../config/config.php')) { - $config->set(array_replace_recursive( - $config->get(null), - require __DIR__ . '/../config/config.php' - )); -} - /** * Configure application */ -date_default_timezone_set($config->get('timezone')); - - -/** - * Initialize Request - * - * @var Request $request - */ -$request = Request::createFromGlobals(); -$app->instance('request', $request); - - -/** - * Check for maintenance - */ -if ($app->get('config')->get('maintenance')) { - echo file_get_contents(__DIR__ . '/../templates/maintenance.html'); - die(); -} - - -/** - * Register UrlGenerator - */ -$urlGenerator = new UrlGenerator(); -$app->instance('routing.urlGenerator', $urlGenerator); +date_default_timezone_set($app->get('config')->get('timezone')); - -/** - * Initialize renderer - */ -$renderer = new Renderer(); -$app->instance('renderer', $renderer); -$renderer->addRenderer(new HtmlEngine()); - - -/** - * Register error handler - */ -$errorHandler = new ExceptionHandler(); -$app->instance('error.handler', $errorHandler); if (config('environment') == 'development') { + $errorHandler = $app->get('error.handler'); $errorHandler->setEnvironment(ExceptionHandler::ENV_DEVELOPMENT); ini_set('display_errors', true); error_reporting(E_ALL); @@ -102,23 +39,21 @@ if (config('environment') == 'development') { /** - * Connect to database + * Check for maintenance */ -Db::connect( - 'mysql:host=' . config('database')['host'] . ';dbname=' . config('database')['db'] . ';charset=utf8', - config('database')['user'], - config('database')['pw'] -) || die('Error: Unable to connect to database'); -Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); -Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); +if ($app->get('config')->get('maintenance')) { + echo file_get_contents(__DIR__ . '/../templates/maintenance.html'); + die(); +} + /** - * Init logger + * Initialize Request + * + * @var Request $request */ -$logger = new EngelsystemLogger(); -$app->instance('logger', $logger); -$app->bind(LoggerInterface::class, 'logger'); -$app->bind(EngelsystemLogger::class, 'logger'); +$request = Request::createFromGlobals(); +$app->instance('request', $request); /** diff --git a/src/Application.php b/src/Application.php index b62b28a9..c9023c7b 100644 --- a/src/Application.php +++ b/src/Application.php @@ -54,7 +54,7 @@ class Application extends Container public function register($provider) { if (is_string($provider)) { - $provider = $this->get($provider); + $provider = $this->make($provider); } $this->serviceProviders[] = $provider; diff --git a/src/Config/ConfigServiceProvider.php b/src/Config/ConfigServiceProvider.php new file mode 100644 index 00000000..01b648df --- /dev/null +++ b/src/Config/ConfigServiceProvider.php @@ -0,0 +1,26 @@ +app->make(Config::class); + $this->app->instance('config', $config); + + $config->set(require $defaultConfigFile); + + if (file_exists($configFile)) { + $config->set(array_replace_recursive( + $config->get(null), + require $configFile + )); + } + } +} diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php new file mode 100644 index 00000000..364816cc --- /dev/null +++ b/src/Database/DatabaseServiceProvider.php @@ -0,0 +1,31 @@ +app->get('config'); + Db::connect( + 'mysql:host=' . $config->get('database')['host'] . ';dbname=' . $config->get('database')['db'] . ';charset=utf8', + $config->get('database')['user'], + $config->get('database')['pw'] + ) || $this->exitOnError(); + + Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + } + + /** + * @throws Exception + */ + protected function exitOnError() + { + throw new Exception('Error: Unable to connect to database'); + } +} diff --git a/src/Exceptions/ExceptionsServiceProvider.php b/src/Exceptions/ExceptionsServiceProvider.php new file mode 100644 index 00000000..7755e1e7 --- /dev/null +++ b/src/Exceptions/ExceptionsServiceProvider.php @@ -0,0 +1,15 @@ +app->make(ExceptionHandler::class); + $this->app->instance('error.handler', $errorHandler); + } +} diff --git a/src/Logger/LoggerServiceProvider.php b/src/Logger/LoggerServiceProvider.php new file mode 100644 index 00000000..cf22f383 --- /dev/null +++ b/src/Logger/LoggerServiceProvider.php @@ -0,0 +1,18 @@ +app->make(EngelsystemLogger::class); + $this->app->instance('logger', $logger); + + $this->app->bind(LoggerInterface::class, 'logger'); + $this->app->bind(EngelsystemLogger::class, 'logger'); + } +} diff --git a/src/Renderer/RendererServiceProvider.php b/src/Renderer/RendererServiceProvider.php new file mode 100644 index 00000000..3e8d69bc --- /dev/null +++ b/src/Renderer/RendererServiceProvider.php @@ -0,0 +1,36 @@ +registerRenderer(); + $this->registerHtmlEngine(); + } + + public function boot() + { + $renderer = $this->app->get('renderer'); + + foreach ($this->app->tagged('renderer.engine') as $engine) { + $renderer->addRenderer($engine); + } + } + + protected function registerRenderer() + { + $renderer = $this->app->make(Renderer::class); + $this->app->instance('renderer', $renderer); + } + + protected function registerHtmlEngine() + { + $htmlEngine = $this->app->make(HtmlEngine::class); + $this->app->instance('renderer.htmlEngine', $htmlEngine); + $this->app->tag('renderer.htmlEngine', ['renderer.engine']); + } +} diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php new file mode 100644 index 00000000..b7db1383 --- /dev/null +++ b/src/Routing/RoutingServiceProvider.php @@ -0,0 +1,14 @@ +app->make(UrlGenerator::class); + $this->app->instance('routing.urlGenerator', $urlGenerator); + } +} diff --git a/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php b/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php new file mode 100644 index 00000000..dd1ce729 --- /dev/null +++ b/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php @@ -0,0 +1,53 @@ +getMockBuilder(Config::class) + ->getMock(); + + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['get']) + ->getMock(); + Application::setInstance($app); + + $app->expects($this->once()) + ->method('get') + ->with('config') + ->willReturn($config); + + $config->expects($this->atLeastOnce()) + ->method('get') + ->with('database') + ->willReturn($this->getDbConfig()); + + $serviceProvider = new DatabaseServiceProvider($app); + $serviceProvider->register(); + } + + private function getDbConfig() + { + $configValues = require __DIR__ . '/../../../config/config.default.php'; + $configFile = __DIR__ . '/../../../config/config.php'; + + if (file_exists($configFile)) { + $configValues = array_replace_recursive($configValues, require $configFile); + } + + return $configValues['database']; + } +} diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php new file mode 100644 index 00000000..26128e79 --- /dev/null +++ b/tests/Unit/Config/ConfigServiceProviderTest.php @@ -0,0 +1,54 @@ +getMockBuilder(Config::class) + ->getMock(); + + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['make', 'instance', 'get']) + ->getMock(); + Application::setInstance($app); + + $app->expects($this->once()) + ->method('make') + ->with(Config::class) + ->willReturn($config); + + $app->expects($this->once()) + ->method('instance') + ->with('config', $config); + + $app->expects($this->atLeastOnce()) + ->method('get') + ->with('path.config') + ->willReturn(__DIR__ . '/../../../config'); + + $config->expects($this->exactly(2)) + ->method('set') + ->withAnyParameters(); + + $config->expects($this->once()) + ->method('get') + ->with(null) + ->willReturn([]); + + $serviceProvider = new ConfigServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php new file mode 100644 index 00000000..d0e3e164 --- /dev/null +++ b/tests/Unit/Database/DatabaseServiceProviderTest.php @@ -0,0 +1,49 @@ +getMockBuilder(Config::class) + ->getMock(); + + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['get']) + ->getMock(); + + $app->expects($this->once()) + ->method('get') + ->with('config') + ->willReturn($config); + + $config->expects($this->atLeastOnce()) + ->method('get') + ->with('database') + ->willReturn([ + 'host' => 'localhost', + 'db' => 'database', + 'user' => 'user', + 'pw' => 'password', + ]); + + $serviceProvider = new DatabaseServiceProvider($app); + $this->expectException(Exception::class); + + $serviceProvider->register(); + } +} diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php new file mode 100644 index 00000000..26eddb75 --- /dev/null +++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php @@ -0,0 +1,39 @@ +getMockBuilder(ExceptionHandler::class) + ->getMock(); + + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['make', 'instance']) + ->getMock(); + + $app->expects($this->once()) + ->method('make') + ->with(ExceptionHandler::class) + ->willReturn($exceptionHandler); + + $app->expects($this->once()) + ->method('instance') + ->with('error.handler', $exceptionHandler); + + $serviceProvider = new ExceptionsServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php new file mode 100644 index 00000000..5143d236 --- /dev/null +++ b/tests/Unit/Logger/LoggerServiceProviderTest.php @@ -0,0 +1,47 @@ +getMockBuilder(EngelsystemLogger::class) + ->getMock(); + + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['make', 'instance', 'bind']) + ->getMock(); + + $app->expects($this->once()) + ->method('make') + ->with(EngelsystemLogger::class) + ->willReturn($logger); + + $app->expects($this->once()) + ->method('instance') + ->with('logger', $logger); + + $app->expects($this->atLeastOnce()) + ->method('bind') + ->withConsecutive( + [LoggerInterface::class, 'logger'], + [EngelsystemLogger::class, 'logger'] + ); + + $serviceProvider = new LoggerServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Unit/Renderer/RendererServiceProviderTest.php b/tests/Unit/Renderer/RendererServiceProviderTest.php new file mode 100644 index 00000000..f9044d8b --- /dev/null +++ b/tests/Unit/Renderer/RendererServiceProviderTest.php @@ -0,0 +1,81 @@ +getMockBuilder(Renderer::class) + ->getMock(); + /** @var PHPUnit_Framework_MockObject_MockObject|HtmlEngine $htmlEngine */ + $htmlEngine = $this->getMockBuilder(HtmlEngine::class) + ->getMock(); + + $app = $this->getApp(['make', 'instance', 'tag']); + + $app->expects($this->exactly(2)) + ->method('make') + ->withConsecutive( + [Renderer::class], + [HtmlEngine::class] + )->willReturnOnConsecutiveCalls( + $renderer, + $htmlEngine + ); + + $app->expects($this->exactly(2)) + ->method('instance') + ->withConsecutive( + ['renderer', $renderer], + ['renderer.htmlEngine', $htmlEngine] + ); + + $this->setExpects($app, 'tag', ['renderer.htmlEngine', ['renderer.engine']]); + + $serviceProvider = new RendererServiceProvider($app); + $serviceProvider->register(); + } + + /** + * @covers \Engelsystem\Renderer\RendererServiceProvider::boot() + */ + public function testBoot() + { + /** @var PHPUnit_Framework_MockObject_MockObject|Renderer $renderer */ + $renderer = $this->getMockBuilder(Renderer::class) + ->getMock(); + /** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine1 */ + $engine1 = $this->getMockForAbstractClass(EngineInterface::class); + /** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine2 */ + $engine2 = $this->getMockForAbstractClass(EngineInterface::class); + + $app = $this->getApp(['get', 'tagged']); + + $engines = [$engine1, $engine2]; + + $this->setExpects($app, 'get', ['renderer'], $renderer); + $this->setExpects($app, 'tagged', ['renderer.engine'], $engines); + + $invocation = $renderer + ->expects($this->exactly(count($engines))) + ->method('addRenderer'); + call_user_func_array([$invocation, 'withConsecutive'], $engines); + + $serviceProvider = new RendererServiceProvider($app); + $serviceProvider->boot(); + } +} diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php new file mode 100644 index 00000000..4f1cd5fc --- /dev/null +++ b/tests/Unit/Routing/RoutingServiceProviderTest.php @@ -0,0 +1,39 @@ +getMockBuilder(UrlGenerator::class) + ->getMock(); + + /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['make', 'instance']) + ->getMock(); + + $app->expects($this->once()) + ->method('make') + ->with(UrlGenerator::class) + ->willReturn($urlGenerator); + + $app->expects($this->once()) + ->method('instance') + ->with('routing.urlGenerator', $urlGenerator); + + $serviceProvider = new RoutingServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Unit/ServiceProviderTest.php b/tests/Unit/ServiceProviderTest.php new file mode 100644 index 00000000..be843742 --- /dev/null +++ b/tests/Unit/ServiceProviderTest.php @@ -0,0 +1,39 @@ +getMockBuilder(Application::class) + ->setMethods($methods) + ->getMock(); + } + + /** + * @param PHPUnit_Framework_MockObject_MockObject $object + * @param string $method + * @param array $arguments + * @param mixed $return + */ + protected function setExpects($object, $method, $arguments, $return = null) + { + $invocation = $object->expects($this->once()) + ->method($method); + call_user_func_array([$invocation, 'with'], $arguments); + + if (!is_null($return)) { + $invocation->willReturn($return); + } + } +} diff --git a/tests/autoload.php b/tests/autoload.php new file mode 100644 index 00000000..3168ce3d --- /dev/null +++ b/tests/autoload.php @@ -0,0 +1,8 @@ +addPsr4('Engelsystem\\Test\\', __DIR__ . '/'); -- cgit v1.2.3-70-g09d2 From 411ea5bb6d0ecf32e6c989a99fac120502db9fe9 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 31 Oct 2017 14:15:19 +0100 Subject: Refactored service provider tests --- tests/Unit/Config/ConfigServiceProviderTest.php | 35 +++++----------------- .../Unit/Database/DatabaseServiceProviderTest.php | 34 +++++++-------------- .../Exceptions/ExceptionsServiceProviderTest.php | 20 ++++--------- tests/Unit/Logger/LoggerServiceProviderTest.php | 20 ++++--------- tests/Unit/Routing/RoutingServiceProviderTest.php | 20 ++++--------- tests/Unit/ServiceProviderTest.php | 31 ++++++++++++------- 6 files changed, 55 insertions(+), 105 deletions(-) (limited to 'tests/Unit') diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php index 26128e79..96ac8704 100644 --- a/tests/Unit/Config/ConfigServiceProviderTest.php +++ b/tests/Unit/Config/ConfigServiceProviderTest.php @@ -5,10 +5,10 @@ namespace Engelsystem\Test\Config; use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Config\ConfigServiceProvider; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; -class ConfigServiceProviderTest extends TestCase +class ConfigServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Config\ConfigServiceProvider::register() @@ -19,34 +19,15 @@ class ConfigServiceProviderTest extends TestCase $config = $this->getMockBuilder(Config::class) ->getMock(); - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ - $app = $this->getMockBuilder(Application::class) - ->setMethods(['make', 'instance', 'get']) - ->getMock(); + $app = $this->getApp(['make', 'instance', 'get']); Application::setInstance($app); - $app->expects($this->once()) - ->method('make') - ->with(Config::class) - ->willReturn($config); - - $app->expects($this->once()) - ->method('instance') - ->with('config', $config); - - $app->expects($this->atLeastOnce()) - ->method('get') - ->with('path.config') - ->willReturn(__DIR__ . '/../../../config'); - - $config->expects($this->exactly(2)) - ->method('set') - ->withAnyParameters(); + $this->setExpects($app, 'make', [Config::class], $config); + $this->setExpects($app, 'instance', ['config', $config]); + $this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce()); - $config->expects($this->once()) - ->method('get') - ->with(null) - ->willReturn([]); + $this->setExpects($config, 'set', null, null, $this->exactly(2)); + $this->setExpects($config, 'get', [null], []); $serviceProvider = new ConfigServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php index d0e3e164..d61f5ae7 100644 --- a/tests/Unit/Database/DatabaseServiceProviderTest.php +++ b/tests/Unit/Database/DatabaseServiceProviderTest.php @@ -2,14 +2,13 @@ namespace Engelsystem\Test\Database; -use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Database\DatabaseServiceProvider; +use Engelsystem\Test\Unit\ServiceProviderTest; use Exception; -use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject; -class DatabaseServiceProviderTest extends TestCase +class DatabaseServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Database\DatabaseServiceProvider::register() @@ -21,29 +20,18 @@ class DatabaseServiceProviderTest extends TestCase $config = $this->getMockBuilder(Config::class) ->getMock(); - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ - $app = $this->getMockBuilder(Application::class) - ->setMethods(['get']) - ->getMock(); - - $app->expects($this->once()) - ->method('get') - ->with('config') - ->willReturn($config); - - $config->expects($this->atLeastOnce()) - ->method('get') - ->with('database') - ->willReturn([ - 'host' => 'localhost', - 'db' => 'database', - 'user' => 'user', - 'pw' => 'password', - ]); + $app = $this->getApp(['get']); - $serviceProvider = new DatabaseServiceProvider($app); + $this->setExpects($app, 'get', ['config'], $config); + $this->setExpects($config, 'get', ['database'], [ + 'host' => 'localhost', + 'db' => 'database', + 'user' => 'user', + 'pw' => 'password', + ], $this->atLeastOnce()); $this->expectException(Exception::class); + $serviceProvider = new DatabaseServiceProvider($app); $serviceProvider->register(); } } diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php index 26eddb75..fc48d39e 100644 --- a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php +++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php @@ -2,13 +2,12 @@ namespace Engelsystem\Test\Exceptions; -use Engelsystem\Application; use Engelsystem\Exceptions\ExceptionsServiceProvider; use Engelsystem\Exceptions\Handler as ExceptionHandler; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; -class ExceptionsServiceProviderTest extends TestCase +class ExceptionsServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register() @@ -19,19 +18,10 @@ class ExceptionsServiceProviderTest extends TestCase $exceptionHandler = $this->getMockBuilder(ExceptionHandler::class) ->getMock(); - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ - $app = $this->getMockBuilder(Application::class) - ->setMethods(['make', 'instance']) - ->getMock(); - - $app->expects($this->once()) - ->method('make') - ->with(ExceptionHandler::class) - ->willReturn($exceptionHandler); + $app = $this->getApp(); - $app->expects($this->once()) - ->method('instance') - ->with('error.handler', $exceptionHandler); + $this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler); + $this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]); $serviceProvider = new ExceptionsServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php index 5143d236..66f63cf4 100644 --- a/tests/Unit/Logger/LoggerServiceProviderTest.php +++ b/tests/Unit/Logger/LoggerServiceProviderTest.php @@ -2,14 +2,13 @@ namespace Engelsystem\Test\Logger; -use Engelsystem\Application; use Engelsystem\Logger\EngelsystemLogger; use Engelsystem\Logger\LoggerServiceProvider; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; use Psr\Log\LoggerInterface; -class LoggerServiceProviderTest extends TestCase +class LoggerServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Logger\LoggerServiceProvider::register() @@ -20,19 +19,10 @@ class LoggerServiceProviderTest extends TestCase $logger = $this->getMockBuilder(EngelsystemLogger::class) ->getMock(); - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ - $app = $this->getMockBuilder(Application::class) - ->setMethods(['make', 'instance', 'bind']) - ->getMock(); - - $app->expects($this->once()) - ->method('make') - ->with(EngelsystemLogger::class) - ->willReturn($logger); + $app = $this->getApp(['make', 'instance', 'bind']); - $app->expects($this->once()) - ->method('instance') - ->with('logger', $logger); + $this->setExpects($app, 'make', [EngelsystemLogger::class], $logger); + $this->setExpects($app, 'instance', ['logger', $logger]); $app->expects($this->atLeastOnce()) ->method('bind') diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php index 4f1cd5fc..bb2a1d65 100644 --- a/tests/Unit/Routing/RoutingServiceProviderTest.php +++ b/tests/Unit/Routing/RoutingServiceProviderTest.php @@ -2,13 +2,12 @@ namespace Engelsystem\Test\Routing; -use Engelsystem\Application; use Engelsystem\Routing\RoutingServiceProvider; use Engelsystem\Routing\UrlGenerator; -use PHPUnit\Framework\TestCase; +use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; -class RoutingServiceProviderTest extends TestCase +class RoutingServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Routing\RoutingServiceProvider::register() @@ -19,19 +18,10 @@ class RoutingServiceProviderTest extends TestCase $urlGenerator = $this->getMockBuilder(UrlGenerator::class) ->getMock(); - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ - $app = $this->getMockBuilder(Application::class) - ->setMethods(['make', 'instance']) - ->getMock(); - - $app->expects($this->once()) - ->method('make') - ->with(UrlGenerator::class) - ->willReturn($urlGenerator); + $app = $this->getApp(); - $app->expects($this->once()) - ->method('instance') - ->with('routing.urlGenerator', $urlGenerator); + $this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator); + $this->setExpects($app, 'instance', ['routing.urlGenerator', $urlGenerator]); $serviceProvider = new RoutingServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/ServiceProviderTest.php b/tests/Unit/ServiceProviderTest.php index be843742..dc58a65e 100644 --- a/tests/Unit/ServiceProviderTest.php +++ b/tests/Unit/ServiceProviderTest.php @@ -4,33 +4,44 @@ namespace Engelsystem\Test\Unit; use Engelsystem\Application; use PHPUnit\Framework\TestCase; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_MockObject_Matcher_InvokedRecorder as InvokedRecorder; +use PHPUnit_Framework_MockObject_MockObject as MockObject; abstract class ServiceProviderTest extends TestCase { /** * @param array $methods - * @return Application|PHPUnit_Framework_MockObject_MockObject + * @return Application|MockObject */ protected function getApp($methods = ['make', 'instance']) { - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ + /** @var MockObject|Application $app */ return $this->getMockBuilder(Application::class) ->setMethods($methods) ->getMock(); } /** - * @param PHPUnit_Framework_MockObject_MockObject $object - * @param string $method - * @param array $arguments - * @param mixed $return + * @param MockObject $object + * @param string $method + * @param array $arguments + * @param mixed $return + * @param InvokedRecorder $times */ - protected function setExpects($object, $method, $arguments, $return = null) + protected function setExpects($object, $method, $arguments = null, $return = null, $times = null) { - $invocation = $object->expects($this->once()) + if (is_null($times)) { + $times = $this->once(); + } + + $invocation = $object->expects($times) ->method($method); - call_user_func_array([$invocation, 'with'], $arguments); + + if (is_null($arguments)) { + $invocation->withAnyParameters(); + } else { + call_user_func_array([$invocation, 'with'], $arguments); + } if (!is_null($return)) { $invocation->willReturn($return); -- cgit v1.2.3-70-g09d2 From e15e86362585f5d00d118653232584ed0920e533 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 31 Oct 2017 14:23:23 +0100 Subject: Added tests for base_path and config_path --- .../DatabaseServiceProviderConnectionTest.php | 2 +- tests/Feature/Logger/EngelsystemLoggerTest.php | 2 +- tests/Feature/Model/LogEntriesModelTest.php | 38 +++++++++++++++++++ tests/Feature/Model/RoomModelTest.php | 40 ++++++++++++++++++++ tests/Feature/model/LogEntriesModelTest.php | 38 ------------------- tests/Feature/model/RoomModelTest.php | 40 -------------------- tests/Unit/ApplicationTest.php | 2 +- tests/Unit/Config/ConfigServiceProviderTest.php | 2 +- tests/Unit/Config/ConfigTest.php | 2 +- .../Unit/Database/DatabaseServiceProviderTest.php | 2 +- .../Exceptions/ExceptionsServiceProviderTest.php | 2 +- tests/Unit/HelpersTest.php | 43 +++++++++++++++++++++- tests/Unit/Logger/LoggerServiceProviderTest.php | 2 +- tests/Unit/Renderer/HtmlEngineTest.php | 2 +- .../Unit/Renderer/RendererServiceProviderTest.php | 2 +- tests/Unit/Renderer/RendererTest.php | 6 ++- tests/Unit/Routing/RoutingServiceProviderTest.php | 2 +- tests/Unit/Routing/UrlGeneratorTest.php | 2 +- 18 files changed, 136 insertions(+), 93 deletions(-) create mode 100644 tests/Feature/Model/LogEntriesModelTest.php create mode 100644 tests/Feature/Model/RoomModelTest.php delete mode 100644 tests/Feature/model/LogEntriesModelTest.php delete mode 100644 tests/Feature/model/RoomModelTest.php (limited to 'tests/Unit') diff --git a/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php b/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php index dd1ce729..636fba2e 100644 --- a/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php +++ b/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php @@ -1,6 +1,6 @@ assertNotFalse(LogEntry_create(LogLevel::WARNING, 'test_LogEntry_create')); + + // There should be one more log entry now + $this->assertEquals(count(LogEntries()), $count + 1); + } + + public function testClearAllLogEntries() + { + LogEntry_create(LogLevel::WARNING, 'test'); + $this->assertTrue(count(LogEntries()) > 0); + + $this->assertNotFalse(LogEntries_clear_all()); + $this->assertCount(0, LogEntries()); + } + + public function tearDown() + { + LogEntries_clear_all(); + } +} diff --git a/tests/Feature/Model/RoomModelTest.php b/tests/Feature/Model/RoomModelTest.php new file mode 100644 index 00000000..20b9e34d --- /dev/null +++ b/tests/Feature/Model/RoomModelTest.php @@ -0,0 +1,40 @@ +room_id = Room_create('test', false, true, ''); + } + + public function test_Room() + { + $this->create_Room(); + + $room = Room($this->room_id); + + $this->assertNotFalse($room); + $this->assertNotNull($room); + $this->assertEquals($room['Name'], 'test'); + + $this->assertNull(Room(-1)); + } + + public function tearDown() + { + if ($this->room_id != null) { + Room_delete($this->room_id); + } + } +} diff --git a/tests/Feature/model/LogEntriesModelTest.php b/tests/Feature/model/LogEntriesModelTest.php deleted file mode 100644 index 6d7b0ebc..00000000 --- a/tests/Feature/model/LogEntriesModelTest.php +++ /dev/null @@ -1,38 +0,0 @@ -assertNotFalse(LogEntry_create(LogLevel::WARNING, 'test_LogEntry_create')); - - // There should be one more log entry now - $this->assertEquals(count(LogEntries()), $count + 1); - } - - public function testClearAllLogEntries() - { - LogEntry_create(LogLevel::WARNING, 'test'); - $this->assertTrue(count(LogEntries()) > 0); - - $this->assertNotFalse(LogEntries_clear_all()); - $this->assertCount(0, LogEntries()); - } - - public function tearDown() - { - LogEntries_clear_all(); - } -} diff --git a/tests/Feature/model/RoomModelTest.php b/tests/Feature/model/RoomModelTest.php deleted file mode 100644 index 96be84a2..00000000 --- a/tests/Feature/model/RoomModelTest.php +++ /dev/null @@ -1,40 +0,0 @@ -room_id = Room_create('test', false, true, ''); - } - - public function test_Room() - { - $this->create_Room(); - - $room = Room($this->room_id); - - $this->assertNotFalse($room); - $this->assertNotNull($room); - $this->assertEquals($room['Name'], 'test'); - - $this->assertNull(Room(-1)); - } - - public function tearDown() - { - if ($this->room_id != null) { - Room_delete($this->room_id); - } - } -} diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php index 78310134..f58483ea 100644 --- a/tests/Unit/ApplicationTest.php +++ b/tests/Unit/ApplicationTest.php @@ -1,6 +1,6 @@ assertEquals($class, app('some.name')); } + /** + * @covers \base_path() + */ + public function testBasePath() + { + /** @var MockObject|Application $app */ + $app = $this->getMockBuilder(Container::class) + ->getMock(); + Application::setInstance($app); + + $app->expects($this->atLeastOnce()) + ->method('get') + ->with('path') + ->willReturn('/foo/bar'); + + $this->assertEquals('/foo/bar', base_path()); + $this->assertEquals('/foo/bar/bla-foo.conf', base_path('bla-foo.conf')); + } + /** * @covers \config */ @@ -53,6 +73,25 @@ class HelpersTest extends TestCase $this->assertEquals(['user' => 'FooBar'], config('mail')); } + /** + * @covers \config_path() + */ + public function testConfigPath() + { + /** @var MockObject|Application $app */ + $app = $this->getMockBuilder(Container::class) + ->getMock(); + Application::setInstance($app); + + $app->expects($this->atLeastOnce()) + ->method('get') + ->with('path.config') + ->willReturn('/foo/conf'); + + $this->assertEquals('/foo/conf', config_path()); + $this->assertEquals('/foo/conf/bar.php', config_path('bar.php')); + } + /** * @covers \env */ @@ -146,7 +185,7 @@ class HelpersTest extends TestCase /** * @param string $alias * @param object $object - * @return Application|\PHPUnit_Framework_MockObject_MockObject + * @return Application|MockObject */ protected function getAppMock($alias, $object) { diff --git a/tests/Unit/Logger/LoggerServiceProviderTest.php b/tests/Unit/Logger/LoggerServiceProviderTest.php index 66f63cf4..cef95d5b 100644 --- a/tests/Unit/Logger/LoggerServiceProviderTest.php +++ b/tests/Unit/Logger/LoggerServiceProviderTest.php @@ -1,6 +1,6 @@ getMockForAbstractClass(EngineInterface::class); $nullRenderer->expects($this->atLeastOnce()) @@ -20,6 +22,7 @@ class RendererTest extends TestCase ->willReturn(false); $renderer->addRenderer($nullRenderer); + /** @var MockObject|EngineInterface $mockRenderer */ $mockRenderer = $this->getMockForAbstractClass(EngineInterface::class); $mockRenderer->expects($this->atLeastOnce()) @@ -42,6 +45,7 @@ class RendererTest extends TestCase { $renderer = new Renderer(); + /** @var MockObject|LoggerInterface $loggerMock */ $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); $loggerMock ->expects($this->once()) diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php index bb2a1d65..dd9441eb 100644 --- a/tests/Unit/Routing/RoutingServiceProviderTest.php +++ b/tests/Unit/Routing/RoutingServiceProviderTest.php @@ -1,6 +1,6 @@ Date: Tue, 31 Oct 2017 14:48:29 +0100 Subject: Added Container\ServiceProvider test --- tests/Unit/Container/ServiceProviderTest.php | 22 ++++++++++++++++++++++ .../Stub/ServiceProviderImplementation.php | 10 ++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/Unit/Container/ServiceProviderTest.php create mode 100644 tests/Unit/Container/Stub/ServiceProviderImplementation.php (limited to 'tests/Unit') diff --git a/tests/Unit/Container/ServiceProviderTest.php b/tests/Unit/Container/ServiceProviderTest.php new file mode 100644 index 00000000..8a9cb76e --- /dev/null +++ b/tests/Unit/Container/ServiceProviderTest.php @@ -0,0 +1,22 @@ +getApp(); + + $serviceProvider = new ServiceProviderImplementation($app); + + $this->assertInstanceOf(ServiceProvider::class, $serviceProvider); + } +} diff --git a/tests/Unit/Container/Stub/ServiceProviderImplementation.php b/tests/Unit/Container/Stub/ServiceProviderImplementation.php new file mode 100644 index 00000000..36ae2c38 --- /dev/null +++ b/tests/Unit/Container/Stub/ServiceProviderImplementation.php @@ -0,0 +1,10 @@ + Date: Tue, 31 Oct 2017 15:13:37 +0100 Subject: Fixed ConfigServiceProviderTest when no config file has been created --- tests/Unit/Config/ConfigServiceProviderTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests/Unit') diff --git a/tests/Unit/Config/ConfigServiceProviderTest.php b/tests/Unit/Config/ConfigServiceProviderTest.php index ab6fa67f..c8be4b7d 100644 --- a/tests/Unit/Config/ConfigServiceProviderTest.php +++ b/tests/Unit/Config/ConfigServiceProviderTest.php @@ -29,7 +29,17 @@ class ConfigServiceProviderTest extends ServiceProviderTest $this->setExpects($config, 'set', null, null, $this->exactly(2)); $this->setExpects($config, 'get', [null], []); + $configFile = __DIR__ . '/../../../config/config.php'; + $configExists = file_exists($configFile); + if (!$configExists) { + file_put_contents($configFile, 'register(); + + if (!$configExists) { + unlink($configFile); + } } } -- cgit v1.2.3-70-g09d2 From 8c703e12029c2170ced5cafe4c91e9fd5f8414d9 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 31 Oct 2017 17:17:32 +0100 Subject: Added request test --- tests/Unit/Http/RequestTest.php | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 tests/Unit/Http/RequestTest.php (limited to 'tests/Unit') diff --git a/tests/Unit/Http/RequestTest.php b/tests/Unit/Http/RequestTest.php new file mode 100644 index 00000000..3f317367 --- /dev/null +++ b/tests/Unit/Http/RequestTest.php @@ -0,0 +1,98 @@ + 'I\'m a test!'], + ['foo' => 'bar'] + ); + + $this->assertEquals('bar', $request->postData('foo')); + $this->assertEquals('LoremIpsum', $request->postData('test-key', 'LoremIpsum')); + } + + /** + * @covers \Engelsystem\Http\Request::input + */ + public function testInput() + { + $request = new Request( + ['foo' => 'I\'m a test!'], + ['foo' => 'bar'] + ); + + $this->assertEquals('I\'m a test!', $request->input('foo')); + $this->assertEquals('LoremIpsum', $request->input('test-key', 'LoremIpsum')); + } + + /** + * @covers \Engelsystem\Http\Request::has + */ + public function testHas() + { + $request = new Request([ + 'foo' => 'I\'m a test!', + 'bar' => '', + ]); + + $this->assertTrue($request->has('foo')); + $this->assertFalse($request->has('bar')); + } + + /** + * @covers \Engelsystem\Http\Request::path + */ + public function testPath() + { + /** @var MockObject|Request $request */ + $request = $this + ->getMockBuilder(Request::class) + ->setMethods(['getPathInfo']) + ->getMock(); + + $request + ->expects($this->atLeastOnce()) + ->method('getPathInfo') + ->willReturnOnConsecutiveCalls( + '/foo', + '/' + ); + + $this->assertEquals('foo', $request->path()); + $this->assertEquals('/', $request->path()); + } + + /** + * @covers \Engelsystem\Http\Request::url + */ + public function testUrl() + { + /** @var MockObject|Request $request */ + $request = $this + ->getMockBuilder(Request::class) + ->setMethods(['getUri']) + ->getMock(); + + $request + ->expects($this->atLeastOnce()) + ->method('getUri') + ->willReturnOnConsecutiveCalls( + 'http://foo.bar/bla/foo/', + 'https://lorem.ipsum/dolor/sit?amet=consetetur&sadipscing=elitr' + ); + + $this->assertEquals('http://foo.bar/bla/foo', $request->url()); + $this->assertEquals('https://lorem.ipsum/dolor/sit', $request->url()); + } +} -- cgit v1.2.3-70-g09d2 From e55d5c7c15411eb58dd99113d94ee8ba55fd414a Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 31 Oct 2017 19:26:48 +0100 Subject: Added database tests --- .../DatabaseServiceProviderConnectionTest.php | 53 ------ .../Database/DatabaseServiceProviderTest.php | 40 +++++ tests/Feature/Database/DatabaseTest.php | 25 +++ tests/Unit/Database/DbTest.php | 192 +++++++++++++++++++++ 4 files changed, 257 insertions(+), 53 deletions(-) delete mode 100644 tests/Feature/Database/DatabaseServiceProviderConnectionTest.php create mode 100644 tests/Feature/Database/DatabaseServiceProviderTest.php create mode 100644 tests/Feature/Database/DatabaseTest.php create mode 100644 tests/Unit/Database/DbTest.php (limited to 'tests/Unit') diff --git a/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php b/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php deleted file mode 100644 index 636fba2e..00000000 --- a/tests/Feature/Database/DatabaseServiceProviderConnectionTest.php +++ /dev/null @@ -1,53 +0,0 @@ -getMockBuilder(Config::class) - ->getMock(); - - /** @var PHPUnit_Framework_MockObject_MockObject|Application $app */ - $app = $this->getMockBuilder(Application::class) - ->setMethods(['get']) - ->getMock(); - Application::setInstance($app); - - $app->expects($this->once()) - ->method('get') - ->with('config') - ->willReturn($config); - - $config->expects($this->atLeastOnce()) - ->method('get') - ->with('database') - ->willReturn($this->getDbConfig()); - - $serviceProvider = new DatabaseServiceProvider($app); - $serviceProvider->register(); - } - - private function getDbConfig() - { - $configValues = require __DIR__ . '/../../../config/config.default.php'; - $configFile = __DIR__ . '/../../../config/config.php'; - - if (file_exists($configFile)) { - $configValues = array_replace_recursive($configValues, require $configFile); - } - - return $configValues['database']; - } -} diff --git a/tests/Feature/Database/DatabaseServiceProviderTest.php b/tests/Feature/Database/DatabaseServiceProviderTest.php new file mode 100644 index 00000000..d5fdd108 --- /dev/null +++ b/tests/Feature/Database/DatabaseServiceProviderTest.php @@ -0,0 +1,40 @@ +getMockBuilder(Config::class) + ->getMock(); + + /** @var MockObject|Application $app */ + $app = $this->getMockBuilder(Application::class) + ->setMethods(['get']) + ->getMock(); + Application::setInstance($app); + + $app->expects($this->once()) + ->method('get') + ->with('config') + ->willReturn($config); + + $config->expects($this->atLeastOnce()) + ->method('get') + ->with('database') + ->willReturn($this->getDbConfig()); + + $serviceProvider = new DatabaseServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Feature/Database/DatabaseTest.php b/tests/Feature/Database/DatabaseTest.php new file mode 100644 index 00000000..11df6779 --- /dev/null +++ b/tests/Feature/Database/DatabaseTest.php @@ -0,0 +1,25 @@ +assertFalse($result); + + $result = Db::connect('sqlite::memory'); + $this->assertTrue($result); + } + + /** + * @covers \Engelsystem\Database\Db::query() + */ + public function testQuery() + { + $stm = Db::query('SELECT * FROM test_data'); + $this->assertEquals('00000', $stm->errorCode()); + + $stm = Db::query('SELECT * FROM test_data WHERE id = ?', [4]); + $this->assertEquals('00000', $stm->errorCode()); + } + + /** + * @covers \Engelsystem\Database\Db::unprepared() + */ + public function testUnprepared() + { + $return = Db::unprepared('SELECT * FROM test_data WHERE id = 3'); + $this->assertTrue($return); + } + + /** + * @covers \Engelsystem\Database\Db::select() + */ + public function testSelect() + { + $return = Db::select('SELECT * FROM test_data'); + $this->assertTrue(count($return) > 3); + + $return = Db::select('SELECT * FROM test_data WHERE id = ?', [2]); + $this->assertCount(1, $return); + } + + /** + * @covers \Engelsystem\Database\Db::selectOne() + */ + public function testSelectOne() + { + $return = Db::selectOne('SELECT * FROM test_data'); + $this->assertEquals('Foo', $return['data']); + + $return = Db::selectOne('SELECT * FROM test_data WHERE id = -1'); + $this->assertEmpty($return); + + $return = Db::selectOne('SELECT * FROM test_data WHERE id = ?', [3]); + $return = array_pop($return); + $this->assertTrue(!is_array($return)); + } + + /** + * @covers \Engelsystem\Database\Db::insert() + */ + public function testInsert() + { + $count = Db::insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')"); + $this->assertEquals(2, $count); + + $count = Db::insert('INSERT INTO test_data(id, data) VALUES (:id, :alias)', ['id' => 7, 'alias' => 'Blafoo']); + $this->assertEquals(1, $count); + } + + /** + * @covers \Engelsystem\Database\Db::update() + */ + public function testUpdate() + { + $count = Db::update("UPDATE test_data SET data='NOPE' WHERE data LIKE '%Replaceme%'"); + $this->assertEquals(3, $count); + + $count = Db::update("UPDATE test_data SET data=? WHERE data LIKE '%NOPE%'", ['Some random text!']); + $this->assertEquals(3, $count); + } + + /** + * @covers \Engelsystem\Database\Db::delete() + */ + public function testDelete() + { + $count = Db::delete('DELETE FROM test_data WHERE id=1'); + $this->assertEquals(1, $count); + + $count = Db::delete('DELETE FROM test_data WHERE data LIKE ?', ['%Replaceme%']); + $this->assertEquals(3, $count); + } + + /** + * @covers \Engelsystem\Database\Db::statement() + */ + public function testStatement() + { + $return = Db::statement('SELECT * FROM test_data WHERE id = 3'); + $this->assertTrue($return); + + $return = Db::statement('SELECT * FROM test_data WHERE id = ?', [2]); + $this->assertTrue($return); + } + + /** + * @covers \Engelsystem\Database\Db::getError() + */ + public function testGetError() + { + try { + Db::statement('foo'); + } catch (Throwable $e) { + } + + $error = Db::getError(); + $this->assertTrue(is_array($error)); + $this->assertEquals('near "foo": syntax error', $error[2]); + + $db = new Db(); + $refObject = new ReflectionObject($db); + $refProperty = $refObject->getProperty('stm'); + $refProperty->setAccessible(true); + $refProperty->setValue(null, null); + + $error = Db::getError(); + $this->assertEquals([-1, null, null], $error); + } + + /** + * @covers \Engelsystem\Database\Db::getPdo() + */ + public function testGetPdo() + { + $pdo = Db::getPdo(); + $this->assertInstanceOf(PDO::class, $pdo); + } + + /** + * @covers \Engelsystem\Database\Db::getStm() + */ + public function testGetStm() + { + $stm = Db::getStm(); + $this->assertInstanceOf(PDOStatement::class, $stm); + } + + /** + * Setup in memory database + */ + protected function setUp() + { + Db::connect('sqlite::memory:'); + Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + Db::query( + ' + CREATE TABLE test_data( + id INT PRIMARY KEY NOT NULL, + data TEXT NOT NULL + ); + '); + Db::query('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);'); + Db::insert(" + INSERT INTO test_data (id, data) + VALUES + (1, 'Foo'), + (2, 'Bar'), + (3, 'Batz'), + (4, 'Lorem ipsum dolor sit'), + (10, 'Replaceme ipsum dolor sit amet'), + (11, 'Lorem Replaceme dolor sit amet'), + (12, 'Lorem ipsum Replaceme sit amet') + ;"); + } +} -- cgit v1.2.3-70-g09d2 From ad948bdd3201e922b626a736b0122533bdd37cae Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 1 Nov 2017 14:47:09 +0100 Subject: Added RequestServiceProvider and SessionServiceProvider --- config/app.php | 2 + includes/engelsystem.php | 34 ++----- src/Http/RequestServiceProvider.php | 14 +++ src/Http/SessionServiceProvider.php | 52 ++++++++++ tests/Unit/Database/DbTest.php | 2 +- tests/Unit/Http/RequestServiceProviderTest.php | 29 ++++++ tests/Unit/Http/SessionServiceProviderTest.php | 126 +++++++++++++++++++++++++ 7 files changed, 233 insertions(+), 26 deletions(-) create mode 100644 src/Http/RequestServiceProvider.php create mode 100644 src/Http/SessionServiceProvider.php create mode 100644 tests/Unit/Http/RequestServiceProviderTest.php create mode 100644 tests/Unit/Http/SessionServiceProviderTest.php (limited to 'tests/Unit') diff --git a/config/app.php b/config/app.php index 8037479b..74eb2991 100644 --- a/config/app.php +++ b/config/app.php @@ -11,5 +11,7 @@ return [ \Engelsystem\Routing\RoutingServiceProvider::class, \Engelsystem\Renderer\RendererServiceProvider::class, \Engelsystem\Database\DatabaseServiceProvider::class, + \Engelsystem\Http\RequestServiceProvider::class, + \Engelsystem\Http\SessionServiceProvider::class, ], ]; diff --git a/includes/engelsystem.php b/includes/engelsystem.php index f9535847..97076895 100644 --- a/includes/engelsystem.php +++ b/includes/engelsystem.php @@ -3,10 +3,6 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Exceptions\Handler as ExceptionHandler; -use Engelsystem\Http\Request; -use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; /** * This file includes all needed functions, connects to the db etc. @@ -14,6 +10,12 @@ use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; require_once __DIR__ . '/autoload.php'; +/** + * Include legacy code + */ +require __DIR__ . '/includes.php'; + + /** * Initialize and bootstrap the application */ @@ -48,30 +50,12 @@ if ($app->get('config')->get('maintenance')) { /** - * Initialize Request - * - * @var Request $request + * Init translations */ -$request = Request::createFromGlobals(); -$app->instance('request', $request); - - -/** - * Include legacy code - */ -require __DIR__ . '/includes.php'; +gettext_init(); /** - * Init application + * Init authorization */ -$sessionStorage = (PHP_SAPI != 'cli' ? new NativeSessionStorage(['cookie_httponly' => true]) : new MockArraySessionStorage()); -$session = new Session($sessionStorage); -$app->instance('session', $session); -$session->start(); -$request->setSession($session); - - -gettext_init(); - load_auth(); diff --git a/src/Http/RequestServiceProvider.php b/src/Http/RequestServiceProvider.php new file mode 100644 index 00000000..077e9ecc --- /dev/null +++ b/src/Http/RequestServiceProvider.php @@ -0,0 +1,14 @@ +app->call([Request::class, 'createFromGlobals']); + $this->app->instance('request', $request); + } +} diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php new file mode 100644 index 00000000..55e3f48b --- /dev/null +++ b/src/Http/SessionServiceProvider.php @@ -0,0 +1,52 @@ +getSessionStorage(); + $this->app->instance('session.storage', $sessionStorage); + $this->app->bind(SessionStorageInterface::class, 'session.storage'); + + $session = $this->app->make(Session::class); + $this->app->instance('session', $session); + + /** @var Request $request */ + $request = $this->app->get('request'); + $request->setSession($session); + + $session->start(); + } + + /** + * Returns the session storage + * + * @return SessionStorageInterface + */ + protected function getSessionStorage() + { + if ($this->isCli()) { + return $this->app->make(MockArraySessionStorage::class); + } + + return $this->app->make(NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]); + } + + /** + * Test if is called from cli + * + * @return bool + */ + protected function isCli() + { + return PHP_SAPI == 'cli'; + } +} diff --git a/tests/Unit/Database/DbTest.php b/tests/Unit/Database/DbTest.php index 4529cd6b..63607cad 100644 --- a/tests/Unit/Database/DbTest.php +++ b/tests/Unit/Database/DbTest.php @@ -19,7 +19,7 @@ class DbTest extends TestCase $result = Db::connect('mysql:host=localhost;dbname=someTestDatabaseThatDoesNotExist;charset=utf8'); $this->assertFalse($result); - $result = Db::connect('sqlite::memory'); + $result = Db::connect('sqlite::memory:'); $this->assertTrue($result); } diff --git a/tests/Unit/Http/RequestServiceProviderTest.php b/tests/Unit/Http/RequestServiceProviderTest.php new file mode 100644 index 00000000..a137b0ac --- /dev/null +++ b/tests/Unit/Http/RequestServiceProviderTest.php @@ -0,0 +1,29 @@ +getMockBuilder(Request::class) + ->getMock(); + + $app = $this->getApp(['call', 'instance']); + + $this->setExpects($app, 'call', [[Request::class, 'createFromGlobals']], $request); + $this->setExpects($app, 'instance', ['request', $request]); + + $serviceProvider = new RequestServiceProvider($app); + $serviceProvider->register(); + } +} diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php new file mode 100644 index 00000000..0f17a1af --- /dev/null +++ b/tests/Unit/Http/SessionServiceProviderTest.php @@ -0,0 +1,126 @@ +getApp(['make', 'instance', 'bind', 'get']); + + $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); + $sessionStorage2 = $this->getMockForAbstractClass(StorageInterface::class); + + $session = $this->getSessionMock(); + $request = $this->getRequestMock(); + + /** @var MockObject|SessionServiceProvider $serviceProvider */ + $serviceProvider = $this->getMockBuilder(SessionServiceProvider::class) + ->setConstructorArgs([$app]) + ->setMethods(['isCli']) + ->getMock(); + $serviceProvider->expects($this->exactly(2)) + ->method('isCli') + ->willReturnOnConsecutiveCalls(true, false); + + $app->expects($this->exactly(4)) + ->method('make') + ->withConsecutive( + [MockArraySessionStorage::class], + [Session::class], + [NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]], + [Session::class] + ) + ->willReturnOnConsecutiveCalls( + $sessionStorage, + $session, + $sessionStorage2, + $session + ); + $app->expects($this->atLeastOnce()) + ->method('instance') + ->withConsecutive( + ['session.storage', $sessionStorage], + ['session', $session] + ); + + $this->setExpects($app, 'bind', [StorageInterface::class, 'session.storage'], null, $this->atLeastOnce()); + $this->setExpects($app, 'get', ['request'], $request, $this->atLeastOnce()); + $this->setExpects($request, 'setSession', [$session], null, $this->atLeastOnce()); + $this->setExpects($session, 'start', null, null, $this->atLeastOnce()); + + $serviceProvider->register(); + $serviceProvider->register(); + } + + /** + * @covers \Engelsystem\Http\SessionServiceProvider::isCli() + */ + public function testIsCli() + { + $app = $this->getApp(['make', 'instance', 'bind', 'get']); + + $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); + + $session = $this->getSessionMock(); + $request = $this->getRequestMock(); + + $app->expects($this->exactly(2)) + ->method('make') + ->withConsecutive( + [MockArraySessionStorage::class], + [Session::class] + ) + ->willReturnOnConsecutiveCalls( + $sessionStorage, + $session + ); + $app->expects($this->exactly(2)) + ->method('instance') + ->withConsecutive( + ['session.storage', $sessionStorage], + ['session', $session] + ); + + $this->setExpects($app, 'bind', [StorageInterface::class, 'session.storage']); + $this->setExpects($app, 'get', ['request'], $request); + $this->setExpects($request, 'setSession', [$session]); + $this->setExpects($session, 'start'); + + $serviceProvider = new SessionServiceProvider($app); + $serviceProvider->register(); + } + + /** + * @return MockObject + */ + private function getSessionMock() + { + return $this->getMockBuilder(Session::class) + ->setMethods(['start']) + ->getMock(); + } + + /** + * @return MockObject + */ + private function getRequestMock() + { + return $this->getMockBuilder(Request::class) + ->setMethods(['setSession']) + ->getMock(); + } +} -- cgit v1.2.3-70-g09d2 From 8cd17419a8fd4a28a93956b2fcd6850cdb89d58f Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 12 Nov 2017 15:38:12 +0100 Subject: TestFixes --- tests/Unit/HelpersTest.php | 3 +++ tests/Unit/Http/SessionServiceProviderTest.php | 2 ++ 2 files changed, 5 insertions(+) (limited to 'tests/Unit') diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index 0a8d5d2b..43c29c84 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -11,6 +11,7 @@ use Engelsystem\Routing\UrlGenerator; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface as StorageInterface; class HelpersTest extends TestCase { @@ -130,7 +131,9 @@ class HelpersTest extends TestCase */ public function testSession() { + $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); $sessionMock = $this->getMockBuilder(Session::class) + ->setConstructorArgs([$sessionStorage]) ->getMock(); $this->getAppMock('session', $sessionMock); diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php index 0f17a1af..a78b4f72 100644 --- a/tests/Unit/Http/SessionServiceProviderTest.php +++ b/tests/Unit/Http/SessionServiceProviderTest.php @@ -109,7 +109,9 @@ class SessionServiceProviderTest extends ServiceProviderTest */ private function getSessionMock() { + $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); return $this->getMockBuilder(Session::class) + ->setConstructorArgs([$sessionStorage]) ->setMethods(['start']) ->getMock(); } -- cgit v1.2.3-70-g09d2 From f8807c4efbbddd02c69d0af63a1bf348b051d078 Mon Sep 17 00:00:00 2001 From: msquare Date: Sun, 19 Nov 2017 14:47:32 +0100 Subject: fix behaviour of HTTP/Request get --- src/Http/Request.php | 4 ++-- tests/Unit/Http/RequestTest.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'tests/Unit') diff --git a/src/Http/Request.php b/src/Http/Request.php index e7850c8b..585fb5e9 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -38,9 +38,9 @@ class Request extends SymfonyRequest */ public function has($key) { - $value = $this->input($key); + $value = $this->input($key, null); - return !empty($value); + return !($value === null); } /** diff --git a/tests/Unit/Http/RequestTest.php b/tests/Unit/Http/RequestTest.php index 3f317367..a68f8b8f 100644 --- a/tests/Unit/Http/RequestTest.php +++ b/tests/Unit/Http/RequestTest.php @@ -47,7 +47,8 @@ class RequestTest extends TestCase ]); $this->assertTrue($request->has('foo')); - $this->assertFalse($request->has('bar')); + $this->assertTrue($request->has('bar')); + $this->assertFalse($request->has('baz')); } /** -- cgit v1.2.3-70-g09d2 From 6eea072376cc9fd1034342a0e1d2173681268138 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 20 Nov 2017 17:08:05 +0100 Subject: Added ExceptionHandler Interface --- includes/engelsystem.php | 2 +- src/Exceptions/BasicHandler.php | 119 +++++++++++++++++++++ src/Exceptions/ExceptionsServiceProvider.php | 5 +- src/Exceptions/Handler.php | 112 ++----------------- .../Exceptions/ExceptionsServiceProviderTest.php | 6 +- tests/Unit/Exceptions/HandlerTest.php | 38 +++++++ 6 files changed, 173 insertions(+), 109 deletions(-) create mode 100644 src/Exceptions/BasicHandler.php create mode 100644 tests/Unit/Exceptions/HandlerTest.php (limited to 'tests/Unit') diff --git a/includes/engelsystem.php b/includes/engelsystem.php index 97076895..688ce49f 100644 --- a/includes/engelsystem.php +++ b/includes/engelsystem.php @@ -2,7 +2,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; -use Engelsystem\Exceptions\Handler as ExceptionHandler; +use Engelsystem\Exceptions\BasicHandler as ExceptionHandler; /** * This file includes all needed functions, connects to the db etc. diff --git a/src/Exceptions/BasicHandler.php b/src/Exceptions/BasicHandler.php new file mode 100644 index 00000000..2ba960a2 --- /dev/null +++ b/src/Exceptions/BasicHandler.php @@ -0,0 +1,119 @@ +exceptionHandler($exception); + } + + /** + * @param Throwable $e + */ + public function exceptionHandler($e) + { + $this->handle( + $e->getCode(), + get_class($e) . ': ' . $e->getMessage(), + $e->getFile(), + $e->getLine(), + ['exception' => $e] + ); + } + + /** + * @param int $number + * @param string $string + * @param string $file + * @param int $line + * @param array $context + * @param array $trace + */ + protected function handle($number, $string, $file, $line, $context = [], $trace = []) + { + error_log(sprintf('Exception: Number: %s, String: %s, File: %s:%u, Context: %s', + $number, + $string, + $file, + $line, + json_encode($context) + )); + + $file = $this->stripBasePath($file); + + if ($this->environment == self::ENV_DEVELOPMENT) { + echo '
';
+            echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
+            var_export([
+                'string'     => $string,
+                'file'       => $file . ':' . $line,
+                'context'    => $context,
+                'stacktrace' => $this->formatStackTrace($trace),
+            ]);
+            echo '
'; + die(); + } + + echo 'An unexpected error occurred, a team of untrained monkeys has been dispatched to deal with it.'; + die(); + } + + /** + * @param array $stackTrace + * @return array + */ + protected function formatStackTrace($stackTrace) + { + $return = []; + + foreach ($stackTrace as $trace) { + $path = ''; + $line = ''; + + if (isset($trace['file']) && isset($trace['line'])) { + $path = $this->stripBasePath($trace['file']); + $line = $trace['line']; + } + + $functionName = $trace['function']; + + $return[] = [ + 'file' => $path . ':' . $line, + $functionName => $trace['args'], + ]; + } + + return $return; + } + + /** + * @param string $path + * @return string + */ + protected function stripBasePath($path) + { + $basePath = realpath(__DIR__ . '/../..') . '/'; + return str_replace($basePath, '', $path); + } +} diff --git a/src/Exceptions/ExceptionsServiceProvider.php b/src/Exceptions/ExceptionsServiceProvider.php index 7755e1e7..8eeccf61 100644 --- a/src/Exceptions/ExceptionsServiceProvider.php +++ b/src/Exceptions/ExceptionsServiceProvider.php @@ -3,13 +3,14 @@ namespace Engelsystem\Exceptions; use Engelsystem\Container\ServiceProvider; -use Engelsystem\Exceptions\Handler as ExceptionHandler; class ExceptionsServiceProvider extends ServiceProvider { public function register() { - $errorHandler = $this->app->make(ExceptionHandler::class); + $errorHandler = $this->app->make(BasicHandler::class); + $errorHandler->register(); $this->app->instance('error.handler', $errorHandler); + $this->app->bind(Handler::class, 'error.handler'); } } diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index 95bcd132..cdf94e32 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -2,9 +2,7 @@ namespace Engelsystem\Exceptions; -use Throwable; - -class Handler +abstract class Handler { /** @var string */ protected $environment; @@ -20,122 +18,28 @@ class Handler public function __construct($environment = self::ENV_PRODUCTION) { $this->environment = $environment; - - set_error_handler([$this, 'errorHandler']); - set_exception_handler([$this, 'exceptionHandler']); - } - - /** - * @param int $number - * @param string $string - * @param string $file - * @param int $line - * @param array $context - */ - public function errorHandler($number, $string, $file, $line, $context) - { - $trace = array_reverse(debug_backtrace()); - - $this->handle('error', $number, $string, $file, $line, $context, $trace); } /** - * @param Throwable $e + * Activate the error handler */ - public function exceptionHandler($e) + public function register() { - $this->handle( - 'exception', - $e->getCode(), - get_class($e) . ': ' . $e->getMessage(), - $e->getFile(), - $e->getLine(), - ['exception' => $e] - ); - } - - /** - * @param string $type - * @param int $number - * @param string $string - * @param string $file - * @param int $line - * @param array $context - * @param array $trace - */ - protected function handle($type, $number, $string, $file, $line, $context = [], $trace = []) - { - error_log(sprintf('%s: Number: %s, String: %s, File: %s:%u, Context: %s', - $type, - $number, - $string, - $file, - $line, - json_encode($context) - )); - - $file = $this->stripBasePath($file); - - if ($this->environment == self::ENV_DEVELOPMENT) { - echo '
';
-            echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
-            var_export([
-                'string'     => $string,
-                'file'       => $file . ':' . $line,
-                'context'    => $context,
-                'stacktrace' => $this->formatStackTrace($trace),
-            ]);
-            echo '
'; - die(); - } - - echo 'An unexpected error occurred, a team of untrained monkeys has been dispatched to deal with it.'; - die(); } /** - * @param array $stackTrace - * @return array + * @param string $environment */ - protected function formatStackTrace($stackTrace) + public function setEnvironment($environment) { - $return = []; - - foreach ($stackTrace as $trace) { - $path = ''; - $line = ''; - - if (isset($trace['file']) && isset($trace['line'])) { - $path = $this->stripBasePath($trace['file']); - $line = $trace['line']; - } - - $functionName = $trace['function']; - - $return[] = [ - 'file' => $path . ':' . $line, - $functionName => $trace['args'], - ]; - } - - return $return; + $this->environment = $environment; } /** - * @param string $path * @return string */ - protected function stripBasePath($path) + public function getEnvironment() { - $basePath = realpath(__DIR__ . '/../..') . '/'; - return str_replace($basePath, '', $path); - } - - /** - * @param string $environment - */ - public function setEnvironment($environment) - { - $this->environment = $environment; + return $this->environment; } } diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php index 9c943d52..01fb2f11 100644 --- a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php +++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php @@ -2,8 +2,9 @@ namespace Engelsystem\Test\Unit\Exceptions; +use Engelsystem\Exceptions\BasicHandler as ExceptionHandler; use Engelsystem\Exceptions\ExceptionsServiceProvider; -use Engelsystem\Exceptions\Handler as ExceptionHandler; +use Engelsystem\Exceptions\Handler; use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject; @@ -18,10 +19,11 @@ class ExceptionsServiceProviderTest extends ServiceProviderTest $exceptionHandler = $this->getMockBuilder(ExceptionHandler::class) ->getMock(); - $app = $this->getApp(); + $app = $this->getApp(['make', 'instance', 'bind']); $this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler); $this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]); + $this->setExpects($app, 'bind', [Handler::class, 'error.handler']); $serviceProvider = new ExceptionsServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php new file mode 100644 index 00000000..29759be7 --- /dev/null +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -0,0 +1,38 @@ +getMockForAbstractClass(Handler::class); + $this->assertInstanceOf(Handler::class, $handler); + $handler->register(); + } + + /** + * @covers \Engelsystem\Exceptions\Handler::setEnvironment() + * @covers \Engelsystem\Exceptions\Handler::getEnvironment() + */ + public function testEnvironment() + { + /** @var Handler|Mock $handler */ + $handler = $this->getMockForAbstractClass(Handler::class); + + $handler->setEnvironment(Handler::ENV_DEVELOPMENT); + $this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment()); + + $handler->setEnvironment(Handler::ENV_PRODUCTION); + $this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment()); + } +} -- cgit v1.2.3-70-g09d2 From 25e434bce4986b48bd72729a55aa1096e5a76be3 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 24 Nov 2017 15:08:43 +0100 Subject: Refactored ExceptionHandler --- composer.json | 4 +- includes/engelsystem.php | 6 +- src/Exceptions/BasicHandler.php | 119 --------------------- src/Exceptions/ExceptionsServiceProvider.php | 46 +++++++- src/Exceptions/Handler.php | 100 ++++++++++++++++- src/Exceptions/Handlers/HandlerInterface.php | 21 ++++ src/Exceptions/Handlers/Legacy.php | 42 ++++++++ src/Exceptions/Handlers/LegacyDevelopment.php | 57 ++++++++++ src/Exceptions/Handlers/Whoops.php | 85 +++++++++++++++ .../Exceptions/ExceptionsServiceProviderTest.php | 94 ++++++++++++++-- tests/Unit/Exceptions/HandlerTest.php | 108 ++++++++++++++++++- .../Exceptions/Handlers/LegacyDevelopmentTest.php | 35 ++++++ tests/Unit/Exceptions/Handlers/LegacyTest.php | 55 ++++++++++ tests/Unit/Exceptions/Handlers/WhoopsTest.php | 83 ++++++++++++++ 14 files changed, 715 insertions(+), 140 deletions(-) delete mode 100644 src/Exceptions/BasicHandler.php create mode 100644 src/Exceptions/Handlers/HandlerInterface.php create mode 100644 src/Exceptions/Handlers/Legacy.php create mode 100644 src/Exceptions/Handlers/LegacyDevelopment.php create mode 100644 src/Exceptions/Handlers/Whoops.php create mode 100644 tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php create mode 100644 tests/Unit/Exceptions/Handlers/LegacyTest.php create mode 100644 tests/Unit/Exceptions/Handlers/WhoopsTest.php (limited to 'tests/Unit') diff --git a/composer.json b/composer.json index a8f0b0d6..ed34ba03 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,9 @@ "twbs/bootstrap": "^3.3" }, "require-dev": { - "phpunit/phpunit": "^6.3" + "filp/whoops": "^2.1", + "phpunit/phpunit": "^6.3", + "symfony/var-dumper": "^3.3" }, "autoload": { "psr-4": { diff --git a/includes/engelsystem.php b/includes/engelsystem.php index 688ce49f..07abbb42 100644 --- a/includes/engelsystem.php +++ b/includes/engelsystem.php @@ -2,7 +2,8 @@ use Engelsystem\Application; use Engelsystem\Config\Config; -use Engelsystem\Exceptions\BasicHandler as ExceptionHandler; +use Engelsystem\Exceptions\Handler; +use Engelsystem\Exceptions\Handlers\HandlerInterface; /** * This file includes all needed functions, connects to the db etc. @@ -32,7 +33,8 @@ date_default_timezone_set($app->get('config')->get('timezone')); if (config('environment') == 'development') { $errorHandler = $app->get('error.handler'); - $errorHandler->setEnvironment(ExceptionHandler::ENV_DEVELOPMENT); + $errorHandler->setEnvironment(Handler::ENV_DEVELOPMENT); + $app->bind(HandlerInterface::class, 'error.handler.development'); ini_set('display_errors', true); error_reporting(E_ALL); } else { diff --git a/src/Exceptions/BasicHandler.php b/src/Exceptions/BasicHandler.php deleted file mode 100644 index 2ba960a2..00000000 --- a/src/Exceptions/BasicHandler.php +++ /dev/null @@ -1,119 +0,0 @@ -exceptionHandler($exception); - } - - /** - * @param Throwable $e - */ - public function exceptionHandler($e) - { - $this->handle( - $e->getCode(), - get_class($e) . ': ' . $e->getMessage(), - $e->getFile(), - $e->getLine(), - ['exception' => $e] - ); - } - - /** - * @param int $number - * @param string $string - * @param string $file - * @param int $line - * @param array $context - * @param array $trace - */ - protected function handle($number, $string, $file, $line, $context = [], $trace = []) - { - error_log(sprintf('Exception: Number: %s, String: %s, File: %s:%u, Context: %s', - $number, - $string, - $file, - $line, - json_encode($context) - )); - - $file = $this->stripBasePath($file); - - if ($this->environment == self::ENV_DEVELOPMENT) { - echo '
';
-            echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
-            var_export([
-                'string'     => $string,
-                'file'       => $file . ':' . $line,
-                'context'    => $context,
-                'stacktrace' => $this->formatStackTrace($trace),
-            ]);
-            echo '
'; - die(); - } - - echo 'An unexpected error occurred, a team of untrained monkeys has been dispatched to deal with it.'; - die(); - } - - /** - * @param array $stackTrace - * @return array - */ - protected function formatStackTrace($stackTrace) - { - $return = []; - - foreach ($stackTrace as $trace) { - $path = ''; - $line = ''; - - if (isset($trace['file']) && isset($trace['line'])) { - $path = $this->stripBasePath($trace['file']); - $line = $trace['line']; - } - - $functionName = $trace['function']; - - $return[] = [ - 'file' => $path . ':' . $line, - $functionName => $trace['args'], - ]; - } - - return $return; - } - - /** - * @param string $path - * @return string - */ - protected function stripBasePath($path) - { - $basePath = realpath(__DIR__ . '/../..') . '/'; - return str_replace($basePath, '', $path); - } -} diff --git a/src/Exceptions/ExceptionsServiceProvider.php b/src/Exceptions/ExceptionsServiceProvider.php index 8eeccf61..a9bc2b17 100644 --- a/src/Exceptions/ExceptionsServiceProvider.php +++ b/src/Exceptions/ExceptionsServiceProvider.php @@ -3,14 +3,56 @@ namespace Engelsystem\Exceptions; use Engelsystem\Container\ServiceProvider; +use Engelsystem\Exceptions\Handlers\HandlerInterface; +use Engelsystem\Exceptions\Handlers\Legacy; +use Engelsystem\Exceptions\Handlers\LegacyDevelopment; +use Engelsystem\Exceptions\Handlers\Whoops; +use Whoops\Run as WhoopsRunner; class ExceptionsServiceProvider extends ServiceProvider { public function register() { - $errorHandler = $this->app->make(BasicHandler::class); - $errorHandler->register(); + $errorHandler = $this->app->make(Handler::class); + $this->addProductionHandler($errorHandler); + $this->addDevelopmentHandler($errorHandler); $this->app->instance('error.handler', $errorHandler); $this->app->bind(Handler::class, 'error.handler'); + $errorHandler->register(); + } + + public function boot() + { + /** @var Handler $handler */ + $handler = $this->app->get('error.handler'); + $request = $this->app->get('request'); + + $handler->setRequest($request); + } + + /** + * @param Handler $errorHandler + */ + protected function addProductionHandler($errorHandler) + { + $handler = $this->app->make(Legacy::class); + $this->app->instance('error.handler.production', $handler); + $errorHandler->setHandler(Handler::ENV_PRODUCTION, $handler); + $this->app->bind(HandlerInterface::class, 'error.handler.production'); + } + + /** + * @param Handler $errorHandler + */ + protected function addDevelopmentHandler($errorHandler) + { + $handler = $this->app->make(LegacyDevelopment::class); + + if (class_exists(WhoopsRunner::class)) { + $handler = $this->app->make(Whoops::class); + } + + $this->app->instance('error.handler.development', $handler); + $errorHandler->setHandler(Handler::ENV_DEVELOPMENT, $handler); } } diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index cdf94e32..ee15717a 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -2,18 +2,29 @@ namespace Engelsystem\Exceptions; -abstract class Handler +use Engelsystem\Exceptions\Handlers\HandlerInterface; +use Engelsystem\Http\Request; +use ErrorException; +use Throwable; + +class Handler { /** @var string */ protected $environment; + /** @var HandlerInterface[] */ + protected $handler = []; + + /** @var Request */ + protected $request; + const ENV_PRODUCTION = 'prod'; const ENV_DEVELOPMENT = 'dev'; /** * Handler constructor. * - * @param string $environment production|development + * @param string $environment prod|dev */ public function __construct($environment = self::ENV_PRODUCTION) { @@ -25,14 +36,47 @@ abstract class Handler */ public function register() { + set_error_handler([$this, 'errorHandler']); + set_exception_handler([$this, 'exceptionHandler']); } /** - * @param string $environment + * @param int $number + * @param string $message + * @param string $file + * @param int $line */ - public function setEnvironment($environment) + public function errorHandler($number, $message, $file, $line) { - $this->environment = $environment; + $exception = new ErrorException($message, 0, $number, $file, $line); + $this->exceptionHandler($exception); + } + + /** + * @param Throwable $e + */ + public function exceptionHandler($e) + { + if (!$this->request instanceof Request) { + $this->request = new Request(); + } + + $handler = $this->handler[$this->environment]; + $handler->report($e); + $handler->render($this->request, $e); + $this->die(); + } + + /** + * Exit the application + * + * @codeCoverageIgnore + * @param string $message + */ + protected function die($message = '') + { + echo $message; + die(); } /** @@ -42,4 +86,50 @@ abstract class Handler { return $this->environment; } + + /** + * @param string $environment + */ + public function setEnvironment($environment) + { + $this->environment = $environment; + } + + /** + * @param string $environment + * @return HandlerInterface|HandlerInterface[] + */ + public function getHandler($environment = null) + { + if (!is_null($environment)) { + return $this->handler[$environment]; + } + + return $this->handler; + } + + /** + * @param string $environment + * @param HandlerInterface $handler + */ + public function setHandler($environment, HandlerInterface $handler) + { + $this->handler[$environment] = $handler; + } + + /** + * @return Request + */ + public function getRequest() + { + return $this->request; + } + + /** + * @param Request $request + */ + public function setRequest(Request $request) + { + $this->request = $request; + } } diff --git a/src/Exceptions/Handlers/HandlerInterface.php b/src/Exceptions/Handlers/HandlerInterface.php new file mode 100644 index 00000000..9de33e1f --- /dev/null +++ b/src/Exceptions/Handlers/HandlerInterface.php @@ -0,0 +1,21 @@ +unexpected error occurred, a team of untrained monkeys has been dispatched to deal with it.'; + } + + /** + * @param Throwable $e + */ + public function report(Throwable $e) + { + error_log(sprintf('Exception: Code: %s, Message: %s, File: %s:%u, Trace: %s', + $e->getCode(), + $e->getMessage(), + $this->stripBasePath($e->getFile()), + $e->getLine(), + json_encode($e->getTrace()) + )); + } + + /** + * @param string $path + * @return string + */ + protected function stripBasePath($path) + { + $basePath = realpath(__DIR__ . '/../../..') . '/'; + return str_replace($basePath, '', $path); + } +} diff --git a/src/Exceptions/Handlers/LegacyDevelopment.php b/src/Exceptions/Handlers/LegacyDevelopment.php new file mode 100644 index 00000000..86f86f4c --- /dev/null +++ b/src/Exceptions/Handlers/LegacyDevelopment.php @@ -0,0 +1,57 @@ +stripBasePath($e->getFile()); + + echo '
';
+        echo sprintf('%s: (%s)' . PHP_EOL, get_class($e), $e->getCode());
+        $data = [
+            'string'     => $e->getMessage(),
+            'file'       => $file . ':' . $e->getLine(),
+            'stacktrace' => $this->formatStackTrace($e->getTrace()),
+        ];
+        var_dump($data);
+        echo '
'; + } + + /** + * @param array $stackTrace + * @return array + */ + protected function formatStackTrace($stackTrace) + { + $return = []; + $stackTrace = array_reverse($stackTrace); + + foreach ($stackTrace as $trace) { + $path = ''; + $line = ''; + + if (isset($trace['file']) && isset($trace['line'])) { + $path = $this->stripBasePath($trace['file']); + $line = $trace['line']; + } + + $functionName = $trace['function']; + + $return[] = [ + 'file' => $path . ':' . $line, + $functionName => isset($trace['args']) ? $trace['args'] : null, + ]; + } + + return $return; + } +} diff --git a/src/Exceptions/Handlers/Whoops.php b/src/Exceptions/Handlers/Whoops.php new file mode 100644 index 00000000..807f5eb0 --- /dev/null +++ b/src/Exceptions/Handlers/Whoops.php @@ -0,0 +1,85 @@ +app = $app; + } + + /** + * @param Request $request + * @param Throwable $e + */ + public function render($request, Throwable $e) + { + $whoops = $this->app->make(WhoopsRunner::class); + $handler = $this->getPrettyPageHandler($e); + $whoops->pushHandler($handler); + + if ($request->isXmlHttpRequest()) { + $handler = $this->getJsonResponseHandler(); + $whoops->pushHandler($handler); + } + + echo $whoops->handleException($e); + } + + /** + * @param Throwable $e + * @return PrettyPageHandler + */ + protected function getPrettyPageHandler(Throwable $e) + { + $handler = $this->app->make(PrettyPageHandler::class); + + $handler->setPageTitle('Just another ' . get_class($e) . ' to fix :('); + $handler->setApplicationPaths([realpath(__DIR__ . '/../..')]); + + $data = $this->getData(); + $handler->addDataTable('Application', $data); + + return $handler; + } + + /** + * @return JsonResponseHandler + */ + protected function getJsonResponseHandler() + { + $handler = $this->app->make(JsonResponseHandler::class); + $handler->setJsonApi(true); + $handler->addTraceToOutput(true); + + return $handler; + } + + /** + * Aggregate application data + * + * @return array + */ + protected function getData() + { + global $user; + + $data = []; + $data['user'] = $user; + $data['Booted'] = $this->app->isBooted(); + + return $data; + } +} diff --git a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php index 01fb2f11..4f2ae654 100644 --- a/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php +++ b/tests/Unit/Exceptions/ExceptionsServiceProviderTest.php @@ -2,30 +2,108 @@ namespace Engelsystem\Test\Unit\Exceptions; -use Engelsystem\Exceptions\BasicHandler as ExceptionHandler; use Engelsystem\Exceptions\ExceptionsServiceProvider; use Engelsystem\Exceptions\Handler; +use Engelsystem\Exceptions\Handlers\HandlerInterface; +use Engelsystem\Exceptions\Handlers\Legacy; +use Engelsystem\Exceptions\Handlers\LegacyDevelopment; +use Engelsystem\Exceptions\Handlers\Whoops; +use Engelsystem\Http\Request; use Engelsystem\Test\Unit\ServiceProviderTest; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_MockObject_MockObject as MockObject; class ExceptionsServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::register() + * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addProductionHandler() + * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::addDevelopmentHandler() */ public function testRegister() { - /** @var PHPUnit_Framework_MockObject_MockObject|ExceptionHandler $exceptionHandler */ - $exceptionHandler = $this->getMockBuilder(ExceptionHandler::class) + $app = $this->getApp(['make', 'instance', 'bind']); + + /** @var MockObject|Handler $handler */ + $handler = $this->createMock(Handler::class); + $this->setExpects($handler, 'register'); + /** @var Legacy|MockObject $legacyHandler */ + $legacyHandler = $this->createMock(Legacy::class); + /** @var LegacyDevelopment|MockObject $developmentHandler */ + $developmentHandler = $this->createMock(LegacyDevelopment::class); + + $whoopsHandler = $this->getMockBuilder(Whoops::class) + ->setConstructorArgs([$app]) ->getMock(); - $app = $this->getApp(['make', 'instance', 'bind']); + $app->expects($this->exactly(3)) + ->method('instance') + ->withConsecutive( + ['error.handler.production', $legacyHandler], + ['error.handler.development', $whoopsHandler], + ['error.handler', $handler] + ); - $this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler); - $this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]); - $this->setExpects($app, 'bind', [Handler::class, 'error.handler']); + $app->expects($this->exactly(4)) + ->method('make') + ->withConsecutive( + [Handler::class], + [Legacy::class], + [LegacyDevelopment::class], + [Whoops::class] + ) + ->willReturnOnConsecutiveCalls( + $handler, + $legacyHandler, + $developmentHandler, + $whoopsHandler + ); + + $app->expects($this->exactly(2)) + ->method('bind') + ->withConsecutive( + [HandlerInterface::class, 'error.handler.production'], + [Handler::class, 'error.handler'] + ); + + $handler->expects($this->exactly(2)) + ->method('setHandler') + ->withConsecutive( + [Handler::ENV_PRODUCTION, $legacyHandler], + [Handler::ENV_DEVELOPMENT, $whoopsHandler] + ); $serviceProvider = new ExceptionsServiceProvider($app); $serviceProvider->register(); } + + /** + * @covers \Engelsystem\Exceptions\ExceptionsServiceProvider::boot() + */ + public function testBoot() + { + /** @var MockObject|Handler $handler */ + $handler = $this->createMock(Handler::class); + + /** @var MockObject|Request $request */ + $request = $this->createMock(Request::class); + + $handler->expects($this->once()) + ->method('setRequest') + ->with($request); + + $app = $this->getApp(['get']); + $app->expects($this->exactly(2)) + ->method('get') + ->withConsecutive( + ['error.handler'], + ['request'] + ) + ->willReturnOnConsecutiveCalls( + $handler, + $request + ); + + $provider = new ExceptionsServiceProvider($app); + $provider->boot(); + } } diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index 29759be7..40202be8 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -3,6 +3,10 @@ namespace Engelsystem\Test\Unit\Exceptions; use Engelsystem\Exceptions\Handler; +use Engelsystem\Exceptions\Handlers\HandlerInterface; +use Engelsystem\Http\Request; +use ErrorException; +use Exception; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as Mock; @@ -10,14 +14,80 @@ class HandlerTest extends TestCase { /** * @covers \Engelsystem\Exceptions\Handler::__construct() + */ + public function testCreate() + { + /** @var Handler|Mock $handler */ + $handler = new Handler(); + $this->assertInstanceOf(Handler::class, $handler); + $this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment()); + + $anotherHandler = new Handler(Handler::ENV_DEVELOPMENT); + $this->assertEquals(Handler::ENV_DEVELOPMENT, $anotherHandler->getEnvironment()); + } + + /** + * @covers \Engelsystem\Exceptions\Handler::errorHandler() + */ + public function testErrorHandler() + { + /** @var Handler|Mock $handler */ + $handler = $this->getMockBuilder(Handler::class) + ->setMethods(['exceptionHandler']) + ->getMock(); + + $handler->expects($this->once()) + ->method('exceptionHandler') + ->with($this->isInstanceOf(ErrorException::class)); + + $handler->errorHandler(1, 'Foo and bar!', '/lo/rem.php', 123); + } + + /** + * @covers \Engelsystem\Exceptions\Handler::exceptionHandler() + */ + public function testExceptionHandler() + { + $exception = new Exception(); + + /** @var HandlerInterface|Mock $handlerMock */ + $handlerMock = $this->getMockForAbstractClass(HandlerInterface::class); + $handlerMock->expects($this->once()) + ->method('report') + ->with($exception); + $handlerMock->expects($this->once()) + ->method('render') + ->with($this->isInstanceOf(Request::class), $exception); + + /** @var Handler|Mock $handler */ + $handler = $this->getMockBuilder(Handler::class) + ->setMethods(['die']) + ->getMock(); + $handler->expects($this->once()) + ->method('die'); + + $handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock); + + $handler->exceptionHandler($exception); + } + + /** * @covers \Engelsystem\Exceptions\Handler::register() */ public function testRegister() { /** @var Handler|Mock $handler */ $handler = $this->getMockForAbstractClass(Handler::class); - $this->assertInstanceOf(Handler::class, $handler); $handler->register(); + + set_error_handler($errorHandler = set_error_handler('var_dump')); + $this->assertEquals($handler, array_shift($errorHandler)); + + set_exception_handler($exceptionHandler = set_error_handler('var_dump')); + $this->assertEquals($handler, array_shift($exceptionHandler)); + + restore_error_handler(); + restore_exception_handler(); } /** @@ -26,8 +96,7 @@ class HandlerTest extends TestCase */ public function testEnvironment() { - /** @var Handler|Mock $handler */ - $handler = $this->getMockForAbstractClass(Handler::class); + $handler = new Handler(); $handler->setEnvironment(Handler::ENV_DEVELOPMENT); $this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment()); @@ -35,4 +104,37 @@ class HandlerTest extends TestCase $handler->setEnvironment(Handler::ENV_PRODUCTION); $this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment()); } + + /** + * @covers \Engelsystem\Exceptions\Handler::setHandler() + * @covers \Engelsystem\Exceptions\Handler::getHandler() + */ + public function testHandler() + { + $handler = new Handler(); + /** @var HandlerInterface|Mock $devHandler */ + $devHandler = $this->getMockForAbstractClass(HandlerInterface::class); + /** @var HandlerInterface|Mock $prodHandler */ + $prodHandler = $this->getMockForAbstractClass(HandlerInterface::class); + + $handler->setHandler(Handler::ENV_DEVELOPMENT, $devHandler); + $handler->setHandler(Handler::ENV_PRODUCTION, $prodHandler); + $this->assertEquals($devHandler, $handler->getHandler(Handler::ENV_DEVELOPMENT)); + $this->assertEquals($prodHandler, $handler->getHandler(Handler::ENV_PRODUCTION)); + $this->assertCount(2, $handler->getHandler()); + } + + /** + * @covers \Engelsystem\Exceptions\Handler::setRequest() + * @covers \Engelsystem\Exceptions\Handler::getRequest() + */ + public function testRequest() + { + $handler = new Handler(); + /** @var Request|Mock $request */ + $request = $this->createMock(Request::class); + + $handler->setRequest($request); + $this->assertEquals($request, $handler->getRequest()); + } } diff --git a/tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php b/tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php new file mode 100644 index 00000000..d5390c9e --- /dev/null +++ b/tests/Unit/Exceptions/Handlers/LegacyDevelopmentTest.php @@ -0,0 +1,35 @@ +createMock(Request::class); + $exception = new ErrorException('Lorem Ipsum', 4242, 1, 'foo.php', 9999); + + $regex = sprintf( + '%%.*ErrorException.*4242.*Lorem Ipsum.*%s.*%s.*%s.*%%is', + 'foo.php', + 9999, + __FUNCTION__ + ); + $this->expectOutputRegex($regex); + + $handler->render($request, $exception); + } +} diff --git a/tests/Unit/Exceptions/Handlers/LegacyTest.php b/tests/Unit/Exceptions/Handlers/LegacyTest.php new file mode 100644 index 00000000..04b214f2 --- /dev/null +++ b/tests/Unit/Exceptions/Handlers/LegacyTest.php @@ -0,0 +1,55 @@ +createMock(Request::class); + /** @var Exception|Mock $exception */ + $exception = $this->createMock(Exception::class); + + $this->expectOutputRegex('/.*error occurred.*/i'); + + $handler->render($request, $exception); + } + + /** + * @covers \Engelsystem\Exceptions\Handlers\Legacy::report() + * @covers \Engelsystem\Exceptions\Handlers\Legacy::stripBasePath() + */ + public function testReport() + { + $handler = new Legacy(); + $exception = new Exception('Lorem Ipsum', 4242); + $line = __LINE__ - 1; + + $log = tempnam(sys_get_temp_dir(), 'engelsystem-log'); + $errorLog = ini_get('error_log'); + ini_set('error_log', $log); + $handler->report($exception); + ini_set('error_log', $errorLog); + $logContent = file_get_contents($log); + unset($log); + + $this->assertContains('4242', $logContent); + $this->assertContains('Lorem Ipsum', $logContent); + $this->assertContains(basename(__FILE__), $logContent); + $this->assertContains((string)$line, $logContent); + $this->assertContains(__FUNCTION__, $logContent); + $this->assertContains(json_encode(__CLASS__), $logContent); + } +} diff --git a/tests/Unit/Exceptions/Handlers/WhoopsTest.php b/tests/Unit/Exceptions/Handlers/WhoopsTest.php new file mode 100644 index 00000000..261ee83f --- /dev/null +++ b/tests/Unit/Exceptions/Handlers/WhoopsTest.php @@ -0,0 +1,83 @@ +createMock(Application::class); + /** @var Request|Mock $request */ + $request = $this->createMock(Request::class); + $request->expects($this->once()) + ->method('isXmlHttpRequest') + ->willReturn(true); + /** @var WhoopsRunnerInterface|Mock $whoopsRunner */ + $whoopsRunner = $this->getMockForAbstractClass(WhoopsRunnerInterface::class); + /** @var PrettyPageHandler|Mock $prettyPageHandler */ + $prettyPageHandler = $this->createMock(PrettyPageHandler::class); + $prettyPageHandler + ->expects($this->atLeastOnce()) + ->method('setApplicationPaths'); + $prettyPageHandler + ->expects($this->once()) + ->method('setApplicationPaths'); + $prettyPageHandler + ->expects($this->once()) + ->method('addDataTable'); + /** @var JsonResponseHandler|Mock $jsonResponseHandler */ + $jsonResponseHandler = $this->createMock(JsonResponseHandler::class); + $jsonResponseHandler->expects($this->once()) + ->method('setJsonApi') + ->with(true); + $jsonResponseHandler->expects($this->once()) + ->method('addTraceToOutput') + ->with(true); + /** @var Exception|Mock $exception */ + $exception = $this->createMock(Exception::class); + + $app->expects($this->exactly(3)) + ->method('make') + ->withConsecutive( + [WhoopsRunner::class], + [PrettyPageHandler::class], + [JsonResponseHandler::class] + ) + ->willReturnOnConsecutiveCalls( + $whoopsRunner, + $prettyPageHandler, + $jsonResponseHandler + ); + + $whoopsRunner + ->expects($this->exactly(2)) + ->method('pushHandler') + ->withConsecutive( + [$prettyPageHandler], + [$jsonResponseHandler] + ); + $whoopsRunner + ->expects($this->once()) + ->method('handleException') + ->with($exception); + + $handler = new Whoops($app); + $handler->render($request, $exception); + } +} -- cgit v1.2.3-70-g09d2