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/IntegrationTestCase.php

250 lines
8.3 KiB
PHP
Raw Normal View History

2013-08-01 21:20:12 +02:00
<?php
/*
* This file is part of Twig.
*
2018-05-10 12:24:53 +02:00
* (c) Fabien Potencier
2013-08-01 21:20:12 +02:00
*
* 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;
2013-08-01 21:20:12 +02:00
/**
2018-05-10 12:24:53 +02:00
* Integration test helper.
2013-08-01 21:20:12 +02:00
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Karma Dordrak <drak@zikula.org>
*/
2018-05-10 12:24:53 +02:00
abstract class Twig_Test_IntegrationTestCase extends TestCase
2013-08-01 21:20:12 +02:00
{
2018-05-10 12:24:53 +02:00
/**
* @return string
*/
2013-08-01 21:20:12 +02:00
abstract protected function getFixturesDir();
2018-05-10 12:24:53 +02:00
/**
* @return Twig_RuntimeLoaderInterface[]
*/
protected function getRuntimeLoaders()
{
return array();
}
/**
* @return Twig_ExtensionInterface[]
*/
protected function getExtensions()
{
return array();
}
/**
* @return Twig_SimpleFilter[]
*/
protected function getTwigFilters()
{
return array();
}
/**
* @return Twig_SimpleFunction[]
*/
protected function getTwigFunctions()
{
return array();
}
/**
* @return Twig_SimpleTest[]
*/
protected function getTwigTests()
{
return array();
}
2013-08-01 21:20:12 +02:00
/**
* @dataProvider getTests
*/
public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
{
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
}
2018-05-10 12:24:53 +02:00
/**
* @dataProvider getLegacyTests
* @group legacy
*/
public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs)
{
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
}
public function getTests($name, $legacyTests = false)
2013-08-01 21:20:12 +02:00
{
$fixturesDir = realpath($this->getFixturesDir());
$tests = array();
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
if (!preg_match('/\.test$/', $file)) {
continue;
}
2018-05-10 12:24:53 +02:00
if ($legacyTests xor false !== strpos($file->getRealpath(), '.legacy.test')) {
continue;
}
2013-08-01 21:20:12 +02:00
$test = file_get_contents($file->getRealpath());
2018-05-10 12:24:53 +02:00
if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
2013-08-01 21:20:12 +02:00
$message = $match[1];
$condition = $match[2];
2018-05-10 12:24:53 +02:00
$templates = self::parseTemplates($match[3]);
2013-08-01 21:20:12 +02:00
$exception = $match[5];
$outputs = array(array(null, $match[4], null, ''));
} elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
$message = $match[1];
$condition = $match[2];
2018-05-10 12:24:53 +02:00
$templates = self::parseTemplates($match[3]);
2013-08-01 21:20:12 +02:00
$exception = false;
preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
} else {
throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
}
$tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
}
2018-05-10 12:24:53 +02:00
if ($legacyTests && empty($tests)) {
// add a dummy test to avoid a PHPUnit message
return array(array('not', '-', '', array(), '', array()));
}
2013-08-01 21:20:12 +02:00
return $tests;
}
2018-05-10 12:24:53 +02:00
public function getLegacyTests()
{
return $this->getTests('testLegacyIntegration', true);
}
2013-08-01 21:20:12 +02:00
protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
{
2018-05-10 12:24:53 +02:00
if (!$outputs) {
$this->markTestSkipped('no legacy tests to run');
}
2013-08-01 21:20:12 +02:00
if ($condition) {
eval('$ret = '.$condition.';');
if (!$ret) {
$this->markTestSkipped($condition);
}
}
$loader = new Twig_Loader_Array($templates);
2018-05-10 12:24:53 +02:00
foreach ($outputs as $i => $match) {
2013-08-01 21:20:12 +02:00
$config = array_merge(array(
'cache' => false,
'strict_variables' => true,
), $match[2] ? eval($match[2].';') : array());
$twig = new Twig_Environment($loader, $config);
$twig->addGlobal('global', 'global');
2018-05-10 12:24:53 +02:00
foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
$twig->addRuntimeLoader($runtimeLoader);
}
2013-08-01 21:20:12 +02:00
foreach ($this->getExtensions() as $extension) {
$twig->addExtension($extension);
}
2018-05-10 12:24:53 +02:00
foreach ($this->getTwigFilters() as $filter) {
$twig->addFilter($filter);
}
foreach ($this->getTwigTests() as $test) {
$twig->addTest($test);
}
foreach ($this->getTwigFunctions() as $function) {
$twig->addFunction($function);
}
// avoid using the same PHP class name for different cases
// only for PHP 5.2+
if (PHP_VERSION_ID >= 50300) {
$p = new ReflectionProperty($twig, 'templateClassPrefix');
$p->setAccessible(true);
$p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_');
}
2013-08-01 21:20:12 +02:00
try {
$template = $twig->loadTemplate('index.twig');
} catch (Exception $e) {
if (false !== $exception) {
2018-05-10 12:24:53 +02:00
$message = $e->getMessage();
$this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $message)));
$last = substr($message, strlen($message) - 1);
$this->assertTrue('.' === $last || '?' === $last, $message, 'Exception message must end with a dot or a question mark.');
2013-08-01 21:20:12 +02:00
return;
}
throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
}
try {
$output = trim($template->render(eval($match[1].';')), "\n ");
} catch (Exception $e) {
if (false !== $exception) {
2018-05-10 12:24:53 +02:00
$this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
2013-08-01 21:20:12 +02:00
return;
}
2018-05-10 12:24:53 +02:00
$e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
2013-08-01 21:20:12 +02:00
$output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
}
if (false !== $exception) {
2018-05-10 12:24:53 +02:00
list($class) = explode(':', $exception);
$constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception';
$this->assertThat(null, new $constraintClass($class));
2013-08-01 21:20:12 +02:00
}
$expected = trim($match[3], "\n ");
2018-05-10 12:24:53 +02:00
if ($expected !== $output) {
printf("Compiled templates that failed on case %d:\n", $i + 1);
2013-08-01 21:20:12 +02:00
foreach (array_keys($templates) as $name) {
echo "Template: $name\n";
2018-05-10 12:24:53 +02:00
$loader = $twig->getLoader();
if (!$loader instanceof Twig_SourceContextLoaderInterface) {
$source = new Twig_Source($loader->getSource($name), $name);
} else {
$source = $loader->getSourceContext($name);
}
echo $twig->compile($twig->parse($twig->tokenize($source)));
2013-08-01 21:20:12 +02:00
}
}
$this->assertEquals($expected, $output, $message.' (in '.$file.')');
}
}
protected static function parseTemplates($test)
{
$templates = array();
preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
}
return $templates;
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_Test_IntegrationTestCase', 'Twig\Test\IntegrationTestCase', false);