diff --git a/phpunit-api.xml b/phpunit-api.xml index 6f2ce2050..c4d91c88a 100644 --- a/phpunit-api.xml +++ b/phpunit-api.xml @@ -1,10 +1,10 @@ @@ -22,9 +22,9 @@ - + bundles/LayoutsAdminBundle/Controller/API - + diff --git a/phpunit.xml b/phpunit.xml index 24faca8cc..0092f1f9e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,10 +1,10 @@ @@ -25,7 +25,7 @@ - + lib bundles/LayoutsBundle @@ -40,5 +40,5 @@ bundles/LayoutsAdminBundle/DependencyInjection/ExtensionPlugin.php bundles/LayoutsAdminBundle/DependencyInjection/NetgenLayoutsAdminExtension.php - + diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php index 03e874c10..050c0c841 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/Node/DefaultContextTest.php @@ -35,12 +35,9 @@ public function testConstructorWithConstant(): void self::assertSame($var, $node->getNode('expr')); } - /** - * @return mixed[] - */ - public function getTests(): array + public static function getTests(): array { - $environment = $this->getEnvironment(); + $environment = self::getEnvironment(); $environment->enableStrictVariables(); $var = new NameExpression('foo', 1); diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/Node/NodeTestBase.php b/tests/bundles/LayoutsBundle/Templating/Twig/Node/NodeTestBase.php index f9653573d..b19fec437 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/Node/NodeTestBase.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/Node/NodeTestBase.php @@ -4,15 +4,55 @@ namespace Netgen\Bundle\LayoutsBundle\Tests\Templating\Twig\Node; -use Twig\Test\NodeTestCase; +use PHPUnit\Framework\TestCase; +use Twig\Compiler; +use Twig\Environment; +use Twig\Loader\ArrayLoader; +use Twig\Node\Node; use function sprintf; +use function trim; -abstract class NodeTestBase extends NodeTestCase +abstract class NodeTestBase extends TestCase { - protected function getNodeGetter(string $name, int $lineNo = 0): string + /** + * @return mixed[] + */ + abstract public static function getTests(): iterable; + + /** + * @dataProvider getTests + */ + public function testCompile(Node $node, string $source, ?Environment $environment = null, bool $isPattern = false): void + { + self::assertNodeCompilation($source, $node, $environment, $isPattern); + } + + public static function assertNodeCompilation(string $source, Node $node, ?Environment $environment = null, bool $isPattern = false): void + { + $compiler = self::getCompiler($environment); + $compiler->compile($node); + + if ($isPattern) { + self::assertStringMatchesFormat($source, trim($compiler->getSource())); + } else { + self::assertSame($source, trim($compiler->getSource())); + } + } + + protected static function getCompiler(?Environment $environment = null): Compiler + { + return new Compiler($environment ?? self::getEnvironment()); + } + + protected static function getEnvironment(): Environment + { + return new Environment(new ArrayLoader([])); + } + + protected static function getNodeGetter(string $name, int $lineNo = 0): string { - $line = $lineNo > 0 ? "// line {$lineNo}\n" : ''; + $line = $lineNo > 0 ? sprintf("// line %d\n", $lineNo) : ''; return sprintf( '%s(isset($context["%s"]) || array_key_exists("%s", $context) ? $context["%s"] : (function () { throw new RuntimeError(\'Variable "%s" does not exist.\', 1, $this->source); })())', diff --git a/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php b/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php index f16d2d9f9..fe2eedc2a 100644 --- a/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php +++ b/tests/bundles/LayoutsBundle/Templating/Twig/Node/RenderZoneTest.php @@ -43,12 +43,9 @@ public function testConstructorWithNoContext(): void self::assertFalse($node->hasNode('context')); } - /** - * @return mixed[] - */ - public function getTests(): array + public static function getTests(): array { - $environment = $this->getEnvironment(); + $environment = self::getEnvironment(); $environment->enableStrictVariables(); $zoneClass = Zone::class; @@ -60,14 +57,17 @@ public function getTests(): array $zoneName = new ConstantExpression('zone', 1); $context = new NameExpression('context', 1); + $zoneNodeGetter = self::getNodeGetter('zone'); + $contextNodeGetter = self::getNodeGetter('context'); + return [ [ new RenderZone($zone, $context, 1), <<getNodeGetter('zone')}; + \$nglZone = {$zoneNodeGetter}; \$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone; - \$nglContext = {$this->getNodeGetter('context')}; + \$nglContext = {$contextNodeGetter}; \$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks); \$this->env->getRuntime("{$runtimeClass}")->displayZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); EOT @@ -80,7 +80,7 @@ public function getTests(): array // line 1 \$nglZone = "zone"; \$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone; - \$nglContext = {$this->getNodeGetter('context')}; + \$nglContext = {$contextNodeGetter}; \$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks); \$this->env->getRuntime("{$runtimeClass}")->displayZone(\$context["nglayouts"]->getLayout(), \$nglZoneIdentifier, \$nglContext, \$nglTemplate); EOT @@ -91,7 +91,7 @@ public function getTests(): array new RenderZone($zone, null, 1), <<getNodeGetter('zone')}; + \$nglZone = {$zoneNodeGetter}; \$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone; \$nglContext = {$viewInterface}::CONTEXT_DEFAULT; \$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks);