diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-02 02:09:56 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-02 02:09:56 +0200 |
commit | 9e217d87c095170460a8580d5215ddf7cbe639f4 (patch) | |
tree | 3b426fe52e70ef0f1395edcb354f1be4788ca329 /tests | |
parent | ac48332166ce28fcb1a2fc130c7f5adbc760e42d (diff) |
Template refactoring to use twig
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Unit/Http/RequestServiceProviderTest.php | 9 | ||||
-rw-r--r-- | tests/Unit/Http/ResponseServiceProviderTest.php | 9 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php | 56 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php | 12 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php | 34 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/LegacyTest.php | 59 | ||||
-rw-r--r-- | tests/Unit/Renderer/TwigServiceProviderTest.php | 28 |
7 files changed, 199 insertions, 8 deletions
diff --git a/tests/Unit/Http/RequestServiceProviderTest.php b/tests/Unit/Http/RequestServiceProviderTest.php index a137b0ac..1a42ae86 100644 --- a/tests/Unit/Http/RequestServiceProviderTest.php +++ b/tests/Unit/Http/RequestServiceProviderTest.php @@ -6,6 +6,7 @@ use Engelsystem\Http\Request; use Engelsystem\Http\RequestServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Symfony\Component\HttpFoundation\Request as SymfonyRequest; class RequestServiceProviderTest extends ServiceProviderTest { @@ -21,7 +22,13 @@ class RequestServiceProviderTest extends ServiceProviderTest $app = $this->getApp(['call', 'instance']); $this->setExpects($app, 'call', [[Request::class, 'createFromGlobals']], $request); - $this->setExpects($app, 'instance', ['request', $request]); + $app->expects($this->exactly(3)) + ->method('instance') + ->withConsecutive( + [Request::class, $request], + [SymfonyRequest::class, $request], + ['request', $request] + ); $serviceProvider = new RequestServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Http/ResponseServiceProviderTest.php b/tests/Unit/Http/ResponseServiceProviderTest.php index 52e95714..6ce1f4fe 100644 --- a/tests/Unit/Http/ResponseServiceProviderTest.php +++ b/tests/Unit/Http/ResponseServiceProviderTest.php @@ -6,6 +6,7 @@ use Engelsystem\Http\Response; use Engelsystem\Http\ResponseServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Symfony\Component\HttpFoundation\Response as SymfonyResponse; class ResponseServiceProviderTest extends ServiceProviderTest { @@ -21,7 +22,13 @@ class ResponseServiceProviderTest extends ServiceProviderTest $app = $this->getApp(); $this->setExpects($app, 'make', [Response::class], $response); - $this->setExpects($app, 'instance', ['response', $response]); + $app->expects($this->exactly(3)) + ->method('instance') + ->withConsecutive( + [Response::class, $response], + [SymfonyResponse::class, $response], + ['response', $response] + ); $serviceProvider = new ResponseServiceProvider($app); $serviceProvider->register(); diff --git a/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php new file mode 100644 index 00000000..0a72c0e7 --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/AuthenticationTest.php @@ -0,0 +1,56 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use Engelsystem\Renderer\Twig\Extensions\Authentication; + +class AuthenticationTest extends ExtensionTest +{ + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::getFunctions + */ + public function testGetFunctions() + { + $extension = new Authentication(); + $functions = $extension->getFunctions(); + + $this->assertExtensionExists('is_user', [$extension, 'isAuthenticated'], $functions); + $this->assertExtensionExists('is_guest', [$extension, 'isGuest'], $functions); + $this->assertExtensionExists('has_permission_to', [$extension, 'checkAuth'], $functions); + } + + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::isAuthenticated + * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::isGuest + */ + public function testIsAuthenticated() + { + global $user; + $user = []; + + $extension = new Authentication(); + + $this->assertFalse($extension->isAuthenticated()); + $this->assertTrue($extension->isGuest()); + + $user = ['lorem' => 'ipsum']; + $this->assertTrue($extension->isAuthenticated()); + $this->assertFalse($extension->isGuest()); + } + + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Authentication::checkAuth + */ + public function testCheckAuth() + { + global $privileges; + $privileges = []; + + $extension = new Authentication(); + + $this->assertFalse($extension->checkAuth('foo.bar')); + + $privileges = ['foo.bar']; + $this->assertTrue($extension->checkAuth('foo.bar')); + } +} diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php index e1c5a378..ef7dba7a 100644 --- a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php @@ -2,8 +2,10 @@ namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Twig_Function as TwigFunction; +use Twig_Node as TwigNode; abstract class ExtensionTest extends TestCase { @@ -35,7 +37,7 @@ abstract class ExtensionTest extends TestCase * @param callable $callback * @param TwigFunction[] $functions */ - protected function assertExtensionExists($name, $callback, $functions) + protected function assertExtensionExists($name, $callback, $functions, $options = []) { foreach ($functions as $function) { if ($function->getName() != $name) { @@ -43,6 +45,14 @@ abstract class ExtensionTest extends TestCase } $this->assertEquals($callback, $function->getCallable()); + + if (isset($options['is_save'])) { + /** @var TwigNode|MockObject $twigNode */ + $twigNode = $this->createMock(TwigNode::class); + + $this->assertArraySubset($options['is_save'], $function->getSafe($twigNode)); + } + return; } diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php index 6cc3a4da..cdd9cd2a 100644 --- a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php @@ -2,24 +2,54 @@ namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; +use Carbon\Carbon; use Engelsystem\Renderer\Twig\Extensions\Globals; +use PHPUnit\Framework\MockObject\MockObject; class GlobalsTest extends ExtensionTest { /** * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals + * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::filterEventConfig */ public function testGetGlobals() { - $extension = new Globals(); + global $user; + $user = []; + + /** @var Globals|MockObject $extension */ + $extension = $this->getMockBuilder(Globals::class) + ->setMethods(['getEventConfig']) + ->getMock(); + + $extension->expects($this->exactly(2)) + ->method('getEventConfig') + ->willReturnOnConsecutiveCalls( + null, + [ + 'lorem' => 'ipsum', + 'event_end_date' => 1234567890, + ] + ); + $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', [], $globals); + $this->assertGlobalsExists('event_config', [], $globals); - global $user; $user['foo'] = 'bar'; $globals = $extension->getGlobals(); $this->assertGlobalsExists('user', ['foo' => 'bar'], $globals); + $this->assertGlobalsExists('event_config', ['lorem' => 'ipsum'], $globals); + + $config = $globals['event_config']; + $this->assertArrayHasKey('event_end_date', $config); + /** @var Carbon $eventEndDate */ + $eventEndDate = $config['event_end_date']; + $this->assertInstanceOf(Carbon::class, $eventEndDate); + + $eventEndDate->setTimezone('UTC'); + $this->assertEquals('2009-02-13 23:31:30', $eventEndDate->format('Y-m-d H:i:s')); } } diff --git a/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php b/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php new file mode 100644 index 00000000..613af0da --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/LegacyTest.php @@ -0,0 +1,59 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use Engelsystem\Http\Request; +use Engelsystem\Renderer\Twig\Extensions\Legacy; +use PHPUnit\Framework\MockObject\MockObject; + +class LegacyTest extends ExtensionTest +{ + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Legacy::getFunctions + */ + public function testGetFunctions() + { + $isSafeHtml = ['is_safe' => ['html']]; + /** @var Request|MockObject $request */ + $request = $this->createMock(Request::class); + + $extension = new Legacy($request); + $functions = $extension->getFunctions(); + + $this->assertExtensionExists('menu', 'make_navigation', $functions, $isSafeHtml); + $this->assertExtensionExists('menuUserShiftState', 'User_shift_state_render', $functions, $isSafeHtml); + $this->assertExtensionExists('menuUserMessages', 'user_unread_messages', $functions, $isSafeHtml); + $this->assertExtensionExists('menuUserHints', 'header_render_hints', $functions, $isSafeHtml); + $this->assertExtensionExists('menuUserSubmenu', 'make_user_submenu', $functions, $isSafeHtml); + $this->assertExtensionExists('page', [$extension, 'getPage'], $functions); + } + + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Legacy::getPage + * @covers \Engelsystem\Renderer\Twig\Extensions\Legacy::__construct + */ + public function testIsAuthenticated() + { + /** @var Request|MockObject $request */ + $request = $this->createMock(Request::class); + + $extension = new Legacy($request); + + $request->expects($this->exactly(2)) + ->method('has') + ->with('p') + ->willReturnOnConsecutiveCalls(true, false); + + $request->expects($this->once()) + ->method('get') + ->with('p') + ->willReturn('foo-bar'); + + $request->expects($this->once()) + ->method('path') + ->willReturn('batz'); + + $this->assertEquals('foo-bar', $extension->getPage()); + $this->assertEquals('batz', $extension->getPage()); + } +} diff --git a/tests/Unit/Renderer/TwigServiceProviderTest.php b/tests/Unit/Renderer/TwigServiceProviderTest.php index 3cd0da4d..0d632633 100644 --- a/tests/Unit/Renderer/TwigServiceProviderTest.php +++ b/tests/Unit/Renderer/TwigServiceProviderTest.php @@ -2,6 +2,7 @@ namespace Engelsystem\Test\Unit\Renderer; +use Engelsystem\Config\Config; use Engelsystem\Renderer\TwigEngine; use Engelsystem\Renderer\TwigLoader; use Engelsystem\Renderer\TwigServiceProvider; @@ -10,6 +11,7 @@ use PHPUnit\Framework\MockObject\MockObject; use ReflectionClass as Reflection; use stdClass; use Twig_Environment as Twig; +use Twig_Extension_Core as TwigCore; use Twig_ExtensionInterface as ExtensionInterface; use Twig_LoaderInterface as TwigLoaderInterface; @@ -97,6 +99,12 @@ class TwigServiceProviderTest extends ServiceProviderTest $twigLoader = $this->createMock(TwigLoader::class); /** @var Twig|MockObject $twig */ $twig = $this->createMock(Twig::class); + /** @var Config|MockObject $config */ + $config = $this->createMock(Config::class); + /** @var TwigCore|MockObject $twigCore */ + $twigCore = $this->getMockBuilder(stdClass::class) + ->setMethods(['setTimezone']) + ->getMock(); $app = $this->getApp(['make', 'instance', 'tag', 'get']); @@ -125,13 +133,27 @@ class TwigServiceProviderTest extends ServiceProviderTest ['renderer.twigEngine', $twigEngine] ); - $app->expects($this->once()) + $app->expects($this->exactly(2)) ->method('get') - ->with('path.views') - ->willReturn($viewsPath); + ->withConsecutive(['path.views'], ['config']) + ->willReturnOnConsecutiveCalls($viewsPath, $config); $this->setExpects($app, 'tag', ['renderer.twigEngine', ['renderer.engine']]); + $config->expects($this->once()) + ->method('get') + ->with('timezone') + ->willReturn('The/World'); + + $twig->expects($this->once()) + ->method('getExtension') + ->with(TwigCore::class) + ->willReturn($twigCore); + + $twigCore->expects($this->once()) + ->method('setTimezone') + ->with('The/World'); + $serviceProvider = new TwigServiceProvider($app); $this->setExtensionsTo($serviceProvider, []); |