From 3de75ad4b5c05a7bc3da9eb42b5a81e7238d5e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Wr=C3=B3blewski?= Date: Fri, 28 Jun 2024 13:14:42 +0200 Subject: [PATCH] Add "data_table_theme" Twig node --- src/Twig/DataTableThemeNode.php | 2 +- src/Twig/DataTableThemeTokenParser.php | 2 +- tests/Unit/Twig/DataTableExtensionTest.php | 2 +- tests/Unit/Twig/DataTableThemeNodeTest.php | 30 +++++++++++++------ .../Twig/DataTableThemeTokenParserTest.php | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/Twig/DataTableThemeNode.php b/src/Twig/DataTableThemeNode.php index ff30f8a4..fddfbc7f 100644 --- a/src/Twig/DataTableThemeNode.php +++ b/src/Twig/DataTableThemeNode.php @@ -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')) ; } } diff --git a/src/Twig/DataTableThemeTokenParser.php b/src/Twig/DataTableThemeTokenParser.php index ac061de4..eebfcaa3 100644 --- a/src/Twig/DataTableThemeTokenParser.php +++ b/src/Twig/DataTableThemeTokenParser.php @@ -43,4 +43,4 @@ public function getTag(): string { return 'data_table_theme'; } -} \ No newline at end of file +} diff --git a/tests/Unit/Twig/DataTableExtensionTest.php b/tests/Unit/Twig/DataTableExtensionTest.php index 3281622f..2e7e5af1 100644 --- a/tests/Unit/Twig/DataTableExtensionTest.php +++ b/tests/Unit/Twig/DataTableExtensionTest.php @@ -39,4 +39,4 @@ private function createExtension(): DataTableExtension $this->createStub(FilterClearUrlGeneratorInterface::class), ); } -} \ No newline at end of file +} diff --git a/tests/Unit/Twig/DataTableThemeNodeTest.php b/tests/Unit/Twig/DataTableThemeNodeTest.php index 3d27ed3f..fc607611 100644 --- a/tests/Unit/Twig/DataTableThemeNodeTest.php +++ b/tests/Unit/Twig/DataTableThemeNodeTest.php @@ -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)); @@ -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 @@ -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' => [ @@ -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' => [ @@ -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);', + ], ]; } -} \ No newline at end of file +} diff --git a/tests/Unit/Twig/DataTableThemeTokenParserTest.php b/tests/Unit/Twig/DataTableThemeTokenParserTest.php index 405cabf3..63b4acee 100644 --- a/tests/Unit/Twig/DataTableThemeTokenParserTest.php +++ b/tests/Unit/Twig/DataTableThemeTokenParserTest.php @@ -81,4 +81,4 @@ public static function provideCompileCases(): iterable ), ]; } -} \ No newline at end of file +}