From a44ad4c70468584d34f12c4ec850d6c687c76245 Mon Sep 17 00:00:00 2001 From: Vasyl Liulka Date: Fri, 15 Nov 2024 14:45:38 +0100 Subject: [PATCH] OXDEV-8893 Fix Twig v3.15.0 update --- src/Extensions/PhpFunctionsExtension.php | 47 ++++- .../Extensions/FormatDateExtensionTest.php | 12 -- .../Extensions/FormatPriceExtensionTest.php | 3 - .../Extensions/InputHelpExtensionTest.php | 6 - .../Extensions/PhpFunctionsExtensionTest.php | 12 +- .../Extensions/ScriptExtensionTest.php | 3 - .../Extensions/TranslateExtensionTest.php | 3 - .../Unit/Extensions/CaptureExtensionTest.php | 5 +- .../Extensions/Filters/CatExtensionTest.php | 8 +- .../Filters/DateFormatExtensionTest.php | 6 +- .../Extensions/Filters/EncloseFilterTest.php | 19 +- .../Filters/FileSizeExtensionTest.php | 6 +- .../Filters/SmartWordwrapExtensionTest.php | 3 - .../Extensions/HasRightsExtensionTest.php | 5 +- .../Unit/Extensions/IncludeExtensionTest.php | 6 - .../Extensions/IncludeWidgetExtensionTest.php | 18 +- tests/Unit/Extensions/StyleExtensionTest.php | 6 +- .../Unit/Loader/CmsTemplateNameParserTest.php | 17 +- tests/Unit/Node/AbstractOxidTwigTestCase.php | 28 --- tests/Unit/Node/CaptureNodeTest.php | 102 +++++------ tests/Unit/Node/HasRightsNodeTest.php | 136 +++++++------- tests/Unit/Node/IfContentNodeTest.php | 92 +++++----- tests/Unit/Node/IncludeDynamicNodeTest.php | 166 ++++++++++-------- .../TokenParser/CaptureTokenParserTest.php | 29 +-- .../TokenParser/HasRightsTokenParserTest.php | 21 +-- .../TokenParser/IfContentTokenParserTest.php | 19 +- tests/phpunit.xml | 2 +- 27 files changed, 337 insertions(+), 443 deletions(-) delete mode 100644 tests/Unit/Node/AbstractOxidTwigTestCase.php diff --git a/src/Extensions/PhpFunctionsExtension.php b/src/Extensions/PhpFunctionsExtension.php index 192190c..db2d87b 100644 --- a/src/Extensions/PhpFunctionsExtension.php +++ b/src/Extensions/PhpFunctionsExtension.php @@ -23,19 +23,54 @@ class PhpFunctionsExtension extends AbstractExtension public function getFunctions(): array { return [ - new TwigFunction('count', 'count', ['deprecated' => true, 'alternative' => 'length']), - new TwigFunction('empty', 'empty', ['deprecated' => true, 'alternative' => 'length']), - new TwigFunction('isset', [$this, 'twigIsset', ['deprecated' => true, 'alternative' => 'is defined']]) + new TwigFunction( + 'count', + 'count', + [ + 'deprecated' => true, + 'alternative' => 'length', + ] + ), + new TwigFunction( + 'empty', + [ + $this, + 'emptyWrapper', + [ + 'deprecated' => true, + 'alternative' => 'length', + ], + ] + ), + new TwigFunction( + 'isset', + [ + $this, + 'twigIsset', + [ + 'deprecated' => true, + 'alternative' => 'is defined', + ], + ] + ) ]; } /** - * @param null $value - * - * @return bool + * isset wrapper, isset is a language construct and can't be called as a function in callback + * @link https://www.php.net/manual/en/functions.variable-functions.php */ public function twigIsset($value = null): bool { return isset($value); } + + /** + * empty is a language construct and can't be called as a function callback + * @link https://www.php.net/manual/en/functions.variable-functions.php + */ + public function emptyWrapper(mixed $value = null): bool + { + return empty($value); + } } diff --git a/tests/Integration/Extensions/FormatDateExtensionTest.php b/tests/Integration/Extensions/FormatDateExtensionTest.php index 67724d3..334df68 100644 --- a/tests/Integration/Extensions/FormatDateExtensionTest.php +++ b/tests/Integration/Extensions/FormatDateExtensionTest.php @@ -21,9 +21,6 @@ protected function setUp(): void $this->extension = new FormatDateExtension(new FormatDateLogic()); } - /** - * @covers \OxidEsales\Twig\Extensions\Filters\FormatDateExtension::formatDate - */ public function testFormDateWithDatetime(): void { $template = "{{ '01.08.2007 11.56.25'|format_date('datetime', true) }}"; @@ -32,9 +29,6 @@ public function testFormDateWithDatetime(): void $this->assertEquals($expected, $this->getTemplate($template)->render([])); } - /** - * @covers \OxidEsales\Twig\Extensions\Filters\FormatDateExtension::formatDate - */ public function testFormDateWithTimestamp(): void { $template = "{{ '20070801115625'|format_date('timestamp', true) }}"; @@ -43,9 +37,6 @@ public function testFormDateWithTimestamp(): void $this->assertEquals($expected, $this->getTemplate($template)->render([])); } - /** - * @covers \OxidEsales\Twig\Extensions\Filters\FormatDateExtension::formatDate - */ public function testFormDateWithDate(): void { $template = "{{ '2007-08-01 11:56:25'|format_date('date', true) }}"; @@ -54,9 +45,6 @@ public function testFormDateWithDate(): void $this->assertEquals($expected, $this->getTemplate($template)->render([])); } - /** - * @covers \OxidEsales\Twig\Extensions\Filters\FormatDateExtension::formatDate - */ public function testFormDateUsingObject(): void { $template = "{{ field|format_date('datetime') }}"; diff --git a/tests/Integration/Extensions/FormatPriceExtensionTest.php b/tests/Integration/Extensions/FormatPriceExtensionTest.php index 27de701..bc432f8 100644 --- a/tests/Integration/Extensions/FormatPriceExtensionTest.php +++ b/tests/Integration/Extensions/FormatPriceExtensionTest.php @@ -30,9 +30,6 @@ public static function priceProvider(): array ]; } - /** - * @covers \OxidEsales\Twig\Extensions\FormatPriceExtension::formatPrice - */ #[DataProvider('priceProvider')] public function testFormatPrice($template, $expected): void { diff --git a/tests/Integration/Extensions/InputHelpExtensionTest.php b/tests/Integration/Extensions/InputHelpExtensionTest.php index a1a9cd5..e8158fc 100644 --- a/tests/Integration/Extensions/InputHelpExtensionTest.php +++ b/tests/Integration/Extensions/InputHelpExtensionTest.php @@ -33,9 +33,6 @@ public static function getIdentProvider(): array ]; } - /** - * @covers \OxidEsales\Twig\Extensions\InputHelpExtension::getHelpId - */ #[DataProvider('getIdentProvider')] public function testGetIdent(?string $params, int $iLang, bool $blAdmin, ?string $expected): void { @@ -54,9 +51,6 @@ public static function getHelpTextProvider(): array ]; } - /** - * @covers \OxidEsales\Twig\Extensions\InputHelpExtension::getHelpText - */ #[DataProvider('getHelpTextProvider')] public function testGetHelpText(?string $params, int $iLang, bool $blAdmin, ?string $expected): void { diff --git a/tests/Integration/Extensions/PhpFunctionsExtensionTest.php b/tests/Integration/Extensions/PhpFunctionsExtensionTest.php index e881ab1..8509104 100644 --- a/tests/Integration/Extensions/PhpFunctionsExtensionTest.php +++ b/tests/Integration/Extensions/PhpFunctionsExtensionTest.php @@ -14,9 +14,6 @@ final class PhpFunctionsExtensionTest extends AbstractExtensionTestCase { - /** @var PhpFunctionsExtension */ - protected \Twig\Extension\AbstractExtension $extension; - protected function setUp(): void { parent::setUp(); @@ -26,10 +23,11 @@ protected function setUp(): void public static function dummyTemplateProvider(): array { return [ - ["{{ count({0:0, 1:1, 2:2}) }}", 3], - ["{{ empty({0:0, 1:1}) }}", false], - ["{{ empty({}) }}", true], - ["{{ isset(foo) }}", false], + ['{{ count({0:0, 1:1, 2:2}) }}', 3], + ['{{ empty({0:0, 1:1}) }}', false], + ['{{ empty({}) }}', true], + ['{{ empty() }}', true], + ['{{ isset(foo) }}', false], ["{% set foo = 'bar' %} {{ isset(foo) }}", true], ]; } diff --git a/tests/Integration/Extensions/ScriptExtensionTest.php b/tests/Integration/Extensions/ScriptExtensionTest.php index 3a262b2..894eed1 100644 --- a/tests/Integration/Extensions/ScriptExtensionTest.php +++ b/tests/Integration/Extensions/ScriptExtensionTest.php @@ -24,9 +24,6 @@ protected function setUp(): void $this->extension = new ScriptExtension(new ScriptLogic()); } - /** - * @covers \OxidEsales\Twig\Extensions\ScriptExtension::script - */ #[DataProvider('getScriptTests')] public function testScript(string $template, string $expected): void { diff --git a/tests/Integration/Extensions/TranslateExtensionTest.php b/tests/Integration/Extensions/TranslateExtensionTest.php index 08d86f9..bf5248d 100644 --- a/tests/Integration/Extensions/TranslateExtensionTest.php +++ b/tests/Integration/Extensions/TranslateExtensionTest.php @@ -35,9 +35,6 @@ public static function dataProvider(): array ]; } - /** - * @covers \OxidEsales\Twig\Extensions\TranslateExtension::translate - */ #[DataProvider('dataProvider')] public function testTranslate(array $params, string $expectedTranslation): void { diff --git a/tests/Unit/Extensions/CaptureExtensionTest.php b/tests/Unit/Extensions/CaptureExtensionTest.php index 1d06cef..c3e0877 100644 --- a/tests/Unit/Extensions/CaptureExtensionTest.php +++ b/tests/Unit/Extensions/CaptureExtensionTest.php @@ -23,10 +23,7 @@ protected function setUp(): void parent::setUp(); } - /** - * @covers \OxidEsales\Twig\Extensions\CaptureExtension::getTokenParsers - */ - public function testGetTokenParsers() + public function testGetTokenParsers(): void { $tokenParser = $this->CaptureExtension->getTokenParsers(); $this->assertInstanceOf(CaptureTokenParser::class, $tokenParser[0]); diff --git a/tests/Unit/Extensions/Filters/CatExtensionTest.php b/tests/Unit/Extensions/Filters/CatExtensionTest.php index c370f61..a6964dd 100644 --- a/tests/Unit/Extensions/Filters/CatExtensionTest.php +++ b/tests/Unit/Extensions/Filters/CatExtensionTest.php @@ -14,15 +14,13 @@ final class CatExtensionTest extends TestCase { - /** - * @covers \OxidEsales\Twig\Extensions\Filters\CatExtension::cat - */ public function testCat(): void { - $catFilter = new CatExtension(); $string = 'foo'; $cat = 'bar'; - $actual = $catFilter->cat($string, $cat); + + $actual = (new CatExtension())->cat($string, $cat); + $this->assertEquals($string . $cat, $actual); } } diff --git a/tests/Unit/Extensions/Filters/DateFormatExtensionTest.php b/tests/Unit/Extensions/Filters/DateFormatExtensionTest.php index 6761e3c..b51a9fc 100644 --- a/tests/Unit/Extensions/Filters/DateFormatExtensionTest.php +++ b/tests/Unit/Extensions/Filters/DateFormatExtensionTest.php @@ -28,7 +28,6 @@ protected function setUp(): void public static function provider(): array { return [ - //dummy data ['', '', '', null], ['foo', '', '', null], @@ -49,11 +48,8 @@ public static function provider(): array ]; } - /** - * @covers \OxidEsales\Twig\Extensions\Filters\DateFormatExtension::dateFormat - */ #[DataProvider('provider')] - public function testDateFormat($string, string $format, string $default_date, string $expectedDate): void + public function testDateFormat($string, $format, $default_date, $expectedDate): void { $actualDate = $this->dateFormatExtension->dateFormat($string, $format, $default_date); $this->assertEquals($expectedDate, $actualDate); diff --git a/tests/Unit/Extensions/Filters/EncloseFilterTest.php b/tests/Unit/Extensions/Filters/EncloseFilterTest.php index f1bd7b8..7aa5b1c 100644 --- a/tests/Unit/Extensions/Filters/EncloseFilterTest.php +++ b/tests/Unit/Extensions/Filters/EncloseFilterTest.php @@ -14,26 +14,17 @@ final class EncloseFilterTest extends TestCase { - /** - * @covers \OxidEsales\Twig\Extensions\Filters\EncloseExtension::enclose - */ public function testEnclose(): void { - $string = "foo"; - $encloser = "*"; - $encloseFilter = new EncloseExtension(); - $enclosedString = $encloseFilter->enclose($string, $encloser); + $enclosedString = (new EncloseExtension())->enclose('foo', '*'); + $this->assertEquals('*foo*', $enclosedString); } - /** - * @covers \OxidEsales\Twig\Extensions\Filters\EncloseExtension::enclose - */ - public function testEncloseNoEncloder(): void + public function testEncloseNoEncloser(): void { - $string = "foo"; - $encloseFilter = new EncloseExtension(); - $enclosedString = $encloseFilter->enclose($string); + $enclosedString = (new EncloseExtension())->enclose('foo'); + $this->assertEquals('foo', $enclosedString); } } diff --git a/tests/Unit/Extensions/Filters/FileSizeExtensionTest.php b/tests/Unit/Extensions/Filters/FileSizeExtensionTest.php index 2e028bb..236445f 100644 --- a/tests/Unit/Extensions/Filters/FileSizeExtensionTest.php +++ b/tests/Unit/Extensions/Filters/FileSizeExtensionTest.php @@ -26,12 +26,8 @@ public static function provider(): array ]; } - /** - * - * @covers \OxidEsales\Twig\Extensions\Filters\FileSizeExtension::fileSize - */ #[DataProvider('provider')] - public function testFileSize(string $fileSize, string $expectedFileSize): void + public function testFileSize($fileSize, $expectedFileSize): void { $fileSizeLogic = new FileSizeLogic(); $fileSizeExtension = new FileSizeExtension($fileSizeLogic); diff --git a/tests/Unit/Extensions/Filters/SmartWordwrapExtensionTest.php b/tests/Unit/Extensions/Filters/SmartWordwrapExtensionTest.php index 4f479ce..c6e4322 100644 --- a/tests/Unit/Extensions/Filters/SmartWordwrapExtensionTest.php +++ b/tests/Unit/Extensions/Filters/SmartWordwrapExtensionTest.php @@ -109,9 +109,6 @@ public static function provider(): array ]; } - /** - * @covers \OxidEsales\Twig\Extensions\Filters\SmartWordwrapExtension::smartWordWrap - */ #[DataProvider('provider')] public function testSmartWordWrap(array $params, string $expectedString): void { diff --git a/tests/Unit/Extensions/HasRightsExtensionTest.php b/tests/Unit/Extensions/HasRightsExtensionTest.php index 9745541..2fd00e3 100644 --- a/tests/Unit/Extensions/HasRightsExtensionTest.php +++ b/tests/Unit/Extensions/HasRightsExtensionTest.php @@ -24,10 +24,7 @@ protected function setUp(): void parent::setUp(); } - /** - * @covers \OxidEsales\Twig\Extensions\HasRightsExtension::getTokenParsers - */ - public function testGetTokenParsers() + public function testGetTokenParsers(): void { $tokenParser = $this->hasRightsExtension->getTokenParsers(); $this->assertInstanceOf(HasRightsTokenParser::class, $tokenParser[0]); diff --git a/tests/Unit/Extensions/IncludeExtensionTest.php b/tests/Unit/Extensions/IncludeExtensionTest.php index ee5a658..0341637 100644 --- a/tests/Unit/Extensions/IncludeExtensionTest.php +++ b/tests/Unit/Extensions/IncludeExtensionTest.php @@ -30,9 +30,6 @@ public function setUp(): void ); } - /** - * @covers \OxidEsales\Twig\Extensions\IncludeExtension::includeDynamicPrefix - */ #[DataProvider('dataProviderTestIncludeDynamicPrefix')] public function testIncludeDynamicPrefix(array $parameters, array $expected): void { @@ -54,9 +51,6 @@ public static function dataProviderTestIncludeDynamicPrefix(): array ]; } - /** - * @covers \OxidEsales\Twig\Extensions\IncludeExtension::renderForCache - */ #[DataProvider('dataProviderTestRenderForCache')] public function testRenderForCache(array $parameters, string $expected): void { diff --git a/tests/Unit/Extensions/IncludeWidgetExtensionTest.php b/tests/Unit/Extensions/IncludeWidgetExtensionTest.php index fbb376b..ba6a44a 100644 --- a/tests/Unit/Extensions/IncludeWidgetExtensionTest.php +++ b/tests/Unit/Extensions/IncludeWidgetExtensionTest.php @@ -9,16 +9,15 @@ namespace OxidEsales\Twig\Tests\Unit\Extension; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\WidgetControl; use OxidEsales\EshopCommunity\Internal\Transition\Adapter\TemplateLogic\IncludeWidgetLogic; use OxidEsales\Twig\Extensions\IncludeWidgetExtension; use PHPUnit\Framework\TestCase; final class IncludeWidgetExtensionTest extends TestCase { - /** - * @var IncludeWidgetExtension - */ - protected $includeWidgetExtension; + private IncludeWidgetExtension $includeWidgetExtension; protected function setUp(): void { @@ -27,14 +26,11 @@ protected function setUp(): void $this->includeWidgetExtension = new IncludeWidgetExtension($includeWidgetLogic); } - /** - * @covers \OxidEsales\Twig\Extensions\IncludeWidgetExtension::includeWidget - */ - public function testIncludeWidget() + public function testIncludeWidget(): void { - $widgetControl = $this->createMock(\OxidEsales\Eshop\Core\WidgetControl::class); - $widgetControl->expects($this->any())->method("start")->will($this->returnValue('html')); - \OxidEsales\Eshop\Core\Registry::set(\OxidEsales\Eshop\Core\WidgetControl::class, $widgetControl); + $widgetControl = $this->createMock(WidgetControl::class); + $widgetControl->method('start')->willReturn('html'); + Registry::set(WidgetControl::class, $widgetControl); $actual = $this->includeWidgetExtension->includeWidget(['cl' => 'oxwTagCloud', 'blShowTags' => 1]); $this->assertEquals('html', $actual); diff --git a/tests/Unit/Extensions/StyleExtensionTest.php b/tests/Unit/Extensions/StyleExtensionTest.php index 1a1232a..a4dead3 100644 --- a/tests/Unit/Extensions/StyleExtensionTest.php +++ b/tests/Unit/Extensions/StyleExtensionTest.php @@ -18,9 +18,6 @@ final class StyleExtensionTest extends TestCase { - /** - * @covers \OxidEsales\EshopCommunity\Internal\Transition\Adapter\TemplateLogic\StyleLogic::collectStyleSheets - */ #[DataProvider('dataProvider')] public function testCollectStyleSheets(array $params, bool $isDynamic): void { @@ -40,7 +37,6 @@ public static function dataProvider(): array private function getTwigEnvironment($isDynamic): Environment { - /** @var LoaderInterface $loader */ $loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); $env = new Environment($loader, []); $env->addGlobal('__oxid_include_dynamic', $isDynamic); @@ -49,10 +45,10 @@ private function getTwigEnvironment($isDynamic): Environment private function getStyleExtensionMock(array $params, bool $isDynamic): StyleExtension { - /** @var StyleLogic $styleLogic */ $styleLogic = $this->getMockBuilder(StyleLogic::class)->disableOriginalConstructor()->getMock(); $styleLogic->method('collectStyleSheets')->willReturn([]); $styleLogic->expects($this->once())->method('collectStyleSheets')->with($params, $isDynamic); + return new StyleExtension($styleLogic); } } diff --git a/tests/Unit/Loader/CmsTemplateNameParserTest.php b/tests/Unit/Loader/CmsTemplateNameParserTest.php index d55e253..c459f60 100644 --- a/tests/Unit/Loader/CmsTemplateNameParserTest.php +++ b/tests/Unit/Loader/CmsTemplateNameParserTest.php @@ -23,9 +23,6 @@ protected function setUp(): void $this->cmsTemplateNameParser = new CmsTemplateNameParser(); } - /** - * @covers \OxidEsales\Twig\Loader\CmsTemplateNameParser::isValidName - */ #[DataProvider('getValidNameTests')] #[DataProvider('getInvalidNameTests')] public function testIsValidName(string $name, array $expected): void @@ -33,36 +30,24 @@ public function testIsValidName(string $name, array $expected): void $this->assertEquals($this->cmsTemplateNameParser->isValidName($name), $expected['valid']); } - /** - * @covers \OxidEsales\Twig\Loader\CmsTemplateNameParser::getLoaderName - */ #[DataProvider('getValidNameTests')] public function testGetLoaderName(string $name, array $expected): void { $this->assertEquals($this->cmsTemplateNameParser->getLoaderName($name), $expected['loaderName']); } - /** - * @covers \OxidEsales\Twig\Loader\CmsTemplateNameParser::getValue - */ #[DataProvider('getValidNameTests')] public function testGetValue(string $name, array $expected): void { $this->assertEquals($this->cmsTemplateNameParser->getValue($name), $expected['value']); } - /** - * @covers \OxidEsales\Twig\Loader\CmsTemplateNameParser::getParameters - */ #[DataProvider('getValidNameTests')] public function testGetParameters(string $name, array $expected): void { $this->assertEquals($this->cmsTemplateNameParser->getParameters($name), $expected['parameters']); } - /** - * @covers \OxidEsales\Twig\Loader\CmsTemplateNameParser::getKey - */ #[DataProvider('getValidNameTests')] public function testGetKey(string $name, array $expected): void { @@ -85,7 +70,7 @@ public static function getInvalidNameTests(): array ]; return array_map( - fn ($name) => [$name, ['valid' => false]], + static fn ($name) => [$name, ['valid' => false]], $invalidNames ); } diff --git a/tests/Unit/Node/AbstractOxidTwigTestCase.php b/tests/Unit/Node/AbstractOxidTwigTestCase.php deleted file mode 100644 index 6f0bb15..0000000 --- a/tests/Unit/Node/AbstractOxidTwigTestCase.php +++ /dev/null @@ -1,28 +0,0 @@ -assertNodeCompilation($source, $node, $environment, $isPattern); - } -} diff --git a/tests/Unit/Node/CaptureNodeTest.php b/tests/Unit/Node/CaptureNodeTest.php index 09d0903..74667be 100644 --- a/tests/Unit/Node/CaptureNodeTest.php +++ b/tests/Unit/Node/CaptureNodeTest.php @@ -10,63 +10,34 @@ namespace OxidEsales\Twig\Tests\Unit\Node; use OxidEsales\Twig\Node\CaptureNode; +use PHPUnit\Framework\Attributes\DataProvider; use Twig\Node\TextNode; +use Twig\Test\NodeTestCase; -final class CaptureNodeTest extends AbstractOxidTwigTestCase +final class CaptureNodeTest extends NodeTestCase { - public static function getOxidTwigTests(): array - { - return array_merge( - self::getTestForCaptureWithAttributeName(), - self::getTestForCaptureWithAttributeAssign(), - self::getTestForCaptureWithAttributeAppend() - ); - } - - private static function getTestForCaptureWithAttributeName(): array - { - $tests = []; - $nodeForCaptureName = self::getCaptureNode('name'); - $tests[] = [$nodeForCaptureName, <<assertNodeCompilation($source, $node); } - private static function getCaptureNode(string $attributeName): CaptureNode + public static function compileDataProvider(): array { - return new CaptureNode( - $attributeName, - 'foo', - new TextNode("Lorem Ipsum", 1), - 1, - 'capture' - ); + $body = new TextNode(data: 'Lorem Ipsum', lineno: 1); + + return [ + [ + new CaptureNode( + attributeName: 'name', + variableName: 'foo', + body: $body, + line: 1, + tag: 'capture' + ), + self::EXPECTED_NAME_ATTRIBUTE_SOURCE, + ], + [ + new CaptureNode( + attributeName: 'assign', + variableName: 'foo', + body: $body, + line: 1, + tag: 'capture' + ), + self::EXPECTED_ASSIGN_ATTRIBUTE_SOURCE, + ], + [ + new CaptureNode( + attributeName: 'append', + variableName: 'foo', + body: $body, + line: 1, + tag: 'capture' + ), + self::EXPECTED_APPEND_ATTRIBUTE_SOURCE, + ] + ]; } } diff --git a/tests/Unit/Node/HasRightsNodeTest.php b/tests/Unit/Node/HasRightsNodeTest.php index fca0b93..463c968 100644 --- a/tests/Unit/Node/HasRightsNodeTest.php +++ b/tests/Unit/Node/HasRightsNodeTest.php @@ -10,89 +10,83 @@ namespace OxidEsales\Twig\Tests\Unit\Node; use OxidEsales\Twig\Node\HasRightsNode; +use PHPUnit\Framework\Attributes\DataProvider; use Twig\Node\Expression\ArrayExpression; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Node; use Twig\Node\TextNode; +use Twig\Test\NodeTestCase; -final class HasRightsNodeTest extends AbstractOxidTwigTestCase +final class HasRightsNodeTest extends NodeTestCase { - public function testConstruct(): void - { - $parameters = []; - $parameters[] = new ConstantExpression('name', 1); - $parameters[] = new ConstantExpression('value', 1); - - $body = new TextNode("Lorem Ipsum", 1); - $parametersNode = new ArrayExpression($parameters, 1); - $node = new HasRightsNode($body, $parametersNode, 1); - - $this->assertEquals($body, $node->getNode('body')); - $this->assertEquals($parametersNode, $node->getNode('parameters')); - } - - public static function getOxidTwigTests(): array - { - return array_merge( - self::getNonNestedBlocksTests(), - self::getNestedBlocksTests() - ); - } - - private static function getNonNestedBlocksTests(): array - { - $tests = []; - - $parameters = []; - $parameters[] = new ConstantExpression('name', 1); - $parameters[] = new ConstantExpression('value', 1); - - $body = new TextNode("Lorem Ipsum", 1); - $node = new HasRightsNode($body, new ArrayExpression($parameters, 1), 1); - - $tests[] = [$node, <<assertNodeCompilation($source, $node, $environment, $isPattern); + } + + public static function compileDataProvider(): array + { + return [ + [ + new HasRightsNode( + body: new TextNode(data: 'Lorem Ipsum', lineno: 1), + parameters: new ArrayExpression( + elements: [ + new ConstantExpression(value: 'name', lineno: 1), + new ConstantExpression(value: 'value', lineno: 1) + ], + lineno: 1 + ), + lineno: 1 + ), + self::EXPECTED_NON_NESTED_NODE_SOURCE, + ], + [ + new HasRightsNode( + body: new Node( + nodes: [ + new TextNode(data: 'Top', lineno: 2), + new HasRightsNode( + new TextNode(data: 'Inner', lineno: 4), + new ArrayExpression( + elements: [ + new ConstantExpression(value: 'type', lineno: 3), + new ConstantExpression(value: 'inner', lineno: 3) + ], + lineno: 3 + ), + 3 + ), + new TextNode(data: 'Bottom', lineno: 6) + ] + ), + parameters: new ArrayExpression( + elements: [ + new ConstantExpression(value: 'type', lineno: 1), + new ConstantExpression(value: 'outer', lineno: 1) + ], + lineno: 1 + ), + lineno: 1 + ), + self::EXPECTED_NESTED_NODE_SOURCE, + ] + ]; } } diff --git a/tests/Unit/Node/IfContentNodeTest.php b/tests/Unit/Node/IfContentNodeTest.php index c7d5a1e..32cbc94 100644 --- a/tests/Unit/Node/IfContentNodeTest.php +++ b/tests/Unit/Node/IfContentNodeTest.php @@ -11,64 +11,58 @@ use OxidEsales\Twig\Extensions\IfContentExtension; use OxidEsales\Twig\Node\IfContentNode; +use PHPUnit\Framework\Attributes\DataProvider; use Twig\Node\Expression\AssignNameExpression; use Twig\Node\Expression\ConstantExpression; use Twig\Node\TextNode; +use Twig\Test\NodeTestCase; -final class IfContentNodeTest extends AbstractOxidTwigTestCase -{ - public function testConstructor(): void - { - $body = new TextNode('Lorem Ipsum', 1); - $variable = new AssignNameExpression('foo', 1); - $node = new IfContentNode($body, [], $variable, 1); - - $this->assertEquals($body, $node->getNode('body')); - $this->assertEquals($variable, $node->getNode('variable')); - - $expr = new ConstantExpression("oxsomething", 1); - - $node = new IfContentNode($body, ['ident' => $expr], $variable, 1); - $this->assertEquals($expr, $node->getNode('ident')); - $this->assertFalse($node->hasNode('oxid')); +use function sprintf; - $node = new IfContentNode($body, ['oxid' => $expr], $variable, 1); - $this->assertEquals($expr, $node->getNode('oxid')); - $this->assertFalse($node->hasNode('ident')); - } +final class IfContentNodeTest extends NodeTestCase +{ + private const EXPECTED_IDENT_NODE_SOURCE = <<extensions['%s']->getContent("oxsomething", null); +if(\$context["foo"]) { \nyield "Lorem Ipsum";\n } \nunset(\$context["foo"]); +EOF; + private const EXPECTED_OXID_NODE_SOURCE = <<extensions['%s']->getContent(null, "oxsomething"); +if(\$context["foo"]) { \nyield "Lorem Ipsum";\n } \nunset(\$context["foo"]); +EOF; - public static function getOxidTwigTests(): array + public static function compileDataProvider(): array { - $ifContentExtensionClass = IfContentExtension::class; + $body = new TextNode(data: 'Lorem Ipsum', lineno: 1); + $referenceExpression = new ConstantExpression(value: 'oxsomething', lineno: 1); + $variable = new AssignNameExpression(name: 'foo', lineno: 1); - $tests = []; - - $body = new TextNode('Lorem Ipsum', 1); - $variable = new AssignNameExpression('foo', 1); - $expr = new ConstantExpression("oxsomething", 1); - $node = new IfContentNode($body, ['ident' => $expr], $variable, 1); - $tests[] = [$node, <<extensions['$ifContentExtensionClass']->getContent("oxsomething", null); -if(\$context["foo"]) { -echo "Lorem Ipsum"; - } -unset(\$context["foo"]); -EOF - ]; - - $node = new IfContentNode($body, ['oxid' => $expr], $variable, 1); - $tests[] = [$node, <<extensions['$ifContentExtensionClass']->getContent(null, "oxsomething"); -if(\$context["foo"]) { -echo "Lorem Ipsum"; - } -unset(\$context["foo"]); -EOF + return [ + [ + new IfContentNode( + body: $body, + reference: ['ident' => $referenceExpression], + variable: $variable, + lineno: 1 + ), + sprintf(self::EXPECTED_IDENT_NODE_SOURCE, IfContentExtension::class) + ], + [ + new IfContentNode( + $body, + ['oxid' => $referenceExpression], + $variable, + 1 + ), + sprintf(self::EXPECTED_OXID_NODE_SOURCE, IfContentExtension::class) + ], ]; + } - - return $tests; + #[DataProvider('compileDataProvider')] + public function testCompile($node, $source, $environment = null, $isPattern = false): void + { + $this->assertNodeCompilation($source, $node, $environment, $isPattern); } } diff --git a/tests/Unit/Node/IncludeDynamicNodeTest.php b/tests/Unit/Node/IncludeDynamicNodeTest.php index 709dec6..a229cf6 100644 --- a/tests/Unit/Node/IncludeDynamicNodeTest.php +++ b/tests/Unit/Node/IncludeDynamicNodeTest.php @@ -11,104 +11,130 @@ use OxidEsales\Twig\Extensions\IncludeExtension; use OxidEsales\Twig\Node\IncludeDynamicNode; +use PHPUnit\Framework\Attributes\DataProvider; use Twig\Node\Expression\ArrayExpression; use Twig\Node\Expression\ConstantExpression; +use Twig\Test\NodeTestCase; -final class IncludeDynamicNodeTest extends AbstractOxidTwigTestCase -{ - public function testConstructor(): void - { - $expr = new ConstantExpression('foo.twig', 1); - $node = new IncludeDynamicNode($expr, null, false, false, 1); - - $this->assertFalse($node->hasNode('variables')); - $this->assertEquals($expr, $node->getNode('expr')); - $this->assertFalse($node->getAttribute('only')); - - $vars = new ArrayExpression( - [ - new ConstantExpression('foo', 1), - new ConstantExpression(true, 1) - ], - 1 - ); - - $node = new IncludeDynamicNode($expr, $vars, true, false, 1); - $this->assertEquals($vars, $node->getNode('variables')); - $this->assertTrue($node->getAttribute('only')); - } +use function sprintf; - public static function getOxidTwigTests(): array - { - $includeExtensionClass = IncludeExtension::class; - - $tests = []; - - $expr = new ConstantExpression('foo.twig', 1); - $node = new IncludeDynamicNode($expr, null, false, false, 1); - $tests[] = [$node, <<extensions['$includeExtensionClass']->renderForCache(['file' => "foo.twig"]); + echo \$this->extensions['%s']->renderForCache(['file' => "foo.twig"]); } else { - \$this->loadTemplate("foo.twig", null, 1)->display(\$context); +\$this->loadTemplate("foo.twig", null, 1)->display(\$context); } -EOF - ]; - - $expr = new ConstantExpression('foo.twig', 1); - $vars = new ArrayExpression( - [ - new ConstantExpression('foo', 1), - new ConstantExpression(true, 1) - ], - 1 - ); - $node = new IncludeDynamicNode($expr, $vars, false, false, 1); - $tests[] = [$node, << true]; if (!empty(\$context["_render4cache"])) { - echo \$this->extensions['$includeExtensionClass']->renderForCache(array_merge(\$parameters, ['file' => "foo.twig"])); + echo \$this->extensions['%s']->renderForCache(array_merge(\$parameters, ['file' => "foo.twig"])); } else { - \$parameters = \$this->extensions['$includeExtensionClass']->includeDynamicPrefix(\$parameters); - \$this->loadTemplate("foo.twig", null, 1)->display(array_merge(\$context, \$parameters)); + \$parameters = \$this->extensions['%s']->includeDynamicPrefix(\$parameters); +\$this->loadTemplate("foo.twig", null, 1)->display(array_merge(\$context, \$parameters)); } -EOF - ]; - - $expr = new ConstantExpression('foo.twig', 1); - $node = new IncludeDynamicNode($expr, $vars, true, false, 1); - $tests[] = [$node, << true]; if (!empty(\$context["_render4cache"])) { - echo \$this->extensions['$includeExtensionClass']->renderForCache(array_merge(\$parameters, ['file' => "foo.twig"])); + echo \$this->extensions['%s']->renderForCache(array_merge(\$parameters, ['file' => "foo.twig"])); } else { - \$parameters = \$this->extensions['$includeExtensionClass']->includeDynamicPrefix(\$parameters); - \$this->loadTemplate("foo.twig", null, 1)->display(\$parameters); + \$parameters = \$this->extensions['%s']->includeDynamicPrefix(\$parameters); +\$this->loadTemplate("foo.twig", null, 1)->display(\$parameters); } -EOF - ]; - - $expr = new ConstantExpression('foo.twig', 1); - $node = new IncludeDynamicNode($expr, $vars, true, true, 1); - $tests[] = [$node, << true]; if (!empty(\$context["_render4cache"])) { - echo \$this->extensions['$includeExtensionClass']->renderForCache(array_merge(\$parameters, ['file' => "foo.twig"])); + echo \$this->extensions['%s']->renderForCache(array_merge(\$parameters, ['file' => "foo.twig"])); } else { - \$parameters = \$this->extensions['$includeExtensionClass']->includeDynamicPrefix(\$parameters); - \$this->loadTemplate("foo.twig", null, 1)->display(\$parameters); + \$parameters = \$this->extensions['%s']->includeDynamicPrefix(\$parameters); +\$this->loadTemplate("foo.twig", null, 1)->display(\$parameters); } } catch (\Twig\Error\LoaderError \$e) { // ignore missing template } -EOF +EOF; + + public static function compileDataProvider(): array + { + $expression = new ConstantExpression(value: 'foo.twig', lineno: 1); + $variables = new ArrayExpression( + elements: [ + new ConstantExpression(value: 'foo', lineno: 1), + new ConstantExpression(value: true, lineno: 1) + ], + lineno: 1 + ); + return [ + [ + new IncludeDynamicNode( + expr: $expression, + variables: null, + only: false, + ignoreMissing: false, + lineno: 1 + ), + sprintf( + self::EXPECTED_ACTIVE_CONTEXT_EMPTY_PARAMETERS_NODE_SOURCE, + IncludeExtension::class + ), + ], + [ + new IncludeDynamicNode( + expr: $expression, + variables: $variables, + only: false, + ignoreMissing: false, + lineno: 1 + ), + sprintf( + self::EXPECTED_ACTIVE_CONTEXT_NODE_SOURCE, + IncludeExtension::class, + IncludeExtension::class + ), + ], + [ + new IncludeDynamicNode( + expr: $expression, + variables: $variables, + only: true, + ignoreMissing: false, + lineno: 1 + ), + sprintf( + self::EXPECTED_LOCAL_CONTEXT_NODE_SOURCE, + IncludeExtension::class, + IncludeExtension::class + ), + ], + [ + new IncludeDynamicNode( + expr: $expression, + variables: $variables, + only: true, + ignoreMissing: true, + lineno: 1 + ), + sprintf( + self::EXPECTED_LOCAL_CONTEXT_IGNORE_MISSING_NODE_SOURCE, + IncludeExtension::class, + IncludeExtension::class + ), + ], ]; + } - return $tests; + #[DataProvider('compileDataProvider')] + public function testCompile($node, $source, $environment = null, $isPattern = false): void + { + $this->assertNodeCompilation($source, $node, $environment, $isPattern); } } diff --git a/tests/Unit/TokenParser/CaptureTokenParserTest.php b/tests/Unit/TokenParser/CaptureTokenParserTest.php index f9b00c5..1319004 100644 --- a/tests/Unit/TokenParser/CaptureTokenParserTest.php +++ b/tests/Unit/TokenParser/CaptureTokenParserTest.php @@ -28,7 +28,7 @@ final class CaptureTokenParserTest extends TestCase protected function setUp(): void { parent::setUp(); - /** @var LoaderInterface $loader */ + $loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); $this->environment = new Environment($loader, ['cache' => false]); @@ -38,29 +38,20 @@ protected function setUp(): void $this->parser = new Parser($this->environment); } - /** - * @covers \OxidEsales\Twig\TokenParser\CaptureTokenParser::getTag - */ public function testGetTag(): void { $this->assertEquals('capture', $this->captureTokenParser->getTag()); } - /** - * @covers \OxidEsales\Twig\TokenParser\CaptureTokenParser::decideBlockEnd - */ public function testDecideBlockEnd(): void { $token = new Token(Token::NAME_TYPE, 'foo', 1); - $this->assertEquals(false, $this->captureTokenParser->decideBlockEnd($token)); + $this->assertFalse($this->captureTokenParser->decideBlockEnd($token)); $token = new Token(Token::NAME_TYPE, 'endcapture', 1); - $this->assertEquals(true, $this->captureTokenParser->decideBlockEnd($token)); + $this->assertTrue($this->captureTokenParser->decideBlockEnd($token)); } - /** - * @covers \OxidEsales\Twig\TokenParser\CaptureTokenParser::parse - */ #[DataProvider('templateSourceCodeProvider')] public function testParse(string $source): void { @@ -82,15 +73,12 @@ public function testParse(string $source): void public static function templateSourceCodeProvider(): array { return [ - ["{% capture name = \"foo\" %}Lorem Ipsum{% endcapture %}"], - ["{% capture assign = \"foo\" %}Lorem Ipsum{% endcapture %}"], - ["{% capture append = \"foo\" %}Lorem Ipsum{% endcapture %}"], + ['{% capture name = "foo" %}Lorem Ipsum{% endcapture %}'], + ['{% capture assign = "foo" %}Lorem Ipsum{% endcapture %}'], + ['{% capture append = "foo" %}Lorem Ipsum{% endcapture %}'], ]; } - /** - * @covers \OxidEsales\Twig\TokenParser\CaptureTokenParser::parse - */ public function testTwigErrorSyntaxIsThrown(): void { $source = '{% capture %}foo{% /endcapture %}'; @@ -100,12 +88,9 @@ public function testTwigErrorSyntaxIsThrown(): void $this->parser->parse($stream); } - /** - * @covers \OxidEsales\Twig\TokenParser\CaptureTokenParser::parse - */ public function testParseException(): void { - $source = "{% capture foo = \"foo\" %}Lorem Ipsum{% endcapture %}"; + $source = '{% capture foo = "foo" %}Lorem Ipsum{% endcapture %}'; $stream = $this->environment->tokenize(new Source($source, 'index')); $this->expectException(SyntaxError::class); diff --git a/tests/Unit/TokenParser/HasRightsTokenParserTest.php b/tests/Unit/TokenParser/HasRightsTokenParserTest.php index 67891c6..caccb47 100644 --- a/tests/Unit/TokenParser/HasRightsTokenParserTest.php +++ b/tests/Unit/TokenParser/HasRightsTokenParserTest.php @@ -35,38 +35,25 @@ protected function setUp(): void parent::setUp(); } - /** - * @covers \OxidEsales\Twig\TokenParser\HasRightsTokenParser::getTag - */ public function testGetTag(): void { $this->assertEquals('hasrights', $this->hasRightsParser->getTag()); } - /** - * @covers \OxidEsales\Twig\TokenParser\HasRightsTokenParser::decideMyTagFork - */ public function testDecideMyTagForkIncorrect(): void { $token = new Token(Token::TEXT_TYPE, 1, 1); - $this->assertEquals(false, $this->hasRightsParser->decideMyTagFork($token)); + $this->assertFalse($this->hasRightsParser->decideMyTagFork($token)); } - /** - * @covers \OxidEsales\Twig\TokenParser\HasRightsTokenParser::decideMyTagFork - */ public function testDecideMyTagForkCorrect(): void { $token = new Token(5, 'endhasrights', 1); - $this->assertEquals(true, $this->hasRightsParser->decideMyTagFork($token)); + $this->assertTrue($this->hasRightsParser->decideMyTagFork($token)); } - /** - * @covers \OxidEsales\Twig\TokenParser\HasRightsTokenParser::parse - */ public function testParse(): void { - /** @var LoaderInterface $loader */ $loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); $env = new Environment($loader, array('cache' => false, 'autoescape' => false)); $env->addExtension(new HasRightsExtension(new HasRightsTokenParser(HasRightsNode::class))); @@ -86,12 +73,8 @@ public function testParse(): void $this->assertTrue(isset($extensions[HasRightsExtension::class])); } - /** - * @covers \OxidEsales\Twig\TokenParser\HasRightsTokenParser::parse - */ public function testParseException(): void { - /** @var LoaderInterface $loader */ $loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); $env = new Environment($loader, ['cache' => false, 'autoescape' => false]); $env->addExtension(new HasRightsExtension(new HasRightsTokenParser(HasRightsNode::class))); diff --git a/tests/Unit/TokenParser/IfContentTokenParserTest.php b/tests/Unit/TokenParser/IfContentTokenParserTest.php index 7306979..014c2d8 100644 --- a/tests/Unit/TokenParser/IfContentTokenParserTest.php +++ b/tests/Unit/TokenParser/IfContentTokenParserTest.php @@ -25,8 +25,7 @@ final class IfContentTokenParserTest extends TestCase protected function setUp(): void { - /** @var LoaderInterface $loader */ - $loader = $this->getMockBuilder(\Twig\Loader\LoaderInterface::class)->getMock(); + $loader = $this->getMockBuilder(LoaderInterface::class)->getMock(); $this->environment = new Environment($loader, ['cache' => false]); $this->ifContentParser = new IfContentTokenParser(); @@ -35,20 +34,15 @@ protected function setUp(): void $this->parser = new Parser($this->environment); } - /** - * @covers \OxidEsales\Twig\TokenParser\IfContentTokenParser::getTag - */ public function testGetTag(): void { $this->assertEquals('ifcontent', $this->ifContentParser->getTag()); } - /** - * @covers \OxidEsales\Twig\TokenParser\IfContentTokenParser::parse - */ + public function testParse(): void { - $source = "{% ifcontent ident \"oxsomething\" set myVar %}Lorem Ipsum{% endifcontent %}"; + $source = '{% ifcontent ident "oxsomething" set myVar %}Lorem Ipsum{% endifcontent %}'; $stream = $this->environment->tokenize(new Source($source, 'index')); $node = $this->parser->parse($stream); @@ -64,15 +58,12 @@ public function testParse(): void $this->assertTrue($ifContentNode->hasNode('ident')); } - /** - * @covers \OxidEsales\Twig\TokenParser\IfContentTokenParser::decideBlockEnd - */ public function testDecideBlockEnd(): void { $token = new Token(Token::NAME_TYPE, 'foo', 1); - $this->assertEquals(false, $this->ifContentParser->decideBlockEnd($token)); + $this->assertFalse($this->ifContentParser->decideBlockEnd($token)); $token = new Token(Token::NAME_TYPE, 'endifcontent', 1); - $this->assertEquals(true, $this->ifContentParser->decideBlockEnd($token)); + $this->assertTrue($this->ifContentParser->decideBlockEnd($token)); } } diff --git a/tests/phpunit.xml b/tests/phpunit.xml index cba098c..26046d6 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,6 +1,6 @@