PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev1
Elementor Website Builder – more than just a page builder v3.28.0-dev1
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / vendor_prefixed / twig / src / Test / IntegrationTestCase.php

IntegrationTestCase.php in Elementor Website Builder – more than just a page builder 3.28.0-dev1, at vendor_prefixed/twig/src/Test/IntegrationTestCase.php

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