Cleanup PHPUnit compatibility

This commit is contained in:
Fabien Potencier
2026-08-29 09:00:40 +02:00
parent 2c1bab533b
commit 1d8243b73e
18 changed files with 51 additions and 74 deletions
+2
View File
@@ -1,6 +1,8 @@
# 4.0.0 (2026-XX-XX)
* Add an optional `string $template = ''` argument to `IncludeNode::addGetTemplate()`; subclasses overriding this method must declare a compatible parameter
* Remove the obsolete `Twig\Test\NodeTestCase::getTests()` method; override `provideTests()` instead
* Add native return types to the protected extension points in `Twig\Test\IntegrationTestCase` and `Twig\Test\NodeTestCase` and require overriding methods to declare compatible return types
* Remove `TemplateVariable` and `AssignTemplateVariable`; use `MacroVariable` and `AssignMacroVariable` instead
* Add the `isAlwaysAllowedInSandbox()` method to `Twig\TwigCallableInterface` and `Twig\TokenParser\TokenParserInterface`
* Always allow printing a `Markup` object in a sandbox, whatever the security policy is
+4 -4
View File
@@ -1042,7 +1042,7 @@ The ``IntegrationTest.php`` file should look like this::
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
protected function getExtensions(): array
{
return [
new CustomTwigExtension1(),
@@ -1050,7 +1050,7 @@ The ``IntegrationTest.php`` file should look like this::
];
}
public function getFixturesDir()
protected static function getFixturesDirectory(): string
{
return __DIR__.'/Fixtures/';
}
@@ -1066,5 +1066,5 @@ Testing the node visitors can be complex, so extend your test cases from
``\Twig\Test\NodeTestCase``. Examples can be found in the Twig repository
`tests/Twig/Node`_ directory.
.. _`tests/Twig/Fixtures`: https://github.com/twigphp/Twig/tree/3.x/tests/Fixtures
.. _`tests/Twig/Node`: https://github.com/twigphp/Twig/tree/3.x/tests/Node
.. _`tests/Twig/Fixtures`: https://github.com/twigphp/Twig/tree/4.x/tests/Fixtures
.. _`tests/Twig/Node`: https://github.com/twigphp/Twig/tree/4.x/tests/Node
+2 -2
View File
@@ -19,14 +19,14 @@ use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
return [
new CacheExtension(),
];
}
protected function getRuntimeLoaders()
protected function getRuntimeLoaders(): array
{
return [
new class implements RuntimeLoaderInterface {
@@ -16,7 +16,7 @@ use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
return [
new CssInlinerExtension(),
+1 -1
View File
@@ -16,7 +16,7 @@ use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
return [
new HtmlExtension(),
+1 -1
View File
@@ -16,7 +16,7 @@ use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
return [
new InkyExtension(),
+1 -1
View File
@@ -16,7 +16,7 @@ use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
return [
new IntlExtension(),
@@ -16,7 +16,7 @@ use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
return [
new MarkdownExtension(),
+1 -1
View File
@@ -16,7 +16,7 @@ use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
return [
new StringExtension(),
+8 -10
View File
@@ -12,6 +12,7 @@
namespace Twig\Test;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Constraint\Exception;
use PHPUnit\Framework\TestCase;
@@ -38,7 +39,7 @@ abstract class IntegrationTestCase extends TestCase
/**
* @return RuntimeLoaderInterface[]
*/
protected function getRuntimeLoaders()
protected function getRuntimeLoaders(): array
{
return [];
}
@@ -46,7 +47,7 @@ abstract class IntegrationTestCase extends TestCase
/**
* @return ExtensionInterface[]
*/
protected function getExtensions()
protected function getExtensions(): array
{
return [];
}
@@ -54,7 +55,7 @@ abstract class IntegrationTestCase extends TestCase
/**
* @return TwigFilter[]
*/
protected function getTwigFilters()
protected function getTwigFilters(): array
{
return [];
}
@@ -62,7 +63,7 @@ abstract class IntegrationTestCase extends TestCase
/**
* @return TwigFunction[]
*/
protected function getTwigFunctions()
protected function getTwigFunctions(): array
{
return [];
}
@@ -70,7 +71,7 @@ abstract class IntegrationTestCase extends TestCase
/**
* @return TwigTest[]
*/
protected function getTwigTests()
protected function getTwigTests(): array
{
return [];
}
@@ -113,10 +114,7 @@ abstract class IntegrationTestCase extends TestCase
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
}
/**
* @group legacy
*/
#[DataProvider('getLegacyTests'), IgnoreDeprecations]
#[DataProvider('getLegacyTests'), Group('legacy'), IgnoreDeprecations]
public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs, $deprecation = ''): void
{
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
@@ -331,7 +329,7 @@ abstract class IntegrationTestCase extends TestCase
/**
* @return array<string, string>
*/
protected static function parseTemplates($test)
protected static function parseTemplates($test): array
{
$templates = [];
preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, \PREG_SET_ORDER);
+1 -12
View File
@@ -22,14 +22,6 @@ abstract class NodeTestCase extends TestCase
{
private Environment $currentEnv;
/**
* @return iterable<array{0: Node, 1: string, 2?: Environment|null, 3?: bool}>
*/
public function getTests()
{
return [];
}
/**
* @return iterable<array{0: Node, 1: string, 2?: Environment|null, 3?: bool}>
*/
@@ -53,10 +45,7 @@ abstract class NodeTestCase extends TestCase
}
}
/**
* @return Compiler
*/
protected function getCompiler(?Environment $environment = null)
protected function getCompiler(?Environment $environment = null): Compiler
{
return new Compiler($environment ?? $this->getEnvironment());
}
-3
View File
@@ -250,9 +250,6 @@ class ExpressionParserTest extends TestCase
$this->assertSame('third', $pairs[2]['value']->getAttribute('name'));
}
/**
* @dataProvider getEmptyDestructuringTests
*/
#[DataProvider('getEmptyDestructuringTests')]
public function testEmptyDestructuringThrows(string $template): void
{
+1 -1
View File
@@ -47,7 +47,7 @@ function html()
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
public function getExtensions(): array
{
$policy = new SecurityPolicy([], [], [], [], ['dump']);
-3
View File
@@ -41,9 +41,6 @@ class TextTest extends NodeTestCase
return $tests;
}
/**
* @dataProvider getIsBlankData
*/
#[DataProvider('getIsBlankData')]
public function testIsBlank($blank): void
{
-9
View File
@@ -33,9 +33,6 @@ use Twig\Source;
class CorrectnessTest extends TestCase
{
/**
* @dataProvider getFilterBodyNodesData
*/
#[DataProvider('getFilterBodyNodesData')]
public function testFilterBodyNodes($input, $expected): void
{
@@ -56,9 +53,6 @@ class CorrectnessTest extends TestCase
];
}
/**
* @dataProvider getFilterBodyNodesDataThrowsException
*/
#[DataProvider('getFilterBodyNodesDataThrowsException')]
public function testFilterBodyNodesThrowsException($input): void
{
@@ -76,9 +70,6 @@ class CorrectnessTest extends TestCase
];
}
/**
* @dataProvider getFilterBodyNodesWithBOMData
*/
#[DataProvider('getFilterBodyNodesWithBOMData')]
public function testFilterBodyNodesWithBOM($emptyText): void
{
-3
View File
@@ -127,9 +127,6 @@ EOF, 'index')));
$this->addToAssertionCount(1);
}
/**
* @dataProvider provideMacroTargetExpressions
*/
#[DataProvider('provideMacroTargetExpressions')]
public function testMacroTargetsOnlyCompileAsMacroReferences(string $expression): void
{
-21
View File
@@ -239,9 +239,6 @@ class TemplateTest extends TestCase
$this->assertSame('value', $twig->render('index', ['array' => ['string' => 'value'], 'object' => $key]));
}
/**
* @dataProvider getStringableKeyArrayAccessContainers
*/
#[DataProvider('getStringableKeyArrayAccessContainers')]
public function testStringableKeyIsCoercedForInternalArrayAccess(bool $strict, bool $sandboxed, \ArrayAccess $data): void
{
@@ -269,9 +266,6 @@ class TemplateTest extends TestCase
}
}
/**
* @dataProvider getStrictVariablesModes
*/
#[DataProvider('getStrictVariablesModes')]
public function testArrayAccessWithObjectKeyKeepsTheObjectKey(bool $strict): void
{
@@ -288,9 +282,6 @@ class TemplateTest extends TestCase
$this->assertSame(0, $key->toStringCalls);
}
/**
* @dataProvider getStrictVariablesModes
*/
#[DataProvider('getStrictVariablesModes')]
public function testArrayAccessLookupDoesNotRepeatOffsetChecks(bool $strict): void
{
@@ -319,9 +310,6 @@ class TemplateTest extends TestCase
$this->assertSame([], $data->offsetGetCalls);
}
/**
* @dataProvider getStrictVariablesModes
*/
#[DataProvider('getStrictVariablesModes')]
public function testRejectedStringableArrayAccessKeyRethrowsOriginalTypeError(bool $strict): void
{
@@ -344,9 +332,6 @@ class TemplateTest extends TestCase
$this->assertSame(0, $key->toStringCalls);
}
/**
* @dataProvider getStrictVariablesModes
*/
#[DataProvider('getStrictVariablesModes')]
public function testSandboxDoesNotAuthorizeStringPropertyForArrayAccessObjectKey(bool $strict): void
{
@@ -368,9 +353,6 @@ class TemplateTest extends TestCase
}
}
/**
* @dataProvider getStrictVariablesModes
*/
#[DataProvider('getStrictVariablesModes')]
public function testArrayWithStringableKeyIsCheckedBySandbox(bool $strict): void
{
@@ -395,9 +377,6 @@ class TemplateTest extends TestCase
$this->assertSame(1, $key->toStringCalls);
}
/**
* @dataProvider getStrictVariablesModes
*/
#[DataProvider('getStrictVariablesModes')]
public function testInternalArrayAccessWithStringableKeyIsCheckedBySandbox(bool $strict): void
{
+27
View File
@@ -0,0 +1,27 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Tests\Test;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Twig\Test\IntegrationTestCase;
final class IntegrationTestCaseTest extends TestCase
{
public function testLegacyIntegrationTestsAreInLegacyGroup(): void
{
$attributes = (new \ReflectionMethod(IntegrationTestCase::class, 'testLegacyIntegration'))->getAttributes(Group::class);
$this->assertCount(1, $attributes);
$this->assertSame('legacy', $attributes[0]->newInstance()->name());
}
}