1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-22 20:35:54 +01:00
vichan/inc/lib/Twig/Test/NodeTestCase.php

76 lines
2.1 KiB
PHP
Raw Normal View History

2013-08-01 21:20:12 +02: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.
*/
2018-05-10 12:24:53 +02:00
use PHPUnit\Framework\TestCase;
abstract class Twig_Test_NodeTestCase extends TestCase
2013-08-01 21:20:12 +02:00
{
abstract public function getTests();
/**
* @dataProvider getTests
*/
2018-05-10 12:24:53 +02:00
public function testCompile($node, $source, $environment = null, $isPattern = false)
2013-08-01 21:20:12 +02:00
{
2018-05-10 12:24:53 +02:00
$this->assertNodeCompilation($source, $node, $environment, $isPattern);
2013-08-01 21:20:12 +02:00
}
2018-05-10 12:24:53 +02:00
public function assertNodeCompilation($source, Twig_Node $node, Twig_Environment $environment = null, $isPattern = false)
2013-08-01 21:20:12 +02:00
{
$compiler = $this->getCompiler($environment);
$compiler->compile($node);
2018-05-10 12:24:53 +02:00
if ($isPattern) {
$this->assertStringMatchesFormat($source, trim($compiler->getSource()));
} else {
$this->assertEquals($source, trim($compiler->getSource()));
}
2013-08-01 21:20:12 +02:00
}
protected function getCompiler(Twig_Environment $environment = null)
{
return new Twig_Compiler(null === $environment ? $this->getEnvironment() : $environment);
}
protected function getEnvironment()
{
2018-05-10 12:24:53 +02:00
return new Twig_Environment(new Twig_Loader_Array(array()));
2013-08-01 21:20:12 +02:00
}
2018-05-10 12:24:53 +02:00
protected function getVariableGetter($name, $line = false)
2013-08-01 21:20:12 +02:00
{
2018-05-10 12:24:53 +02:00
$line = $line > 0 ? "// line {$line}\n" : '';
if (PHP_VERSION_ID >= 70000) {
return sprintf('%s($context["%s"] ?? null)', $line, $name, $name);
2013-08-01 21:20:12 +02:00
}
2018-05-10 12:24:53 +02:00
if (PHP_VERSION_ID >= 50400) {
return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
}
return sprintf('%s$this->getContext($context, "%s")', $line, $name);
2013-08-01 21:20:12 +02:00
}
protected function getAttributeGetter()
{
if (function_exists('twig_template_get_attributes')) {
return 'twig_template_get_attributes($this, ';
}
return '$this->getAttribute(';
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_Test_NodeTestCase', 'Twig\Test\NodeTestCase', false);
class_exists('Twig_Environment');
class_exists('Twig_Node');