Skip to content

Commit c849b16

Browse files
Support guard
1 parent 9ad7ba9 commit c849b16

4 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/Parser.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,28 @@ public function getTest(int $line): TwigTest
494494
// try 2-words tests
495495
$name = $name.' '.$this->getCurrentToken()->getValue();
496496

497-
if ($test = $this->env->getTest($name)) {
497+
try {
498+
$test = $this->env->getTest($name);
499+
} catch (SyntaxError $e) {
500+
if (!$this->shouldIgnoreUnknownTwigCallables()) {
501+
throw $e;
502+
}
503+
504+
$test = null;
505+
}
506+
if ($test) {
498507
$this->stream->next();
499508
}
500509
} else {
501-
$test = $this->env->getTest($name);
510+
try {
511+
$test = $this->env->getTest($name);
512+
} catch (SyntaxError $e) {
513+
if (!$this->shouldIgnoreUnknownTwigCallables()) {
514+
throw $e;
515+
}
516+
517+
$test = null;
518+
}
502519
}
503520

504521
if (!$test) {

src/Test/IntegrationTestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ protected function getUndefinedFunctionCallbacks(): array
101101
return [];
102102
}
103103

104+
/**
105+
* @return array<callable(string): (TwigTest|false)>
106+
*/
107+
protected function getUndefinedTestCallbacks(): array
108+
{
109+
return [];
110+
}
111+
104112
/**
105113
* @return array<callable(string): (TokenParserInterface|false)>
106114
*/

tests/Fixtures/tags/guard/throwing_handler.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
1414
{% else -%}
1515
The throwing_undefined_function function doesn't exist
1616
{% endguard %}
17+
18+
{% guard function throwing_undefined_test -%}
19+
NEVER
20+
{{ 'a' is throwing_undefined_test('b') }}
21+
{% else -%}
22+
The throwing_undefined_test function doesn't exist
23+
{% endguard %}
1724
--DATA--
1825
return []
1926
--EXPECT--
2027
The throwing_undefined_filter filter doesn't exist
2128

2229
The throwing_undefined_function function doesn't exist
30+
31+
The throwing_undefined_test function doesn't exist

tests/IntegrationTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ static function (string $name) {
7272
];
7373
}
7474

75+
protected function getUndefinedTestCallbacks(): array
76+
{
77+
return [
78+
static function (string $name) {
79+
if ('throwing_undefined_test' === $name) {
80+
throw new SyntaxError('This test is undefined in the tests.');
81+
}
82+
83+
return false;
84+
},
85+
];
86+
}
87+
7588
protected function getUndefinedTokenParserCallbacks(): array
7689
{
7790
return [

0 commit comments

Comments
 (0)