Skip to content

Commit df2c4aa

Browse files
committed
Add unit tests for EnvironmentBuilder caching
Tests cover: - Environment creation without caching (default) - Environment creation with cache directory - Debug extension is always enabled - Custom extensions are properly added - Context setting adds global variable - Auto-reload is enabled for development - setEnvironmentFactory replaces environment
1 parent 8de8eb7 commit df2c4aa

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\Twig;
15+
16+
use phpDocumentor\Guides\RenderContext;
17+
use phpDocumentor\Guides\Twig\Theme\ThemeManager;
18+
use PHPUnit\Framework\TestCase;
19+
use Twig\Environment;
20+
use Twig\Extension\DebugExtension;
21+
use Twig\Extension\ExtensionInterface;
22+
use Twig\Loader\FilesystemLoader;
23+
24+
use function glob;
25+
use function is_dir;
26+
use function rmdir;
27+
use function sys_get_temp_dir;
28+
use function uniqid;
29+
use function unlink;
30+
31+
final class EnvironmentBuilderTest extends TestCase
32+
{
33+
private ThemeManager $themeManager;
34+
private string $cacheDir;
35+
private string $fixtureDir;
36+
37+
protected function setUp(): void
38+
{
39+
$this->fixtureDir = __DIR__ . '/Theme/fixtures';
40+
$loader = new FilesystemLoader([], $this->fixtureDir);
41+
$this->themeManager = new ThemeManager($loader, []);
42+
$this->cacheDir = sys_get_temp_dir() . '/phpDocumentor-guides-twig-test-' . uniqid();
43+
}
44+
45+
protected function tearDown(): void
46+
{
47+
// Clean up cache directory
48+
if (!is_dir($this->cacheDir)) {
49+
return;
50+
}
51+
52+
$files = glob($this->cacheDir . '/*');
53+
if ($files !== false) {
54+
foreach ($files as $file) {
55+
unlink($file);
56+
}
57+
}
58+
59+
rmdir($this->cacheDir);
60+
}
61+
62+
public function testEnvironmentBuilderWithoutCaching(): void
63+
{
64+
$builder = new EnvironmentBuilder($this->themeManager);
65+
$environment = $builder->getTwigEnvironment();
66+
67+
self::assertFalse($environment->getCache());
68+
self::assertTrue($environment->isDebug());
69+
}
70+
71+
public function testEnvironmentBuilderWithCaching(): void
72+
{
73+
$builder = new EnvironmentBuilder($this->themeManager, [], $this->cacheDir);
74+
$environment = $builder->getTwigEnvironment();
75+
76+
self::assertSame($this->cacheDir, $environment->getCache());
77+
self::assertTrue($environment->isDebug());
78+
}
79+
80+
public function testDebugExtensionIsAlwaysAdded(): void
81+
{
82+
$builder = new EnvironmentBuilder($this->themeManager);
83+
$environment = $builder->getTwigEnvironment();
84+
85+
self::assertTrue($environment->hasExtension(DebugExtension::class));
86+
}
87+
88+
public function testCustomExtensionsAreAdded(): void
89+
{
90+
$extension = $this->createMock(ExtensionInterface::class);
91+
$extensionClass = $extension::class;
92+
93+
$builder = new EnvironmentBuilder($this->themeManager, [$extension]);
94+
$environment = $builder->getTwigEnvironment();
95+
96+
self::assertTrue($environment->hasExtension($extensionClass));
97+
}
98+
99+
public function testSetContextAddsGlobal(): void
100+
{
101+
$builder = new EnvironmentBuilder($this->themeManager);
102+
$context = $this->createMock(RenderContext::class);
103+
104+
$builder->setContext($context);
105+
$environment = $builder->getTwigEnvironment();
106+
107+
$globals = $environment->getGlobals();
108+
self::assertArrayHasKey('env', $globals);
109+
self::assertSame($context, $globals['env']);
110+
}
111+
112+
public function testAutoReloadIsEnabled(): void
113+
{
114+
$builder = new EnvironmentBuilder($this->themeManager, [], $this->cacheDir);
115+
$environment = $builder->getTwigEnvironment();
116+
117+
self::assertTrue($environment->isAutoReload());
118+
}
119+
120+
public function testSetEnvironmentFactory(): void
121+
{
122+
$builder = new EnvironmentBuilder($this->themeManager);
123+
$originalEnvironment = $builder->getTwigEnvironment();
124+
125+
$loader = new FilesystemLoader();
126+
$builder->setEnvironmentFactory(static fn () => new Environment($loader));
127+
128+
$newEnvironment = $builder->getTwigEnvironment();
129+
130+
self::assertNotSame($originalEnvironment, $newEnvironment);
131+
}
132+
}

0 commit comments

Comments
 (0)