summaryrefslogtreecommitdiff
path: root/tests/Unit/Renderer/HtmlEngineTest.php
blob: f76e7528dfa9b40aa14f611713663863a0b9c716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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();
        $engine->share('shared_data', 'tester');

        $file = $this->createTempFile('<div>%main_content% is a %shared_data%</div>');

        $data = $engine->get($file, ['main_content' => 'Lorem ipsum dolor sit']);
        $this->assertEquals('<div>Lorem ipsum dolor sit is a tester</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;
    }

    /**
     * Remove files
     */
    protected function tearDown(): void
    {
        foreach ($this->tmpFileNames as $fileName) {
            unlink($fileName);
        }
    }
}