You are here

protected function Twig_Test_IntegrationTestCase::doIntegrationTest in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/twig/twig/lib/Twig/Test/IntegrationTestCase.php \Twig_Test_IntegrationTestCase::doIntegrationTest()
2 calls to Twig_Test_IntegrationTestCase::doIntegrationTest()
Twig_Test_IntegrationTestCase::testIntegration in vendor/twig/twig/lib/Twig/Test/IntegrationTestCase.php
@dataProvider getTests
Twig_Test_IntegrationTestCase::testLegacyIntegration in vendor/twig/twig/lib/Twig/Test/IntegrationTestCase.php
@dataProvider getLegacyTests @group legacy

File

vendor/twig/twig/lib/Twig/Test/IntegrationTestCase.php, line 122

Class

Twig_Test_IntegrationTestCase
Integration test helper.

Code

protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs) {
  if ($condition) {
    eval('$ret = ' . $condition . ';');
    if (!$ret) {
      $this
        ->markTestSkipped($condition);
    }
  }
  $loader = new Twig_Loader_Array($templates);
  foreach ($outputs as $i => $match) {
    $config = array_merge(array(
      'cache' => false,
      'strict_variables' => true,
    ), $match[2] ? eval($match[2] . ';') : array());
    $twig = new Twig_Environment($loader, $config);
    $twig
      ->addGlobal('global', 'global');
    foreach ($this
      ->getExtensions() as $extension) {
      $twig
        ->addExtension($extension);
    }
    foreach ($this
      ->getTwigFilters() as $filter) {
      $twig
        ->addFilter($filter);
    }
    foreach ($this
      ->getTwigTests() as $test) {
      $twig
        ->addTest($test);
    }
    foreach ($this
      ->getTwigFunctions() as $function) {
      $twig
        ->addFunction($function);
    }

    // avoid using the same PHP class name for different cases
    // only for PHP 5.2+
    if (PHP_VERSION_ID >= 50300) {
      $p = new ReflectionProperty($twig, 'templateClassPrefix');
      $p
        ->setAccessible(true);
      $p
        ->setValue($twig, '__TwigTemplate_' . hash('sha256', uniqid(mt_rand(), true), false) . '_');
    }
    try {
      $template = $twig
        ->loadTemplate('index.twig');
    } catch (Exception $e) {
      if (false !== $exception) {
        $message = $e
          ->getMessage();
        $this
          ->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $message)));
        $this
          ->assertSame('.', substr($message, strlen($message) - 1), $message, 'Exception message must end with a dot.');
        return;
      }
      if ($e instanceof Twig_Error_Syntax) {
        $e
          ->setTemplateFile($file);
        throw $e;
      }
      throw new Twig_Error(sprintf('%s: %s', get_class($e), $e
        ->getMessage()), -1, $file, $e);
    }
    try {
      $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())));
        return;
      }
      if ($e instanceof Twig_Error_Syntax) {
        $e
          ->setTemplateFile($file);
      }
      else {
        $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e
          ->getMessage()), -1, $file, $e);
      }
      $output = trim(sprintf('%s: %s', get_class($e), $e
        ->getMessage()));
    }
    if (false !== $exception) {
      list($class) = explode(':', $exception);
      $this
        ->assertThat(null, new PHPUnit_Framework_Constraint_Exception($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";
        $source = $loader
          ->getSource($name);
        echo $twig
          ->compile($twig
          ->parse($twig
          ->tokenize($source, $name)));
      }
    }
    $this
      ->assertEquals($expected, $output, $message . ' (in ' . $file . ')');
  }
}