1 |
1852
|
darkviper
|
<?php
|
2 |
|
|
|
3 |
|
|
/*
|
4 |
|
|
* This file is part of Twig.
|
5 |
|
|
*
|
6 |
|
|
* (c) 2010 Fabien Potencier
|
7 |
|
|
*
|
8 |
|
|
* For the full copyright and license information, please view the LICENSE
|
9 |
|
|
* file that was distributed with this source code.
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
|
|
/**
|
13 |
|
|
* Integration test helper
|
14 |
|
|
*
|
15 |
|
|
* @package twig
|
16 |
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
17 |
|
|
* @author Karma Dordrak <drak@zikula.org>
|
18 |
|
|
*/
|
19 |
|
|
abstract class Twig_Test_IntegrationTestCase extends PHPUnit_Framework_TestCase
|
20 |
|
|
{
|
21 |
|
|
abstract protected function getExtensions();
|
22 |
|
|
abstract protected function getFixturesDir();
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* @dataProvider getTests
|
26 |
|
|
*/
|
27 |
|
|
public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
|
28 |
|
|
{
|
29 |
|
|
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
public function getTests()
|
33 |
|
|
{
|
34 |
|
|
$fixturesDir = realpath($this->getFixturesDir());
|
35 |
|
|
$tests = array();
|
36 |
|
|
|
37 |
|
|
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
|
38 |
|
|
if (!preg_match('/\.test$/', $file)) {
|
39 |
|
|
continue;
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
$test = file_get_contents($file->getRealpath());
|
43 |
|
|
|
44 |
|
|
if (preg_match('/
|
45 |
|
|
--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
|
46 |
|
|
$message = $match[1];
|
47 |
|
|
$condition = $match[2];
|
48 |
|
|
$templates = $this->parseTemplates($match[3]);
|
49 |
|
|
$exception = $match[5];
|
50 |
|
|
$outputs = array(array(null, $match[4], null, ''));
|
51 |
|
|
} elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
|
52 |
|
|
$message = $match[1];
|
53 |
|
|
$condition = $match[2];
|
54 |
|
|
$templates = $this->parseTemplates($match[3]);
|
55 |
|
|
$exception = false;
|
56 |
|
|
preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
|
57 |
|
|
} else {
|
58 |
|
|
throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
$tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
return $tests;
|
65 |
|
|
}
|
66 |
|
|
|
67 |
|
|
protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
|
68 |
|
|
{
|
69 |
|
|
if ($condition) {
|
70 |
|
|
eval('$ret = '.$condition.';');
|
71 |
|
|
if (!$ret) {
|
72 |
|
|
$this->markTestSkipped($condition);
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
$loader = new Twig_Loader_Array($templates);
|
77 |
|
|
|
78 |
|
|
foreach ($outputs as $match) {
|
79 |
|
|
$config = array_merge(array(
|
80 |
|
|
'cache' => false,
|
81 |
|
|
'strict_variables' => true,
|
82 |
|
|
), $match[2] ? eval($match[2].';') : array());
|
83 |
|
|
$twig = new Twig_Environment($loader, $config);
|
84 |
|
|
$twig->addGlobal('global', 'global');
|
85 |
|
|
foreach ($this->getExtensions() as $extension) {
|
86 |
|
|
$twig->addExtension($extension);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
try {
|
90 |
|
|
$template = $twig->loadTemplate('index.twig');
|
91 |
|
|
} catch (Exception $e) {
|
92 |
|
|
if (false !== $exception) {
|
93 |
|
|
$this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
|
94 |
|
|
|
95 |
|
|
return;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
if ($e instanceof Twig_Error_Syntax) {
|
99 |
|
|
$e->setTemplateFile($file);
|
100 |
|
|
|
101 |
|
|
throw $e;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
try {
|
108 |
|
|
$output = trim($template->render(eval($match[1].';')), "\n ");
|
109 |
|
|
} catch (Exception $e) {
|
110 |
|
|
if (false !== $exception) {
|
111 |
|
|
$this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
|
112 |
|
|
|
113 |
|
|
return;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
if ($e instanceof Twig_Error_Syntax) {
|
117 |
|
|
$e->setTemplateFile($file);
|
118 |
|
|
} else {
|
119 |
|
|
$e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
$output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
if (false !== $exception) {
|
126 |
|
|
list($class, ) = explode(':', $exception);
|
127 |
|
|
$this->assertThat(NULL, new PHPUnit_Framework_Constraint_Exception($class));
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
$expected = trim($match[3], "\n ");
|
131 |
|
|
|
132 |
|
|
if ($expected != $output) {
|
133 |
|
|
echo 'Compiled template that failed:';
|
134 |
|
|
|
135 |
|
|
foreach (array_keys($templates) as $name) {
|
136 |
|
|
echo "Template: $name\n";
|
137 |
|
|
$source = $loader->getSource($name);
|
138 |
|
|
echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
$this->assertEquals($expected, $output, $message.' (in '.$file.')');
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
protected static function parseTemplates($test)
|
146 |
|
|
{
|
147 |
|
|
$templates = array();
|
148 |
|
|
preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
|
149 |
|
|
foreach ($matches as $match) {
|
150 |
|
|
$templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
return $templates;
|
154 |
|
|
}
|
155 |
|
|
}
|