Skip to content

Commit 7d98d45

Browse files
committed
add more tests
1 parent b800053 commit 7d98d45

1 file changed

Lines changed: 56 additions & 10 deletions

File tree

tests/ExpressionParserTest.php

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,6 @@ public function testStringExpression($template, $expected)
244244
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode('0')->getNode('expr'));
245245
}
246246

247-
public function testNullSafeOperator()
248-
{
249-
$env = new Environment(new ArrayLoader([
250-
'template' => '{{ foo?.bar }}',
251-
]));
252-
253-
$this->assertSame('baz', $env->render('template', ['foo' => (object) ['bar' => 'baz']]));
254-
$this->assertSame('', $env->render('template', ['foo' => null]));
255-
}
256-
257247
public static function getTestsForString()
258248
{
259249
return [
@@ -298,6 +288,62 @@ public static function getTestsForString()
298288
];
299289
}
300290

291+
/**
292+
* @dataProvider getTestsForNullSafeOperator
293+
*/
294+
public function testNullSafeOperator($template, $data, $expected)
295+
{
296+
$env = new Environment(new ArrayLoader(['template' => $template]));
297+
298+
$this->assertSame($expected, $env->render('template', $data));
299+
}
300+
301+
public static function getTestsForNullSafeOperator()
302+
{
303+
return [
304+
[
305+
'{{ foo?.bar }}',
306+
['foo' => (object)['bar' => 'baz']],
307+
'baz',
308+
],
309+
[
310+
'{{ foo?.bar }}',
311+
['foo' => null],
312+
'',
313+
],
314+
[
315+
'{{ foo?.bar?.baz }}',
316+
['foo' => (object)['bar' => (object)['baz' => 'qux']]],
317+
'qux',
318+
],
319+
[
320+
'{{ foo?.bar?.baz }}',
321+
['foo' => (object)['bar' => null]],
322+
'',
323+
],
324+
[
325+
'{{ foo?.bar?.baz }}',
326+
['foo' => null],
327+
'',
328+
],
329+
[
330+
'{{ foo?.bar?.baz ?? "qux" }}',
331+
['foo' => null],
332+
'qux',
333+
],
334+
[
335+
'{{ foo?.bar ?? "qux" }}',
336+
['foo' => (object)['bar' => 0]],
337+
'0',
338+
],
339+
[
340+
'{{ foo?.bar ?? "qux" }}',
341+
['foo' => (object)['bar' => false]],
342+
'',
343+
],
344+
];
345+
}
346+
301347
public function testMacroDefinitionDoesNotSupportNonNameVariableName()
302348
{
303349
$env = new Environment(new ArrayLoader(), ['cache' => false, 'autoescape' => false]);

0 commit comments

Comments
 (0)