diff options
author | msquare <msquare@notrademark.de> | 2017-11-28 15:43:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-28 15:43:51 +0100 |
commit | 599f2fd264bfc7b1b6826fe206442806e317340f (patch) | |
tree | 50cf84d7d07d11bd65b45c2c17f37632f6cd8eff /tests/Unit/Renderer | |
parent | a5fc5bd0979e8de1fce8a8addd351a6e7bd6aeb8 (diff) | |
parent | eda7f7788ea8012bd8be46405c56a666c11f3fa5 (diff) |
Merge pull request #365 from engelsystem/feature-igel-rewrite
Feature igel rewrite
Diffstat (limited to 'tests/Unit/Renderer')
-rw-r--r-- | tests/Unit/Renderer/HtmlEngineTest.php | 67 | ||||
-rw-r--r-- | tests/Unit/Renderer/RendererServiceProviderTest.php | 81 | ||||
-rw-r--r-- | tests/Unit/Renderer/RendererTest.php | 59 |
3 files changed, 207 insertions, 0 deletions
diff --git a/tests/Unit/Renderer/HtmlEngineTest.php b/tests/Unit/Renderer/HtmlEngineTest.php new file mode 100644 index 00000000..8c262932 --- /dev/null +++ b/tests/Unit/Renderer/HtmlEngineTest.php @@ -0,0 +1,67 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer; + +use Engelsystem\Renderer\HtmlEngine; +use PHPUnit\Framework\TestCase; + +class HtmlEngineTest extends TestCase +{ + /** @var string[] */ + protected $tmpFileNames = []; + + /** + * @covers \Engelsystem\Renderer\HtmlEngine::get + */ + public function testGet() + { + $engine = new HtmlEngine(); + + $file = $this->createTempFile('<div>%main_content%</div>'); + + $data = $engine->get($file, ['main_content' => 'Lorem ipsum dolor sit']); + $this->assertEquals('<div>Lorem ipsum dolor sit</div>', $data); + } + + /** + * @covers \Engelsystem\Renderer\HtmlEngine::canRender + */ + public function testCanRender() + { + $engine = new HtmlEngine(); + + $this->assertFalse($engine->canRender('/dev/null')); + + $file = $this->createTempFile(); + $this->assertTrue($engine->canRender($file)); + + $htmFile = $this->createTempFile('', '.htm'); + $this->assertTrue($engine->canRender($htmFile)); + } + + /** + * @param string $content + * @param string $extension + * @return string + */ + protected function createTempFile($content = '', $extension = '.html') + { + $tmpFileName = tempnam(sys_get_temp_dir(), 'EngelsystemUnitTest'); + + $fileName = $tmpFileName . $extension; + rename($tmpFileName, $fileName); + + file_put_contents($fileName, $content); + + $this->tmpFileNames[] = $fileName; + + return $fileName; + } + + public function tearDown() + { + foreach ($this->tmpFileNames as $fileName) { + unlink($fileName); + } + } +} diff --git a/tests/Unit/Renderer/RendererServiceProviderTest.php b/tests/Unit/Renderer/RendererServiceProviderTest.php new file mode 100644 index 00000000..3826da7e --- /dev/null +++ b/tests/Unit/Renderer/RendererServiceProviderTest.php @@ -0,0 +1,81 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer; + +use Engelsystem\Renderer\EngineInterface; +use Engelsystem\Renderer\HtmlEngine; +use Engelsystem\Renderer\Renderer; +use Engelsystem\Renderer\RendererServiceProvider; +use Engelsystem\Test\Unit\ServiceProviderTest; +use PHPUnit_Framework_MockObject_MockObject; + +class RendererServiceProviderTest extends ServiceProviderTest +{ + /** + * @covers \Engelsystem\Renderer\RendererServiceProvider::register() + * @covers \Engelsystem\Renderer\RendererServiceProvider::registerRenderer() + * @covers \Engelsystem\Renderer\RendererServiceProvider::registerHtmlEngine() + */ + public function testRegister() + { + /** @var PHPUnit_Framework_MockObject_MockObject|Renderer $renderer */ + $renderer = $this->getMockBuilder(Renderer::class) + ->getMock(); + /** @var PHPUnit_Framework_MockObject_MockObject|HtmlEngine $htmlEngine */ + $htmlEngine = $this->getMockBuilder(HtmlEngine::class) + ->getMock(); + + $app = $this->getApp(['make', 'instance', 'tag']); + + $app->expects($this->exactly(2)) + ->method('make') + ->withConsecutive( + [Renderer::class], + [HtmlEngine::class] + )->willReturnOnConsecutiveCalls( + $renderer, + $htmlEngine + ); + + $app->expects($this->exactly(2)) + ->method('instance') + ->withConsecutive( + ['renderer', $renderer], + ['renderer.htmlEngine', $htmlEngine] + ); + + $this->setExpects($app, 'tag', ['renderer.htmlEngine', ['renderer.engine']]); + + $serviceProvider = new RendererServiceProvider($app); + $serviceProvider->register(); + } + + /** + * @covers \Engelsystem\Renderer\RendererServiceProvider::boot() + */ + public function testBoot() + { + /** @var PHPUnit_Framework_MockObject_MockObject|Renderer $renderer */ + $renderer = $this->getMockBuilder(Renderer::class) + ->getMock(); + /** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine1 */ + $engine1 = $this->getMockForAbstractClass(EngineInterface::class); + /** @var PHPUnit_Framework_MockObject_MockObject|EngineInterface $engine2 */ + $engine2 = $this->getMockForAbstractClass(EngineInterface::class); + + $app = $this->getApp(['get', 'tagged']); + + $engines = [$engine1, $engine2]; + + $this->setExpects($app, 'get', ['renderer'], $renderer); + $this->setExpects($app, 'tagged', ['renderer.engine'], $engines); + + $invocation = $renderer + ->expects($this->exactly(count($engines))) + ->method('addRenderer'); + call_user_func_array([$invocation, 'withConsecutive'], $engines); + + $serviceProvider = new RendererServiceProvider($app); + $serviceProvider->boot(); + } +} diff --git a/tests/Unit/Renderer/RendererTest.php b/tests/Unit/Renderer/RendererTest.php new file mode 100644 index 00000000..969ced7f --- /dev/null +++ b/tests/Unit/Renderer/RendererTest.php @@ -0,0 +1,59 @@ +<?php + +namespace Engelsystem\Test\Unit\Renderer; + +use Engelsystem\Renderer\EngineInterface; +use Engelsystem\Renderer\Renderer; +use PHPUnit\Framework\TestCase; +use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Psr\Log\LoggerInterface; + +class RendererTest extends TestCase +{ + public function testGet() + { + $renderer = new Renderer(); + + /** @var MockObject|EngineInterface $nullRenderer */ + $nullRenderer = $this->getMockForAbstractClass(EngineInterface::class); + + $nullRenderer->expects($this->atLeastOnce()) + ->method('canRender') + ->willReturn(false); + $renderer->addRenderer($nullRenderer); + + /** @var MockObject|EngineInterface $mockRenderer */ + $mockRenderer = $this->getMockForAbstractClass(EngineInterface::class); + + $mockRenderer->expects($this->atLeastOnce()) + ->method('canRender') + ->with('foo.template') + ->willReturn(true); + + $mockRenderer->expects($this->atLeastOnce()) + ->method('get') + ->with('foo.template', ['lorem' => 'ipsum']) + ->willReturn('Rendered content'); + + $renderer->addRenderer($mockRenderer); + $data = $renderer->render('foo.template', ['lorem' => 'ipsum']); + + $this->assertEquals('Rendered content', $data); + } + + public function testError() + { + $renderer = new Renderer(); + + /** @var MockObject|LoggerInterface $loggerMock */ + $loggerMock = $this->getMockForAbstractClass(LoggerInterface::class); + $loggerMock + ->expects($this->once()) + ->method('error'); + + $renderer->setLogger($loggerMock); + + $data = $renderer->render('testing.template'); + $this->assertEquals('', $data); + } +} |