Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ec092b6

Browse filesBrowse files
committed
add and improve tests
1 parent cedf138 commit ec092b6
Copy full SHA for ec092b6

File tree

Expand file treeCollapse file tree

5 files changed

+120
-17
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+120
-17
lines changed

‎core-bundle/tests/DependencyInjection/Compiler/TwigPathsPassTest.php

Copy file name to clipboardExpand all lines: core-bundle/tests/DependencyInjection/Compiler/TwigPathsPassTest.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@
1414

1515
use Contao\CoreBundle\DependencyInjection\Compiler\TwigPathsPass;
1616
use Contao\CoreBundle\Tests\TestCase;
17-
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
17+
use Contao\CoreBundle\Twig\Loader\FailTolerantFilesystemLoader;
1818
use Symfony\Component\DependencyInjection\ContainerBuilder;
1919
use Symfony\Component\DependencyInjection\Definition;
20-
use Twig\Loader\FilesystemLoader as BaseFilesystemLoader;
20+
use Twig\Loader\FilesystemLoader;
2121

2222
class TwigPathsPassTest extends TestCase
2323
{
2424
public function testRewiresAndAddsMethodCalls(): void
2525
{
2626
$container = new ContainerBuilder();
2727

28-
$baseLoader = (new Definition(BaseFilesystemLoader::class))
28+
$baseLoader = (new Definition(FilesystemLoader::class))
2929
->addMethodCall('addPath', ['path1', 'namespace1'])
3030
->addMethodCall('addPath', ['path2', 'namespace2'])
3131
->addMethodCall('foo')
3232
;
3333

34-
$loader = new Definition(ContaoFilesystemLoader::class);
34+
$loader = new Definition(FailTolerantFilesystemLoader::class);
3535

3636
$container->addDefinitions([
3737
'twig.loader.native_filesystem' => $baseLoader,
38-
ContaoFilesystemLoader::class => $loader,
38+
FailTolerantFilesystemLoader::class => $loader,
3939
]);
4040

4141
(new TwigPathsPass())->process($container);

‎core-bundle/tests/Fixtures/Twig/paths/1/1.html.twig

Copy file name to clipboardExpand all lines: core-bundle/tests/Fixtures/Twig/paths/1/1.html.twig
Whitespace-only changes.

‎core-bundle/tests/Fixtures/Twig/paths/2/2.html.twig

Copy file name to clipboardExpand all lines: core-bundle/tests/Fixtures/Twig/paths/2/2.html.twig
Whitespace-only changes.

‎core-bundle/tests/Twig/Inheritance/InheritanceTest.php

Copy file name to clipboardExpand all lines: core-bundle/tests/Twig/Inheritance/InheritanceTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private function getDemoEnvironment(array $bundlesMetadata = null): Environment
9797

9898
$loader = new ContaoFilesystemLoader(new NullAdapter(), $templateLocator, $projectDir);
9999

100-
$warmer = new ContaoFilesystemLoaderWarmer($loader, $templateLocator, $projectDir);
100+
$warmer = new ContaoFilesystemLoaderWarmer($loader, $templateLocator, $projectDir, 'prod');
101101
$warmer->warmUp('');
102102

103103
$environment = new Environment($loader);

‎core-bundle/tests/Twig/Loader/ContaoFilesystemLoaderTest.php

Copy file name to clipboardExpand all lines: core-bundle/tests/Twig/Loader/ContaoFilesystemLoaderTest.php
+114-11Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,127 @@
1515
use Contao\CoreBundle\Tests\TestCase;
1616
use Contao\CoreBundle\Twig\Loader\ContaoFilesystemLoader;
1717
use Contao\CoreBundle\Twig\Loader\TemplateLocator;
18+
use Symfony\Component\Cache\Adapter\AdapterInterface;
19+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1820
use Symfony\Component\Cache\Adapter\NullAdapter;
21+
use Twig\Error\LoaderError;
22+
use Webmozart\PathUtil\Path;
1923

2024
class ContaoFilesystemLoaderTest extends TestCase
2125
{
22-
public function testIgnoresInvalidPaths(): void
26+
public function testAddPath(): void
2327
{
24-
$loader = new ContaoFilesystemLoader(
25-
new NullAdapter(),
26-
$this->createMock(TemplateLocator::class),
27-
'/project/dir'
28-
);
28+
$loader = $this->getContaoFilesystemLoader();
29+
30+
$path1 = Path::canonicalize(__DIR__.'/../../Fixtures/Twig/paths/1');
31+
$path2 = Path::canonicalize(__DIR__.'/../../Fixtures/Twig/paths/2');
32+
33+
$loader->addPath($path1);
34+
$loader->addPath($path2, 'Contao');
35+
$loader->addPath($path1, 'Contao_Foo');
36+
37+
$this->assertTrue($loader->exists('@Contao/1.html.twig'));
38+
$this->assertTrue($loader->exists('@Contao/2.html.twig'));
39+
$this->assertTrue($loader->exists('@Contao_Foo/1.html.twig'));
40+
$this->assertFalse($loader->exists('@Contao_Foo/2.html.twig'));
41+
}
42+
43+
public function testPrependPath(): void
44+
{
45+
$loader = $this->getContaoFilesystemLoader();
46+
47+
$path1 = Path::canonicalize(__DIR__.'/../../Fixtures/Twig/paths/1');
48+
$path2 = Path::canonicalize(__DIR__.'/../../Fixtures/Twig/paths/2');
49+
50+
$loader->prependPath($path1);
51+
$loader->prependPath($path2, 'Contao');
52+
$loader->prependPath($path1, 'Contao_Foo');
53+
54+
$this->assertTrue($loader->exists('@Contao/1.html.twig'));
55+
$this->assertTrue($loader->exists('@Contao/2.html.twig'));
56+
$this->assertTrue($loader->exists('@Contao_Foo/1.html.twig'));
57+
$this->assertFalse($loader->exists('@Contao_Foo/2.html.twig'));
58+
}
59+
60+
public function testDoesNotAllowToAddNonContaoNamespacedPath(): void
61+
{
62+
$loader = $this->getContaoFilesystemLoader();
63+
64+
$this->expectException(LoaderError::class);
65+
$this->expectExceptionMessage("Tried to register an invalid Contao namespace 'Foo'.");
66+
67+
$loader->addPath('foo/path', 'Foo');
68+
}
69+
70+
public function testDoesNotAllowToPrependNonContaoNamespacedPath(): void
71+
{
72+
$loader = $this->getContaoFilesystemLoader();
73+
74+
$this->expectException(LoaderError::class);
75+
$this->expectExceptionMessage("Tried to register an invalid Contao namespace 'Foo'.");
76+
77+
$loader->prependPath('foo/path', 'Foo');
78+
}
79+
80+
public function testToleratesInvalidPaths(): void
81+
{
82+
$loader = $this->getContaoFilesystemLoader();
2983

30-
// Should be ignored
31-
$loader->addPath('non/existing/path', 'Namespace');
32-
$loader->prependPath('non/existing/path', 'Namespace');
84+
$loader->addPath('non/existing/path');
85+
$loader->prependPath('non/existing/path');
3386

34-
$this->assertFalse($loader->exists('non/existing/path'));
87+
$this->assertEmpty($loader->getPaths());
3588
}
3689

37-
// todo
90+
public function testClearPaths(): void
91+
{
92+
$loader = $this->getContaoFilesystemLoader();
93+
94+
$loader->addPath(Path::canonicalize(__DIR__.'/../../Fixtures/Twig/paths/1'));
95+
96+
$this->assertTrue($loader->exists('@Contao/1.html.twig'));
97+
98+
$loader->clear();
99+
100+
$this->assertFalse($loader->exists('@Contao/1.html.twig'));
101+
}
102+
103+
public function testPersistsAndRecallsPaths(): void
104+
{
105+
$path1 = Path::canonicalize(__DIR__.'/../../Fixtures/Twig/paths/1');
106+
$path2 = Path::canonicalize(__DIR__.'/../../Fixtures/Twig/paths/2');
107+
108+
$cacheAdapter = new ArrayAdapter();
109+
110+
$loader1 = $this->getContaoFilesystemLoader($cacheAdapter);
111+
112+
$loader1->addPath($path1);
113+
$loader1->addPath($path2, 'Contao_Foo');
114+
115+
// Persist
116+
$this->assertEmpty(array_filter($cacheAdapter->getValues()));
117+
118+
$loader1->persist();
119+
120+
$this->assertNotEmpty(array_filter($cacheAdapter->getValues()));
121+
122+
// Recall
123+
$loader2 = $this->getContaoFilesystemLoader($cacheAdapter);
124+
125+
$this->assertSame([$path1], $loader2->getPaths());
126+
$this->assertSame([$path2], $loader2->getPaths('Contao_Foo'));
127+
}
128+
129+
// todo:
130+
// - getCacheKey (with theme)
131+
// - getSourceContext (with theme)
132+
// - everything hierarchy related
133+
134+
private function getContaoFilesystemLoader(AdapterInterface $cacheAdapter = null): ContaoFilesystemLoader
135+
{
136+
return new ContaoFilesystemLoader(
137+
$cacheAdapter ?? new NullAdapter(),
138+
$this->createMock(TemplateLocator::class)
139+
);
140+
}
38141
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.