leilukin-tumbleblog/includes/lib/Twig/Test/NodeTestCase.php

126 lines
3.6 KiB
PHP
Raw Normal View History

2024-06-20 14:10:42 +00:00
<?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\Test;
2025-01-13 09:56:01 +00:00
use PHPUnit\Framework\Attributes\BeforeClass;
use PHPUnit\Framework\Attributes\DataProvider;
2024-06-20 14:10:42 +00:00
use PHPUnit\Framework\TestCase;
use Twig\Compiler;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Node\Node;
abstract class NodeTestCase extends TestCase
{
/**
* @var Environment
*/
private $currentEnv;
2025-01-13 09:56:01 +00:00
public function getTests()
{
return [];
}
/**
* @return iterable<array{0: Node, 1: string, 2?: Environment|null, 3?: bool}>
*/
public static function provideTests(): iterable
{
trigger_deprecation('twig/twig', '3.13', 'Not implementing "%s()" in "%s" is deprecated. This method will be abstract in 4.0.', __METHOD__, static::class);
return [];
}
2024-06-20 14:10:42 +00:00
/**
* @dataProvider getTests
2025-01-13 09:56:01 +00:00
* @dataProvider provideTests
2024-06-20 14:10:42 +00:00
*/
2025-01-13 09:56:01 +00:00
#[DataProvider('getTests'), DataProvider('provideTests')]
2024-06-20 14:10:42 +00:00
public function testCompile($node, $source, $environment = null, $isPattern = false)
{
$this->assertNodeCompilation($source, $node, $environment, $isPattern);
}
public function assertNodeCompilation($source, Node $node, ?Environment $environment = null, $isPattern = false)
{
$compiler = $this->getCompiler($environment);
$compiler->compile($node);
if ($isPattern) {
$this->assertStringMatchesFormat($source, trim($compiler->getSource()));
} else {
$this->assertEquals($source, trim($compiler->getSource()));
}
}
protected function getCompiler(?Environment $environment = null)
{
return new Compiler($environment ?? $this->getEnvironment());
}
2025-01-13 09:56:01 +00:00
/**
* @final since Twig 3.13
*/
2024-06-20 14:10:42 +00:00
protected function getEnvironment()
{
2025-01-13 09:56:01 +00:00
return $this->currentEnv ??= static::createEnvironment();
}
2024-09-05 17:51:48 +00:00
2025-01-13 09:56:01 +00:00
protected static function createEnvironment(): Environment
{
return new Environment(new ArrayLoader());
2024-06-20 14:10:42 +00:00
}
2025-01-13 09:56:01 +00:00
/**
* @deprecated since Twig 3.13, use createVariableGetter() instead.
*/
2024-06-20 14:10:42 +00:00
protected function getVariableGetter($name, $line = false)
2025-01-13 09:56:01 +00:00
{
trigger_deprecation('twig/twig', '3.13', 'Method "%s()" is deprecated, use "createVariableGetter()" instead.', __METHOD__);
return self::createVariableGetter($name, $line);
}
final protected static function createVariableGetter(string $name, bool $line = false): string
2024-06-20 14:10:42 +00:00
{
$line = $line > 0 ? "// line $line\n" : '';
2024-09-05 17:51:48 +00:00
return \sprintf('%s($context["%s"] ?? null)', $line, $name);
2024-06-20 14:10:42 +00:00
}
2025-01-13 09:56:01 +00:00
/**
* @deprecated since Twig 3.13, use createAttributeGetter() instead.
*/
2024-06-20 14:10:42 +00:00
protected function getAttributeGetter()
2025-01-13 09:56:01 +00:00
{
trigger_deprecation('twig/twig', '3.13', 'Method "%s()" is deprecated, use "createAttributeGetter()" instead.', __METHOD__);
return self::createAttributeGetter();
}
final protected static function createAttributeGetter(): string
2024-06-20 14:10:42 +00:00
{
return 'CoreExtension::getAttribute($this->env, $this->source, ';
}
2025-01-13 09:56:01 +00:00
/** @beforeClass */
#[BeforeClass]
final public static function checkDataProvider(): void
{
$r = new \ReflectionMethod(static::class, 'getTests');
if (self::class !== $r->getDeclaringClass()->getName()) {
trigger_deprecation('twig/twig', '3.13', 'Implementing "%s::getTests()" in "%s" is deprecated, implement "provideTests()" instead.', self::class, static::class);
}
}
2024-06-20 14:10:42 +00:00
}