: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* This file is part of Twig.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
namespace WPML\Core\Twig\Test;
use WPML\Core\PHPUnit\Framework\TestCase;
use WPML\Core\Twig\Environment;
use WPML\Core\Twig\Error\Error;
use WPML\Core\Twig\Extension\ExtensionInterface;
use WPML\Core\Twig\Loader\ArrayLoader;
use WPML\Core\Twig\Loader\SourceContextLoaderInterface;
use WPML\Core\Twig\RuntimeLoader\RuntimeLoaderInterface;
use WPML\Core\Twig\Source;
use WPML\Core\Twig\TwigFilter;
use WPML\Core\Twig\TwigFunction;
use WPML\Core\Twig\TwigTest;
* Integration test helper.
* @author Fabien Potencier <fabien@symfony.com>
* @author Karma Dordrak <drak@zikula.org>
abstract class IntegrationTestCase extends \WPML\Core\PHPUnit\Framework\TestCase
protected abstract function getFixturesDir();
* @return RuntimeLoaderInterface[]
protected function getRuntimeLoaders()
* @return ExtensionInterface[]
protected function getExtensions()
protected function getTwigFilters()
protected function getTwigFunctions()
protected function getTwigTests()
public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
* @dataProvider getLegacyTests
public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs)
$this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
public function getTests($name, $legacyTests = \false)
$fixturesDir = \realpath($this->getFixturesDir());
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
if (!\preg_match('/\\.test$/', $file)) {
if ($legacyTests xor \false !== \strpos($file->getRealpath(), '.legacy.test')) {
$test = \file_get_contents($file->getRealpath());
if (\preg_match('/--TEST--\\s*(.*?)\\s*(?:--CONDITION--\\s*(.*))?\\s*((?:--TEMPLATE(?:\\(.*?\\))?--(?:.*?))+)\\s*(?:--DATA--\\s*(.*))?\\s*--EXCEPTION--\\s*(.*)/sx', $test, $match)) {
$templates = self::parseTemplates($match[3]);
$outputs = [[null, $match[4], null, '']];
} elseif (\preg_match('/--TEST--\\s*(.*?)\\s*(?:--CONDITION--\\s*(.*))?\\s*((?:--TEMPLATE(?:\\(.*?\\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
$templates = self::parseTemplates($match[3]);
\preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\\-\\-DATA\\-\\-|$)/s', $test, $outputs, \PREG_SET_ORDER);
throw new \InvalidArgumentException(\sprintf('Test "%s" is not valid.', \str_replace($fixturesDir . '/', '', $file)));
$tests[] = [\str_replace($fixturesDir . '/', '', $file), $message, $condition, $templates, $exception, $outputs];
if ($legacyTests && empty($tests)) {
// add a dummy test to avoid a PHPUnit message
return [['not', '-', '', [], '', []]];
public function getLegacyTests()
return $this->getTests('testLegacyIntegration', \true);
protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
$this->markTestSkipped('no tests to run');
eval('$ret = ' . $condition . ';');
$this->markTestSkipped($condition);
$loader = new \WPML\Core\Twig\Loader\ArrayLoader($templates);
foreach ($outputs as $i => $match) {
$config = \array_merge(['cache' => \false, 'strict_variables' => \true], $match[2] ? eval($match[2] . ';') : []);
$twig = new \WPML\Core\Twig\Environment($loader, $config);
$twig->addGlobal('global', 'global');
foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
$twig->addRuntimeLoader($runtimeLoader);
foreach ($this->getExtensions() as $extension) {
$twig->addExtension($extension);
foreach ($this->getTwigFilters() as $filter) {
$twig->addFilter($filter);
foreach ($this->getTwigTests() as $test) {
foreach ($this->getTwigFunctions() as $function) {
$twig->addFunction($function);
$p = new \ReflectionProperty($twig, 'templateClassPrefix');
$p->setAccessible(\true);
$p->setValue($twig, '\\WPML\\Core\\__TwigTemplate_' . \hash('sha256', \uniqid(\mt_rand(), \true), \false) . '_');
$template = $twig->load('index.twig');
} catch (\Exception $e) {
if (\false !== $exception) {
$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, 'Exception message must end with a dot or a question mark.');
throw new \WPML\Core\Twig\Error\Error(\sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
$output = \trim($template->render(eval($match[1] . ';')), "\n ");
} catch (\Exception $e) {
if (\false !== $exception) {
$this->assertSame(\trim($exception), \trim(\sprintf('%s: %s', \get_class($e), $e->getMessage())));
$e = new \WPML\Core\Twig\Error\Error(\sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
$output = \trim(\sprintf('%s: %s', \get_class($e), $e->getMessage()));
if (\false !== $exception) {
list($class) = \explode(':', $exception);
$constraintClass = \class_exists('WPML\\Core\\PHPUnit\\Framework\\Constraint\\Exception') ? 'PHPUnit\\Framework\\Constraint\\Exception' : 'PHPUnit_Framework_Constraint_Exception';
$this->assertThat(null, new $constraintClass($class));
$expected = \trim($match[3], "\n ");
if ($expected !== $output) {
\printf("Compiled templates that failed on case %d:\n", $i + 1);
foreach (\array_keys($templates) as $name) {
echo "Template: {$name}\n";
$loader = $twig->getLoader();
if (!$loader instanceof \WPML\Core\Twig\Loader\SourceContextLoaderInterface) {
$source = new \WPML\Core\Twig\Source($loader->getSource($name), $name);
$source = $loader->getSourceContext($name);
echo $twig->compile($twig->parse($twig->tokenize($source)));
$this->assertEquals($expected, $output, $message . ' (in ' . $file . ')');
protected static function parseTemplates($test)
\preg_match_all('/--TEMPLATE(?:\\((.*?)\\))?--(.*?)(?=\\-\\-TEMPLATE|$)/s', $test, $matches, \PREG_SET_ORDER);
foreach ($matches as $match) {
$templates[$match[1] ? $match[1] : 'index.twig'] = $match[2];
\class_alias('WPML\\Core\\Twig\\Test\\IntegrationTestCase', 'WPML\\Core\\Twig_Test_IntegrationTestCase');