diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-23 19:13:19 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-23 20:11:37 +0200 |
commit | 66038eda14d5d4e624b6636a6156570e3e940e49 (patch) | |
tree | 6e4b7557b7d91786ef47f22f7ddef85eed1dfb42 /tests/Unit | |
parent | 590adffa9316b98544cb8d67b03b80e44ba9c8b7 (diff) | |
parent | 9d34f371cb9c5ab0d60bd3158678b9cc9da6cc80 (diff) |
Merge branch 'twig-templates'
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/Controllers/CreditsControllerTest.php | 28 | ||||
-rw-r--r-- | tests/Unit/Http/RequestServiceProviderTest.php | 10 | ||||
-rw-r--r-- | tests/Unit/Http/ResponseServiceProviderTest.php | 9 | ||||
-rw-r--r-- | tests/Unit/Middleware/RequestHandlerTest.php | 77 | ||||
-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 |
9 files changed, 298 insertions, 15 deletions
diff --git a/tests/Unit/Controllers/CreditsControllerTest.php b/tests/Unit/Controllers/CreditsControllerTest.php new file mode 100644 index 00000000..6f0200f2 --- /dev/null +++ b/tests/Unit/Controllers/CreditsControllerTest.php @@ -0,0 +1,28 @@ +<?php + +namespace Unit\Controllers; + +use Engelsystem\Controllers\CreditsController; +use Engelsystem\Http\Response; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; + +class CreditsControllerTest extends TestCase +{ + /** + * @covers \Engelsystem\Controllers\CreditsController::__construct + * @covers \Engelsystem\Controllers\CreditsController::index + */ + public function testIndex() + { + /** @var Response|MockObject $response */ + $response = $this->createMock(Response::class); + + $response->expects($this->once()) + ->method('withView') + ->with('pages/credits.twig'); + + $controller = new CreditsController($response); + $controller->index(); + } +} diff --git a/tests/Unit/Http/RequestServiceProviderTest.php b/tests/Unit/Http/RequestServiceProviderTest.php index eddf7ee5..4e9bb4e0 100644 --- a/tests/Unit/Http/RequestServiceProviderTest.php +++ b/tests/Unit/Http/RequestServiceProviderTest.php @@ -8,6 +8,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 { @@ -45,9 +46,16 @@ class RequestServiceProviderTest extends ServiceProviderTest $this->setExpects($app, 'call', [[Request::class, 'createFromGlobals']], $request); $this->setExpects($app, 'get', ['config'], $config); - $this->setExpects($app, 'instance', ['request', $request]); $this->setExpects($config, 'get', ['trusted_proxies'], $configuredProxies); + $app->expects($this->exactly(3)) + ->method('instance') + ->withConsecutive( + [Request::class, $request], + [SymfonyRequest::class, $request], + ['request', $request] + ); + /** @var ServiceProvider|MockObject $serviceProvider */ $serviceProvider = $this->getMockBuilder(RequestServiceProvider::class) ->setConstructorArgs([$app]) 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/Middleware/RequestHandlerTest.php b/tests/Unit/Middleware/RequestHandlerTest.php index 896b55c3..cb5fc4a6 100644 --- a/tests/Unit/Middleware/RequestHandlerTest.php +++ b/tests/Unit/Middleware/RequestHandlerTest.php @@ -38,15 +38,12 @@ class RequestHandlerTest extends TestCase public function testProcess() { /** @var Application|MockObject $container */ - $container = $this->createMock(Application::class); /** @var ServerRequestInterface|MockObject $request */ - $request = $this->getMockForAbstractClass(ServerRequestInterface::class); /** @var RequestHandlerInterface|MockObject $handler */ - $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class); /** @var ResponseInterface|MockObject $response */ - $response = $this->getMockForAbstractClass(ResponseInterface::class); + /** @var MiddlewareInterface|MockObject $middlewareInterface */ + list($container, $request, $handler, $response, $middlewareInterface) = $this->getMocks(); - $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class); $requestHandlerInterface = $this->getMockForAbstractClass(RequestHandlerInterface::class); $request->expects($this->exactly(3)) @@ -57,10 +54,10 @@ class RequestHandlerTest extends TestCase /** @var RequestHandler|MockObject $middleware */ $middleware = $this->getMockBuilder(RequestHandler::class) ->setConstructorArgs([$container]) - ->setMethods(['resolveMiddleware']) + ->setMethods(['resolveRequestHandler']) ->getMock(); $middleware->expects($this->exactly(3)) - ->method('resolveMiddleware') + ->method('resolveRequestHandler') ->with('FooBarClass') ->willReturnOnConsecutiveCalls( $middlewareInterface, @@ -86,4 +83,70 @@ class RequestHandlerTest extends TestCase $this->expectException(InvalidArgumentException::class); $middleware->process($request, $handler); } + + /** + * @covers \Engelsystem\Middleware\RequestHandler::resolveRequestHandler + */ + public function testResolveRequestHandler() + { + /** @var Application|MockObject $container */ + /** @var ServerRequestInterface|MockObject $request */ + /** @var RequestHandlerInterface|MockObject $handler */ + /** @var ResponseInterface|MockObject $response */ + /** @var MiddlewareInterface|MockObject $middlewareInterface */ + list($container, $request, $handler, $response, $middlewareInterface) = $this->getMocks(); + + $className = 'Engelsystem\\Controllers\\FooBarTestController'; + + $request->expects($this->exactly(1)) + ->method('getAttribute') + ->with('route-request-handler') + ->willReturn('FooBarTestController@showStuff'); + + /** @var RequestHandler|MockObject $middleware */ + $middleware = $this->getMockBuilder(RequestHandler::class) + ->setConstructorArgs([$container]) + ->setMethods(['resolveMiddleware']) + ->getMock(); + $middleware->expects($this->once()) + ->method('resolveMiddleware') + ->with([$middlewareInterface, 'showStuff']) + ->willReturn($middlewareInterface); + + $middlewareInterface->expects($this->once()) + ->method('process') + ->with($request, $handler) + ->willReturn($response); + + $container->expects($this->exactly(2)) + ->method('has') + ->withConsecutive(['FooBarTestController'], [$className]) + ->willReturnOnConsecutiveCalls(false, true); + $container->expects($this->once()) + ->method('make') + ->with($className) + ->willReturn($middlewareInterface); + + $return = $middleware->process($request, $handler); + $this->assertEquals($return, $response); + } + + /** + * @return array + */ + protected function getMocks(): array + { + /** @var Application|MockObject $container */ + $container = $this->createMock(Application::class); + /** @var ServerRequestInterface|MockObject $request */ + $request = $this->getMockForAbstractClass(ServerRequestInterface::class); + /** @var RequestHandlerInterface|MockObject $handler */ + $handler = $this->getMockForAbstractClass(RequestHandlerInterface::class); + /** @var ResponseInterface|MockObject $response */ + $response = $this->getMockForAbstractClass(ResponseInterface::class); + /** @var MiddlewareInterface $middlewareInterface */ + $middlewareInterface = $this->getMockForAbstractClass(MiddlewareInterface::class); + + return [$container, $request, $handler, $response, $middlewareInterface]; + } } 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, []); |