diff options
Diffstat (limited to 'tests/Unit/Renderer/Twig/Extensions')
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/ConfigTest.php | 25 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php | 48 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php | 25 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/SessionTest.php | 25 | ||||
-rw-r--r-- | tests/Unit/Renderer/Twig/Extensions/UrlTest.php | 64 |
5 files changed, 187 insertions, 0 deletions
diff --git a/tests/Unit/Renderer/Twig/Extensions/ConfigTest.php b/tests/Unit/Renderer/Twig/Extensions/ConfigTest.php new file mode 100644 index 00000000..6a9cb78a --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/ConfigTest.php @@ -0,0 +1,25 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use Engelsystem\Config\Config as EngelsystemConfig; +use Engelsystem\Renderer\Twig\Extensions\Config; +use PHPUnit\Framework\MockObject\MockObject; + +class ConfigTest extends ExtensionTest +{ + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Config::__construct + * @covers \Engelsystem\Renderer\Twig\Extensions\Config::getFunctions + */ + public function testGetFunctions() + { + /** @var EngelsystemConfig|MockObject $config */ + $config = $this->createMock(EngelsystemConfig::class); + + $extension = new Config($config); + $functions = $extension->getFunctions(); + + $this->assertExtensionExists('config', [$config, 'get'], $functions); + } +} diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php new file mode 100644 index 00000000..10f2e69a --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php @@ -0,0 +1,48 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use PHPUnit\Framework\TestCase; +use Twig_Function as TwigFunction; + +abstract class ExtensionTest extends TestCase +{ + /** + * Assert that a twig function was registered + * + * @param string $name + * @param callable $callback + * @param TwigFunction[] $functions + */ + protected function assertExtensionExists($name, $callback, $functions) + { + foreach ($functions as $function) { + if ($function->getName() != $name) { + continue; + } + + $this->assertEquals($callback, $function->getCallable()); + return; + } + + $this->fail(sprintf('Function %s not found', $name)); + } + + /** + * Assert that a global exists + * + * @param string $name + * @param mixed $value + * @param mixed[] $globals + */ + protected function assertGlobalsExists($name, $value, $globals) + { + if (isset($globals[$name])) { + $this->assertArraySubset([$name => $value], $globals); + + return; + } + + $this->fail(sprintf('Global %s not found', $name)); + } +} diff --git a/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php new file mode 100644 index 00000000..6cc3a4da --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/GlobalsTest.php @@ -0,0 +1,25 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use Engelsystem\Renderer\Twig\Extensions\Globals; + +class GlobalsTest extends ExtensionTest +{ + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals + */ + public function testGetGlobals() + { + $extension = new Globals(); + $globals = $extension->getGlobals(); + + $this->assertGlobalsExists('user', [], $globals); + + global $user; + $user['foo'] = 'bar'; + + $globals = $extension->getGlobals(); + $this->assertGlobalsExists('user', ['foo' => 'bar'], $globals); + } +} diff --git a/tests/Unit/Renderer/Twig/Extensions/SessionTest.php b/tests/Unit/Renderer/Twig/Extensions/SessionTest.php new file mode 100644 index 00000000..7ce4dc3a --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/SessionTest.php @@ -0,0 +1,25 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use Engelsystem\Renderer\Twig\Extensions\Session; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\HttpFoundation\Session\Session as SymfonySession; + +class SessionTest extends ExtensionTest +{ + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Session::__construct + * @covers \Engelsystem\Renderer\Twig\Extensions\Session::getFunctions + */ + public function testGetGlobals() + { + /** @var SymfonySession|MockObject $session */ + $session = $this->createMock(SymfonySession::class); + + $extension = new Session($session); + $functions = $extension->getFunctions(); + + $this->assertExtensionExists('session_get', [$session, 'get'], $functions); + } +} diff --git a/tests/Unit/Renderer/Twig/Extensions/UrlTest.php b/tests/Unit/Renderer/Twig/Extensions/UrlTest.php new file mode 100644 index 00000000..c7e40bea --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/UrlTest.php @@ -0,0 +1,64 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions; + +use Engelsystem\Http\UrlGenerator; +use Engelsystem\Renderer\Twig\Extensions\Url; +use PHPUnit\Framework\MockObject\MockObject; + +class UrlTest extends ExtensionTest +{ + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Url::__construct + * @covers \Engelsystem\Renderer\Twig\Extensions\Url::getFunctions + */ + public function testGetGlobals() + { + /** @var UrlGenerator|MockObject $urlGenerator */ + $urlGenerator = $this->createMock(UrlGenerator::class); + + $extension = new Url($urlGenerator); + $functions = $extension->getFunctions(); + + $this->assertExtensionExists('url', [$extension, 'getUrl'], $functions); + } + + /** + * @return string[][] + */ + public function getUrls() + { + return [ + ['/', '/', 'http://foo.bar/'], + ['/foo', '/foo', 'http://foo.bar/foo'], + ['foo_bar', 'foo-bar', 'http://foo.bar/foo-bar'], + ['dolor', 'dolor', 'http://foo.bar/dolor?lorem_ipsum=dolor', ['lorem_ipsum' => 'dolor']], + ]; + } + + /** + * @dataProvider getUrls + * + * @param string $url + * @param string $return + * @param string $urlTo + * @param array $parameters + * + * @covers \Engelsystem\Renderer\Twig\Extensions\Url::getUrl + */ + public function testGetUrl($url, $urlTo, $return, $parameters = []) + { + /** @var UrlGenerator|MockObject $urlGenerator */ + $urlGenerator = $this->createMock(UrlGenerator::class); + + $urlGenerator->expects($this->once()) + ->method('to') + ->with($urlTo, $parameters) + ->willReturn($return); + + $extension = new Url($urlGenerator); + $generatedUrl = $extension->getUrl($url, $parameters); + + $this->assertEquals($return, $generatedUrl); + } +} |