3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
8 use PhpMyAdmin\Template
;
9 use PHPUnit\Framework\Attributes\CoversClass
;
10 use PHPUnit\Framework\Attributes\DataProvider
;
11 use ReflectionProperty
;
12 use Twig\Cache\CacheInterface
;
14 use Twig\Error\LoaderError
;
16 #[CoversClass(Template::class)]
17 class TemplateTest
extends AbstractTestCase
19 protected Template
$template;
22 * Sets up the fixture.
24 protected function setUp(): void
28 $this->template
= new Template();
32 * Test that Twig Environment can be built
33 * and that all Twig extensions are loaded
35 public function testGetTwigEnvironment(): void
37 $twig = Template
::getTwigEnvironment(null, false);
38 self
::assertFalse($twig->isDebug());
40 $twig = Template
::getTwigEnvironment(null, true);
41 self
::assertTrue($twig->isDebug());
45 * Test for set function
47 * @param string $data Template name
49 #[DataProvider('providerTestSet')]
50 public function testSet(string $data): void
52 $result = $this->template
->render($data, ['variable1' => 'value1', 'variable2' => 'value2']);
53 self
::assertStringContainsString('value1', $result);
54 self
::assertStringContainsString('value2', $result);
58 * Data provider for testSet
62 public static function providerTestSet(): array
64 return [['test/add_data']];
70 * @param string $templateFile Template name
71 * @param string $key Template variable array key
72 * @param string $value Template variable array value
74 #[DataProvider('providerTestDynamicRender')]
75 public function testDynamicRender(string $templateFile, string $key, string $value): void
79 $this->template
->render($templateFile, [$key => $value]),
84 * Data provider for testDynamicRender
88 public static function providerTestDynamicRender(): array
90 return [['test/echo', 'variable', 'value']];
96 public function testRenderTemplateNotFound(): void
98 $this->expectException(LoaderError
::class);
99 $this->template
->render('template not found');
105 * @param string $templateFile Template name
106 * @param string $expectedResult Expected result
108 #[DataProvider('providerTestRender')]
109 public function testRender(string $templateFile, string $expectedResult): void
113 $this->template
->render($templateFile),
118 * Data provider for testSet
122 public static function providerTestRender(): array
124 return [['test/static', "static content\n"]];
127 public function testLoadingTwigEnvOnlyOnce(): void
129 $twigEnvCacheProperty = new ReflectionProperty(Template
::class, 'twig');
130 $twigEnvCacheProperty->setValue(null, null);
131 $template = new Template();
132 self
::assertSame("static content\n", $template->render('test/static'));
133 $twigEnv = $twigEnvCacheProperty->getValue();
134 self
::assertInstanceOf(Environment
::class, $twigEnv);
135 $template2 = new Template();
136 self
::assertSame("static content\n", $template2->render('test/static'));
137 self
::assertSame($twigEnv, $twigEnvCacheProperty->getValue());
140 public function testDisableCache(): void
142 (new ReflectionProperty(Template
::class, 'twig'))->setValue(null, null);
143 $template = new Template(self
::createStub(Config
::class));
144 $template->disableCache();
145 $twig = (new ReflectionProperty(Template
::class, 'twig'))->getValue();
146 self
::assertInstanceOf(Environment
::class, $twig);
147 self
::assertFalse($twig->getCache());
148 $twig->setCache(self
::createStub(CacheInterface
::class));
149 self
::assertNotFalse($twig->getCache());
150 $template->disableCache();
151 self
::assertFalse($twig->getCache());
152 (new ReflectionProperty(Template
::class, 'twig'))->setValue(null, null);