4 * This file is part of Twig.
6 * (c) 2010 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Integration test helper
15 * @author Fabien Potencier <fabien@symfony.com>
16 * @author Karma Dordrak <drak@zikula.org>
18 abstract class Twig_Test_IntegrationTestCase
extends PHPUnit_Framework_TestCase
20 abstract protected function getExtensions();
21 abstract protected function getFixturesDir();
24 * @dataProvider getTests
26 public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
28 $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
31 public function getTests()
33 $fixturesDir = realpath($this->getFixturesDir());
36 foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator
::LEAVES_ONLY
) as $file) {
37 if (!preg_match('/\.test$/', $file)) {
41 $test = file_get_contents($file->getRealpath());
44 --TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
46 $condition = $match[2];
47 $templates = $this->parseTemplates($match[3]);
48 $exception = $match[5];
49 $outputs = array(array(null, $match[4], null, ''));
50 } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
52 $condition = $match[2];
53 $templates = $this->parseTemplates($match[3]);
55 preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER
);
57 throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
60 $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
66 protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
69 eval('$ret = '.$condition.';');
71 $this->markTestSkipped($condition);
75 $loader = new Twig_Loader_Array($templates);
77 foreach ($outputs as $match) {
78 $config = array_merge(array(
80 'strict_variables' => true,
81 ), $match[2] ? eval($match[2].';') : array());
82 $twig = new Twig_Environment($loader, $config);
83 $twig->addGlobal('global', 'global');
84 foreach ($this->getExtensions() as $extension) {
85 $twig->addExtension($extension);
89 $template = $twig->loadTemplate('index.twig');
90 } catch (Exception
$e) {
91 if (false !== $exception) {
92 $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
97 if ($e instanceof Twig_Error_Syntax
) {
98 $e->setTemplateFile($file);
103 throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
107 $output = trim($template->render(eval($match[1].';')), "\n ");
108 } catch (Exception
$e) {
109 if (false !== $exception) {
110 $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
115 if ($e instanceof Twig_Error_Syntax
) {
116 $e->setTemplateFile($file);
118 $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
121 $output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
124 if (false !== $exception) {
125 list($class, ) = explode(':', $exception);
126 $this->assertThat(NULL, new PHPUnit_Framework_Constraint_Exception($class));
129 $expected = trim($match[3], "\n ");
131 if ($expected != $output) {
132 echo 'Compiled template that failed:';
134 foreach (array_keys($templates) as $name) {
135 echo "Template: $name\n";
136 $source = $loader->getSource($name);
137 echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
140 $this->assertEquals($expected, $output, $message.' (in '.$file.')');
144 protected static function parseTemplates($test)
146 $templates = array();
147 preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER
);
148 foreach ($matches as $match) {
149 $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];