function TwigEnvironmentTest::testTemplateNotFoundException

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

Tests that exceptions are thrown when a template is not found.

File

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

Class

TwigEnvironmentTest
Tests the twig environment.

Namespace

Drupal\KernelTests\Core\Theme

Code

public function testTemplateNotFoundException() {
    
    /** @var \Drupal\Core\Template\TwigEnvironment $environment */
    $environment = \Drupal::service('twig');
    try {
        $environment->loadTemplate('this-template-does-not-exist.html.twig')
            ->render([]);
        $this->fail('Did not throw an exception as expected.');
    } catch (\Twig_Error_Loader $e) {
        $this->assertStringStartsWith('Template "this-template-does-not-exist.html.twig" is not defined', $e->getMessage());
    }
}

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