Skip to content

Commit

Permalink
Bump test config to PHPUnit 10
Browse files Browse the repository at this point in the history
  • Loading branch information
emodric committed Nov 23, 2023
1 parent 8d1b024 commit 08fac73
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
8 changes: 4 additions & 4 deletions phpunit-api.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
forceCoversAnnotation="true"
requireCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
>
Expand All @@ -22,9 +22,9 @@
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory suffix=".php">bundles/LayoutsAdminBundle/Controller/API</directory>
</include>
</coverage>
</source>
</phpunit>
8 changes: 4 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
forceCoversAnnotation="true"
requireCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
>
Expand All @@ -25,7 +25,7 @@
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory suffix=".php">lib</directory>
<directory suffix=".php">bundles/LayoutsBundle</directory>
Expand All @@ -40,5 +40,5 @@
<file>bundles/LayoutsAdminBundle/DependencyInjection/ExtensionPlugin.php</file>
<file>bundles/LayoutsAdminBundle/DependencyInjection/NetgenLayoutsAdminExtension.php</file>
</exclude>
</coverage>
</source>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 44 additions & 4 deletions tests/bundles/LayoutsBundle/Templating/Twig/Node/NodeTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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); })())',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
<<<EOT
// line 1
\$nglZone = {$this->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
Expand All @@ -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
Expand All @@ -91,7 +91,7 @@ public function getTests(): array
new RenderZone($zone, null, 1),
<<<EOT
// line 1
\$nglZone = {$this->getNodeGetter('zone')};
\$nglZone = {$zoneNodeGetter};
\$nglZoneIdentifier = \$nglZone instanceof {$zoneClass} ? \$nglZone->getIdentifier() : \$nglZone;
\$nglContext = {$viewInterface}::CONTEXT_DEFAULT;
\$nglTemplate = new {$templateClass}(\$this, \$context, \$blocks);
Expand Down

0 comments on commit 08fac73

Please sign in to comment.