function TwigEnvironmentTest::testTemplateInvalidation

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php \Drupal\KernelTests\Core\Theme\TwigEnvironmentTest::testTemplateInvalidation()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php \Drupal\KernelTests\Core\Theme\TwigEnvironmentTest::testTemplateInvalidation()
  3. 10 core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php \Drupal\KernelTests\Core\Theme\TwigEnvironmentTest::testTemplateInvalidation()

Tests template invalidation.

File

core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php, line 198

Class

TwigEnvironmentTest
Tests the twig environment.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testTemplateInvalidation() : void {
    $template_before = <<<TWIG
<div>Hello before</div>
TWIG;
    $template_after = <<<TWIG
<div>Hello after</div>
TWIG;
    $template_file = tempnam(sys_get_temp_dir(), '__METHOD__') . '.html.twig';
    file_put_contents($template_file, $template_before);
    
    /** @var \Drupal\Core\Template\TwigEnvironment $environment */
    $environment = \Drupal::service('twig');
    $output = $environment->load(basename($template_file))
        ->render();
    $this->assertEquals($template_before, $output);
    file_put_contents($template_file, $template_after);
    $output = $environment->load(basename($template_file))
        ->render();
    $this->assertEquals($template_before, $output);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.