1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) 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
12namespace Twig\Test;
13
14use PHPUnit\Framework\TestCase;
15use Twig\Environment;
16use Twig\Error\Error;
17use Twig\Extension\ExtensionInterface;
18use Twig\Loader\ArrayLoader;
19use Twig\RuntimeLoader\RuntimeLoaderInterface;
20use Twig\TwigFilter;
21use Twig\TwigFunction;
22use Twig\TwigTest;
23
24/**
25 * Integration test helper.
26 *
27 * @author Fabien Potencier <fabien@symfony.com>
28 * @author Karma Dordrak <drak@zikula.org>
29 */
30abstract class IntegrationTestCase extends TestCase
31{
32    /**
33     * @return string
34     */
35    abstract protected function getFixturesDir();
36
37    /**
38     * @return RuntimeLoaderInterface[]
39     */
40    protected function getRuntimeLoaders()
41    {
42        return [];
43    }
44
45    /**
46     * @return ExtensionInterface[]
47     */
48    protected function getExtensions()
49    {
50        return [];
51    }
52
53    /**
54     * @return TwigFilter[]
55     */
56    protected function getTwigFilters()
57    {
58        return [];
59    }
60
61    /**
62     * @return TwigFunction[]
63     */
64    protected function getTwigFunctions()
65    {
66        return [];
67    }
68
69    /**
70     * @return TwigTest[]
71     */
72    protected function getTwigTests()
73    {
74        return [];
75    }
76
77    /**
78     * @dataProvider getTests
79     */
80    public function testIntegration($file, $message, $condition, $templates, $exception, $outputs, $deprecation = '')
81    {
82        $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
83    }
84
85    /**
86     * @dataProvider getLegacyTests
87     * @group legacy
88     */
89    public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs, $deprecation = '')
90    {
91        $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
92    }
93
94    public function getTests($name, $legacyTests = false)
95    {
96        $fixturesDir = realpath($this->getFixturesDir());
97        $tests = [];
98
99        foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
100            if (!preg_match('/\.test$/', $file)) {
101                continue;
102            }
103
104            if ($legacyTests xor false !== strpos($file->getRealpath(), '.legacy.test')) {
105                continue;
106            }
107
108            $test = file_get_contents($file->getRealpath());
109
110            if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
111                $message = $match[1];
112                $condition = $match[2];
113                $deprecation = $match[3];
114                $templates = self::parseTemplates($match[4]);
115                $exception = $match[6];
116                $outputs = [[null, $match[5], null, '']];
117            } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
118                $message = $match[1];
119                $condition = $match[2];
120                $deprecation = $match[3];
121                $templates = self::parseTemplates($match[4]);
122                $exception = false;
123                preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, \PREG_SET_ORDER);
124            } else {
125                throw new \InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
126            }
127
128            $tests[] = [str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs, $deprecation];
129        }
130
131        if ($legacyTests && empty($tests)) {
132            // add a dummy test to avoid a PHPUnit message
133            return [['not', '-', '', [], '', []]];
134        }
135
136        return $tests;
137    }
138
139    public function getLegacyTests()
140    {
141        return $this->getTests('testLegacyIntegration', true);
142    }
143
144    protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation = '')
145    {
146        if (!$outputs) {
147            $this->markTestSkipped('no tests to run');
148        }
149
150        if ($condition) {
151            eval('$ret = '.$condition.';');
152            if (!$ret) {
153                $this->markTestSkipped($condition);
154            }
155        }
156
157        $loader = new ArrayLoader($templates);
158
159        foreach ($outputs as $i => $match) {
160            $config = array_merge([
161                'cache' => false,
162                'strict_variables' => true,
163            ], $match[2] ? eval($match[2].';') : []);
164            $twig = new Environment($loader, $config);
165            $twig->addGlobal('global', 'global');
166            foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
167                $twig->addRuntimeLoader($runtimeLoader);
168            }
169
170            foreach ($this->getExtensions() as $extension) {
171                $twig->addExtension($extension);
172            }
173
174            foreach ($this->getTwigFilters() as $filter) {
175                $twig->addFilter($filter);
176            }
177
178            foreach ($this->getTwigTests() as $test) {
179                $twig->addTest($test);
180            }
181
182            foreach ($this->getTwigFunctions() as $function) {
183                $twig->addFunction($function);
184            }
185
186            // avoid using the same PHP class name for different cases
187            $p = new \ReflectionProperty($twig, 'templateClassPrefix');
188            $p->setAccessible(true);
189            $p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_');
190
191            $deprecations = [];
192            try {
193                $prevHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$deprecations, &$prevHandler) {
194                    if (\E_USER_DEPRECATED === $type) {
195                        $deprecations[] = $msg;
196
197                        return true;
198                    }
199
200                    return $prevHandler ? $prevHandler($type, $msg, $file, $line, $context) : false;
201                });
202
203                $template = $twig->load('index.twig');
204            } catch (\Exception $e) {
205                if (false !== $exception) {
206                    $message = $e->getMessage();
207                    $this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $message)));
208                    $last = substr($message, \strlen($message) - 1);
209                    $this->assertTrue('.' === $last || '?' === $last, 'Exception message must end with a dot or a question mark.');
210
211                    return;
212                }
213
214                throw new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
215            } finally {
216                restore_error_handler();
217            }
218
219            $this->assertSame($deprecation, implode("\n", $deprecations));
220
221            try {
222                $output = trim($template->render(eval($match[1].';')), "\n ");
223            } catch (\Exception $e) {
224                if (false !== $exception) {
225                    $this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $e->getMessage())));
226
227                    return;
228                }
229
230                $e = new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
231
232                $output = trim(sprintf('%s: %s', \get_class($e), $e->getMessage()));
233            }
234
235            if (false !== $exception) {
236                list($class) = explode(':', $exception);
237                $constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception';
238                $this->assertThat(null, new $constraintClass($class));
239            }
240
241            $expected = trim($match[3], "\n ");
242
243            if ($expected !== $output) {
244                printf("Compiled templates that failed on case %d:\n", $i + 1);
245
246                foreach (array_keys($templates) as $name) {
247                    echo "Template: $name\n";
248                    echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext($name))));
249                }
250            }
251            $this->assertEquals($expected, $output, $message.' (in '.$file.')');
252        }
253    }
254
255    protected static function parseTemplates($test)
256    {
257        $templates = [];
258        preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, \PREG_SET_ORDER);
259        foreach ($matches as $match) {
260            $templates[($match[1] ?: 'index.twig')] = $match[2];
261        }
262
263        return $templates;
264    }
265}
266
267class_alias('Twig\Test\IntegrationTestCase', 'Twig_Test_IntegrationTestCase');
268