1<?php declare(strict_types=1); 2 3namespace App\Tests\Unit\Template; 4 5use PHPUnit\Framework\TestCase; 6use App\Template\Engine; 7 8class EngineTest extends TestCase 9{ 10 /** @var Engine */ 11 private $template; 12 13 public function setUp(): void 14 { 15 $this->template = new Engine(TEST_FIXTURES_DIRECTORY . '/templates'); 16 } 17 18 public function testView(): void 19 { 20 $content = $this->template->render('pages/view.php', [ 21 'foo' => 'Lorem ipsum dolor sit amet.', 22 'sidebar' => 'PHP is a popular general-purpose scripting language that is especially suited to web development', 23 ]); 24 25 $this->assertRegexp('/Lorem ipsum dolor sit amet/', $content); 26 $this->assertRegexp('/PHP is a popular general-purpose/', $content); 27 } 28 29 public function testRegisterNew(): void 30 { 31 // Register callable function 32 $this->template->register('addAsterisks', function ($var) { 33 return '***'.$var.'***'; 34 }); 35 36 // Register callable object and method 37 $object = new class { 38 public $property; 39 40 public function doSomething($argument) {} 41 }; 42 $this->template->register('doSomething', [$object, 'doSomething']); 43 44 $content = $this->template->render('pages/add_function.php', [ 45 'foo' => 'Lorem ipsum dolor sit amet.', 46 ]); 47 48 $this->assertRegexp('/\*\*\*Lorem ipsum dolor sit amet\.\*\*\*/', $content); 49 } 50 51 public function testRegisterExisting(): void 52 { 53 $this->expectException(\Exception::class); 54 55 $this->template->register('noHtml', function ($var) { 56 return $var; 57 }); 58 } 59 60 public function testAssignments(): void 61 { 62 $this->template->assign([ 63 'parameter' => 'FooBarBaz', 64 ]); 65 66 $content = $this->template->render('pages/assignments.php', [ 67 'foo' => 'Lorem ipsum dolor sit amet.', 68 ]); 69 70 $this->assertRegexp('/Lorem ipsum dolor sit amet\./', $content); 71 $this->assertRegexp('/FooBarBaz/', $content); 72 } 73 74 public function testMerge(): void 75 { 76 $this->template->assign([ 77 'foo', 78 'bar', 79 'qux' => 'quux', 80 ]); 81 82 $this->template->assign([ 83 'baz', 84 'qux' => 'quuz', 85 ]); 86 87 $this->assertEquals(['baz', 'bar', 'qux' => 'quuz'], $this->template->getVariables()); 88 } 89 90 public function testVariablesScope(): void 91 { 92 $this->template->assign([ 93 'parameter' => 'Parameter value', 94 ]); 95 96 $content = $this->template->render('pages/invalid_variables.php', [ 97 'foo' => 'Lorem ipsum dolor sit amet', 98 ]); 99 100 $expected = var_export([ 101 'parameter' => 'Parameter value', 102 'foo' => 'Lorem ipsum dolor sit amet', 103 ], true); 104 105 $this->assertEquals($expected, $content); 106 } 107 108 public function testInvalidVariables(): void 109 { 110 $this->template->assign([ 111 'Invalid value with key 0', 112 'parameter' => 'Parameter value', 113 'Invalid value with key 1', 114 ]); 115 116 $this->expectException(\Exception::class); 117 118 $this->template->render('pages/invalid_variables.php', [ 119 'foo' => 'Lorem ipsum dolor sit amet', 120 1 => 'Invalid overridden value with key 1', 121 ]); 122 } 123 124 public function testOverrides(): void 125 { 126 $this->template->assign([ 127 'pageParameter_1' => 'Page parameter 1', 128 'pageParameter_2' => 'Page parameter 2', 129 'layoutParameter_1' => 'Layout parameter 1', 130 'layoutParameter_2' => 'Layout parameter 2', 131 'layoutParameter_3' => 'Layout parameter 3', 132 ]); 133 134 $content = $this->template->render('pages/overrides.php', [ 135 'pageParameter_2' => 'Overridden parameter 2', 136 'layoutParameter_2' => 'Layout overridden parameter 2', 137 ]); 138 139 $this->assertRegexp('/Page parameter 1/', $content); 140 $this->assertRegexp('/^((?!Page parameter 2).)*$/s', $content); 141 $this->assertRegexp('/Overridden parameter 2/', $content); 142 $this->assertRegexp('/Layout parameter 1/', $content); 143 $this->assertRegexp('/^((?!Layout parameter 2).)*$/s', $content); 144 $this->assertRegexp('/Layout overridden parameter 2/', $content); 145 } 146 147 public function testAppending(): void 148 { 149 $content = $this->template->render('pages/appending.php'); 150 151 $this->assertRegexp('/file\_1\.js/', $content); 152 $this->assertRegexp('/file\_2\.js/', $content); 153 } 154 155 public function testIncluding(): void 156 { 157 $content = $this->template->render('pages/including.php'); 158 159 $this->assertRegexp('/\<form method\=\"post\"\>/', $content); 160 $this->assertRegexp('/Banner inclusion/', $content); 161 } 162 163 public function testNoLayout(): void 164 { 165 $content = $this->template->render('pages/no_layout.rss'); 166 167 $this->assertEquals(file_get_contents(TEST_FIXTURES_DIRECTORY . '/templates/pages/no_layout.rss'), $content); 168 } 169 170 public function testMissingTemplate(): void 171 { 172 $this->template->assign([ 173 'parameter' => 'Parameter value', 174 ]); 175 176 $this->expectException(\Exception::class); 177 178 $this->template->render('pages/this/does/not/exist.php', [ 179 'foo' => 'Lorem ipsum dolor sit amet', 180 ]); 181 } 182 183 public function testExtending(): void 184 { 185 $this->expectException(\Exception::class); 186 187 $this->template->render('pages/extends.php'); 188 } 189} 190