diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-26 02:54:52 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-29 23:46:11 +0200 |
commit | bb3d16d273bb3e4552e4869dd22cb2c2d81f5387 (patch) | |
tree | 7de41548a5bd3a56aead1d9d528aa93e058e973a /tests/Unit | |
parent | a1bc763a16ee8be109de5c9053fbc5eded53824e (diff) |
Added Twig template renderer, closes #338
Diffstat (limited to 'tests/Unit')
-rw-r--r-- | tests/Unit/ApplicationTest.php | 1 | ||||
-rw-r--r-- | tests/Unit/Renderer/TwigEngineTest.php | 60 | ||||
-rw-r--r-- | tests/Unit/Renderer/TwigLoaderTest.php | 31 | ||||
-rw-r--r-- | tests/Unit/Renderer/TwigServiceProviderTest.php | 63 |
4 files changed, 155 insertions, 0 deletions
diff --git a/tests/Unit/ApplicationTest.php b/tests/Unit/ApplicationTest.php index 866eb957..012226b2 100644 --- a/tests/Unit/ApplicationTest.php +++ b/tests/Unit/ApplicationTest.php @@ -48,6 +48,7 @@ class ApplicationTest extends TestCase $this->assertTrue($app->has('path')); $this->assertTrue($app->has('path.config')); $this->assertTrue($app->has('path.lang')); + $this->assertTrue($app->has('path.views')); $this->assertEquals(realpath('.'), $app->path()); $this->assertEquals(realpath('.') . '/config', $app->get('path.config')); 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(); + } +} |