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-54-g00ecf 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-54-g00ecf 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-54-g00ecf 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-54-g00ecf 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-54-g00ecf 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-54-g00ecf 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-54-g00ecf