Added Renderer unit test
This commit is contained in:
parent
2cb636b651
commit
1e267ce3b1
|
@ -29,6 +29,6 @@ class HtmlEngine implements EngineInterface
|
|||
*/
|
||||
public function canRender($path)
|
||||
{
|
||||
return strpos($path, '.html') && file_exists($path);
|
||||
return strpos($path, '.htm') && file_exists($path);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,12 @@
|
|||
|
||||
namespace Engelsystem\Renderer;
|
||||
|
||||
use Psr\Log\LoggerAwareTrait;
|
||||
|
||||
class Renderer
|
||||
{
|
||||
use LoggerAwareTrait;
|
||||
|
||||
/** @var EngineInterface[] */
|
||||
protected $renderer = [];
|
||||
|
||||
|
@ -24,7 +28,10 @@ class Renderer
|
|||
return $renderer->get($template, $data);
|
||||
}
|
||||
|
||||
engelsystem_error('Unable to find a renderer for template file "' . $template . '".');
|
||||
if ($this->logger) {
|
||||
$this->logger->error('Unable to find a renderer for template file "{file}"', ['file' => $template]);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Config;
|
||||
|
||||
use Engelsystem\Renderer\EngineInterface;
|
||||
use Engelsystem\Renderer\Renderer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class RendererTest extends TestCase
|
||||
{
|
||||
public function testGet()
|
||||
{
|
||||
$renderer = new Renderer();
|
||||
|
||||
$nullRenderer = $this->getMockForAbstractClass(EngineInterface::class);
|
||||
|
||||
$nullRenderer->expects($this->atLeastOnce())
|
||||
->method('canRender')
|
||||
->willReturn(false);
|
||||
$renderer->addRenderer($nullRenderer);
|
||||
|
||||
$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();
|
||||
|
||||
$loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
|
||||
$loggerMock
|
||||
->expects($this->once())
|
||||
->method('error');
|
||||
|
||||
$renderer->setLogger($loggerMock);
|
||||
|
||||
$data = $renderer->render('testing.template');
|
||||
$this->assertEquals('', $data);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue