summaryrefslogtreecommitdiff
path: root/tests/Unit/Renderer
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Unit/Renderer')
-rw-r--r--tests/Unit/Renderer/TwigEngineTest.php60
-rw-r--r--tests/Unit/Renderer/TwigLoaderTest.php31
-rw-r--r--tests/Unit/Renderer/TwigServiceProviderTest.php63
3 files changed, 154 insertions, 0 deletions
diff --git a/tests/Unit/Renderer/TwigEngineTest.php b/tests/Unit/Renderer/TwigEngineTest.php
new file mode 100644
index 00000000..9d0618f1
--- /dev/null
+++ b/tests/Unit/Renderer/TwigEngineTest.php
@@ -0,0 +1,60 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer;
+
+use Engelsystem\Renderer\TwigEngine;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Twig_Environment as Twig;
+use Twig_LoaderInterface as LoaderInterface;
+
+class TwigEngineTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Renderer\TwigEngine::__construct
+ * @covers \Engelsystem\Renderer\TwigEngine::get
+ */
+ public function testGet()
+ {
+ /** @var Twig|MockObject $twig */
+ $twig = $this->createMock(Twig::class);
+
+ $path = 'foo.twig';
+ $data = ['lorem' => 'ipsum'];
+
+ $twig->expects($this->once())
+ ->method('render')
+ ->with($path, $data)
+ ->willReturn('LoremIpsum!');
+
+ $engine = new TwigEngine($twig);
+ $return = $engine->get($path, $data);
+ $this->assertEquals('LoremIpsum!', $return);
+ }
+
+
+ /**
+ * @covers \Engelsystem\Renderer\TwigEngine::canRender
+ */
+ public function testCanRender()
+ {
+ /** @var Twig|MockObject $twig */
+ $twig = $this->createMock(Twig::class);
+ /** @var LoaderInterface|MockObject $loader */
+ $loader = $this->getMockForAbstractClass(LoaderInterface::class);
+
+ $path = 'foo.twig';
+
+ $twig->expects($this->once())
+ ->method('getLoader')
+ ->willReturn($loader);
+ $loader->expects($this->once())
+ ->method('exists')
+ ->with($path)
+ ->willReturn(true);
+
+ $engine = new TwigEngine($twig);
+ $return = $engine->canRender($path);
+ $this->assertTrue($return);
+ }
+}
diff --git a/tests/Unit/Renderer/TwigLoaderTest.php b/tests/Unit/Renderer/TwigLoaderTest.php
new file mode 100644
index 00000000..e6867643
--- /dev/null
+++ b/tests/Unit/Renderer/TwigLoaderTest.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer;
+
+use Engelsystem\Renderer\TwigLoader;
+use PHPUnit\Framework\TestCase;
+use ReflectionClass as Reflection;
+
+class TwigLoaderTest extends TestCase
+{
+ /**
+ * @covers \Engelsystem\Renderer\TwigLoader::findTemplate
+ */
+ public function testFindTemplate()
+ {
+ $loader = new TwigLoader();
+
+ $reflection = new Reflection(get_class($loader));
+ $property = $reflection->getProperty('cache');
+ $property->setAccessible(true);
+
+ $realPath = __DIR__ . '/Stub/foo.twig';
+ $property->setValue($loader, ['Stub/foo.twig' => $realPath]);
+
+ $return = $loader->findTemplate('Stub/foo.twig');
+ $this->assertEquals($realPath, $return);
+
+ $return = $loader->findTemplate('Stub/foo');
+ $this->assertEquals($realPath, $return);
+ }
+}
diff --git a/tests/Unit/Renderer/TwigServiceProviderTest.php b/tests/Unit/Renderer/TwigServiceProviderTest.php
new file mode 100644
index 00000000..ede6fae4
--- /dev/null
+++ b/tests/Unit/Renderer/TwigServiceProviderTest.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Engelsystem\Test\Unit\Renderer;
+
+use Engelsystem\Renderer\TwigEngine;
+use Engelsystem\Renderer\TwigLoader;
+use Engelsystem\Renderer\TwigServiceProvider;
+use Engelsystem\Test\Unit\ServiceProviderTest;
+use PHPUnit\Framework\MockObject\MockObject;
+use Twig_Environment as Twig;
+use Twig_LoaderInterface as TwigLoaderInterface;
+
+class TwigServiceProviderTest extends ServiceProviderTest
+{
+ /**
+ * @covers \Engelsystem\Renderer\TwigServiceProvider::register
+ * @covers \Engelsystem\Renderer\TwigServiceProvider::registerTwigEngine
+ */
+ public function testRegister()
+ {
+ /** @var TwigEngine|MockObject $htmlEngine */
+ $twigEngine = $this->createMock(TwigEngine::class);
+ /** @var TwigLoader|MockObject $twigLoader */
+ $twigLoader = $this->createMock(TwigLoader::class);
+ /** @var Twig|MockObject $twig */
+ $twig = $this->createMock(Twig::class);
+
+ $app = $this->getApp(['make', 'instance', 'tag', 'get']);
+
+ $viewsPath = __DIR__ . '/Stub';
+
+ $app->expects($this->exactly(3))
+ ->method('make')
+ ->withConsecutive(
+ [TwigLoader::class, ['paths' => $viewsPath]],
+ [Twig::class],
+ [TwigEngine::class]
+ )->willReturnOnConsecutiveCalls(
+ $twigLoader,
+ $twig,
+ $twigEngine
+ );
+
+ $app->expects($this->exactly(4))
+ ->method('instance')
+ ->withConsecutive(
+ [TwigLoader::class, $twigLoader],
+ [TwigLoaderInterface::class, $twigLoader],
+ [Twig::class, $twig],
+ ['renderer.twigEngine', $twigEngine]
+ );
+
+ $app->expects($this->once())
+ ->method('get')
+ ->with('path.views')
+ ->willReturn($viewsPath);
+
+ $this->setExpects($app, 'tag', ['renderer.twigEngine', ['renderer.engine']]);
+
+ $serviceProvider = new TwigServiceProvider($app);
+ $serviceProvider->register();
+ }
+}