Skip to content

Commit

Permalink
Add "data_table_theme" Twig node
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Jun 28, 2024
1 parent bb9ca6e commit 3de75ad
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Twig/DataTableThemeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function compile(Compiler $compiler): void
->subcompile($this->getNode('data_table'))
->raw(', ')
->subcompile($this->getNode('themes'))
->raw(sprintf(", %s);\n", $this->getAttribute('only') ? 'true' : 'false'));
->raw(sprintf(", %s);\n", $this->getAttribute('only') ? 'true' : 'false'))
;
}
}
2 changes: 1 addition & 1 deletion src/Twig/DataTableThemeTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ public function getTag(): string
{
return 'data_table_theme';
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Twig/DataTableExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ private function createExtension(): DataTableExtension
$this->createStub(FilterClearUrlGeneratorInterface::class),
);
}
}
}
30 changes: 21 additions & 9 deletions tests/Unit/Twig/DataTableThemeNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testConstructor()
}

#[DataProvider('provideCompileCases')]
public function testCompile(DataTableThemeNode $node, string $expected)
public function testCompile(DataTableThemeNode $node, array $expected)
{
$environment = new Environment($this->createMock(LoaderInterface::class));

Expand All @@ -45,7 +45,13 @@ public function testCompile(DataTableThemeNode $node, string $expected)

$compiler = new Compiler($environment);

$this->assertEquals($expected, trim($compiler->compile($node)->getSource()));
// In Twig 2, the ["foo", "bar"] is parsed with as [0 => "foo", 1 => "bar"].
// In Twig 3, the ["foo", "bar"] is parsed with as ["foo", "bar"].
// For compatibility, we are checking whether the compiled value is equal to one of the expected values.
$this->assertThat(trim($compiler->compile($node)->getSource()), $this->logicalOr(
$this->equalTo($expected[0]),
$this->equalTo($expected[1]),
));
}

public static function provideCompileCases(): iterable
Expand All @@ -60,8 +66,10 @@ public static function provideCompileCases(): iterable
0,
'data_table_theme',
false,
),
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), ["foo"], false);',
), [
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), ["foo"], false);',
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), [0 => "foo"], false);',
],
];

yield 'multiple themes without only' => [
Expand All @@ -76,8 +84,10 @@ public static function provideCompileCases(): iterable
0,
'data_table_theme',
false,
),
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), ["foo", "bar"], false);',
), [
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), ["foo", "bar"], false);',
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), [0 => "foo", 1 => "bar"], false);',
],
];

yield 'multiple themes with only' => [
Expand All @@ -92,8 +102,10 @@ public static function provideCompileCases(): iterable
0,
'data_table_theme',
true,
),
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), ["foo", "bar"], true);',
), [
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), ["foo", "bar"], true);',
'$this->env->getExtension("Kreyu\\Bundle\\DataTableBundle\\Twig\\DataTableExtension")->setDataTableThemes(($context["data_table"] ?? null), [0 => "foo", 1 => "bar"], true);',
],
];
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Twig/DataTableThemeTokenParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ public static function provideCompileCases(): iterable
),
];
}
}
}

0 comments on commit 3de75ad

Please sign in to comment.