Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de4f44c

Browse files
committed
improve coverage
1 parent bf862f5 commit de4f44c

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

core-bundle/src/Twig/Extension/ContaoExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,6 @@ private function getTwigIncludeFunction(): TwigFunction
168168
}
169169
}
170170

171-
throw new \RuntimeException(sprintf('The %s class was expected to register the "include" Twig function but did\'nt.', CoreExtension::class));
171+
throw new \RuntimeException(sprintf('The %s class was expected to register the "include" Twig function but did not.', CoreExtension::class));
172172
}
173173
}

core-bundle/src/Twig/Inheritance/DynamicExtendsTokenParser.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ final class DynamicExtendsTokenParser extends AbstractTokenParser
3131
*/
3232
private $hierarchy;
3333

34-
/**
35-
* Track use of inherited templates to prevent endless loops.
36-
*
37-
* @var array<string,array<string>>
38-
*/
39-
private $inheritanceChains = [];
40-
4134
public function __construct(TemplateHierarchyInterface $hierarchy)
4235
{
4336
$this->hierarchy = $hierarchy;
@@ -48,11 +41,11 @@ public function parse(Token $token): Node
4841
$stream = $this->parser->getStream();
4942

5043
if ($this->parser->peekBlockStack()) {
51-
throw new SyntaxError('Cannot use "extend" in a block.', $token->getLine(), $stream->getSourceContext());
44+
throw new SyntaxError('Cannot use "extends" in a block.', $token->getLine(), $stream->getSourceContext());
5245
}
5346

5447
if (!$this->parser->isMainScope()) {
55-
throw new SyntaxError('Cannot use "extend" in a macro.', $token->getLine(), $stream->getSourceContext());
48+
throw new SyntaxError('Cannot use "extends" in a macro.', $token->getLine(), $stream->getSourceContext());
5649
}
5750

5851
if (null !== $this->parser->getParent()) {
@@ -87,15 +80,6 @@ function (Node $node) use ($stream): void {
8780
$sourcePath = $stream->getSourceContext()->getPath();
8881
$parentName = $this->hierarchy->getDynamicParent($shortName, $sourcePath);
8982

90-
// Detect loops
91-
if (\in_array($parentName, $this->inheritanceChains[$shortName] ?? [], true)) {
92-
$chain = implode(' -> ', $this->inheritanceChains[$shortName]);
93-
94-
throw new \LogicException("Loop detected when extending '$parentName': $chain -> &0");
95-
}
96-
97-
$this->inheritanceChains[$shortName][] = $parentName;
98-
9983
// Adjust parent template according to the template hierarchy
10084
$node->setAttribute('value', $parentName);
10185
}

core-bundle/tests/Twig/Extension/ContaoExtensionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Contao\System;
2323
use PHPUnit\Framework\MockObject\MockObject;
2424
use Twig\Environment;
25+
use Twig\Extension\AbstractExtension;
2526
use Twig\Extension\CoreExtension;
2627
use Twig\Extension\EscaperExtension;
2728
use Twig\Node\Expression\ConstantExpression;
@@ -93,6 +94,29 @@ public function testIncludeFunctionDelegatesToTwigInclude(): void
9394
($includeFunction->getCallable())(...$args);
9495
}
9596

97+
public function testThrowsIfCoreIncludeFunctionIsNotFound(): void
98+
{
99+
$environment = $this->createMock(Environment::class);
100+
$environment
101+
->method('getExtension')
102+
->willReturnMap([
103+
[EscaperExtension::class, new EscaperExtension()],
104+
[CoreExtension::class, new class() extends AbstractExtension {
105+
}],
106+
])
107+
;
108+
109+
$extension = new ContaoExtension(
110+
$environment,
111+
$this->createMock(TemplateHierarchyInterface::class)
112+
);
113+
114+
$this->expectException(\RuntimeException::class);
115+
$this->expectExceptionMessage('The Twig\Extension\CoreExtension class was expected to register the "include" Twig function but did not.');
116+
117+
$extension->getFunctions();
118+
}
119+
96120
public function testAllowsOnTheFlyRegisteringTemplatesForInputEncoding(): void
97121
{
98122
$contaoExtension = $this->getContaoExtension();
@@ -131,6 +155,10 @@ public function testAllowsOnTheFlyRegisteringTemplatesForInputEncoding(): void
131155

132156
// Add rule that allows the template and traverse tree a second time (change expected)
133157
$contaoExtension->addContaoEscaperRule('/foo\.html\.twig/');
158+
159+
// Adding the same rule should be ignored
160+
$contaoExtension->addContaoEscaperRule('/foo\.html\.twig/');
161+
134162
$traverser->traverse($node);
135163
$iteration2 = $node->__toString();
136164

core-bundle/tests/Twig/Inheritance/DynamicExtendsTokenParserTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Contao\CoreBundle\Twig\Inheritance\DynamicExtendsTokenParser;
1717
use Contao\CoreBundle\Twig\Inheritance\TemplateHierarchyInterface;
1818
use Twig\Environment;
19+
use Twig\Error\SyntaxError;
1920
use Twig\Lexer;
2021
use Twig\Loader\LoaderInterface;
2122
use Twig\Parser;
@@ -71,4 +72,47 @@ public function provideSources(): \Generator
7172
'<parent>',
7273
];
7374
}
75+
76+
/**
77+
* @dataProvider provideSourcesWithErrors
78+
*/
79+
public function testValidatesTokenStream(string $code, string $expectedException): void
80+
{
81+
$environment = new Environment($this->createMock(LoaderInterface::class));
82+
$environment->addTokenParser(new DynamicExtendsTokenParser(
83+
$this->createMock(TemplateHierarchyInterface::class)
84+
));
85+
86+
$source = new Source(
87+
$code,
88+
'template.html.twig',
89+
'/path/to/the/template.html.twig'
90+
);
91+
92+
$tokenStream = (new Lexer($environment))->tokenize($source);
93+
$parser = new Parser($environment);
94+
95+
$this->expectException(SyntaxError::class);
96+
$this->expectExceptionMessage($expectedException);
97+
98+
$parser->parse($tokenStream);
99+
}
100+
101+
public function provideSourcesWithErrors(): \Generator
102+
{
103+
yield 'extend from within a block' => [
104+
"{% block b %}{% extends '@Foo/bar.html.twig' %}{% endblock %}",
105+
'Cannot use "extends" in a block.',
106+
];
107+
108+
yield 'extend from within macro' => [
109+
"{% macro m() %}{% extends '@Foo/bar.html.twig' %}{% endmacro %}",
110+
'Cannot use "extends" in a macro.',
111+
];
112+
113+
yield 'multiple extends' => [
114+
"{% extends '@Foo/bar1.html.twig' %}{% extends '@Foo/bar2.html.twig' %}",
115+
'Multiple extends tags are forbidden.',
116+
];
117+
}
74118
}

0 commit comments

Comments
 (0)