Move some tests

This commit is contained in:
Fabien Potencier
2025-02-26 22:08:43 +01:00
parent 33a08191b9
commit 56204e951a
4 changed files with 44 additions and 65 deletions
+39
View File
@@ -16,6 +16,7 @@ use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\ArrayLoader;
@@ -417,6 +418,44 @@ EOHTML,
],
];
}
public function testErrorFromArrayLoader()
{
$templates = [
'index.twig' => '{% include "include.twig" %}',
'include.twig' => $include = <<<EOF
{% extends 'invalid.twig' %}
EOF,
];
$twig = new Environment(new ArrayLoader($templates), ['debug' => true, 'cache' => false]);
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertSame('Template "invalid.twig" is not defined.', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}
public function testErrorFromFilesystemLoader()
{
$twig = new Environment(new FilesystemLoader([$dir = __DIR__.'/Fixtures/errors/extends']), ['debug' => true, 'cache' => false]);
$include = file_get_contents($dir.'/include.twig');
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Unable to find template "invalid.twig"', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}
}
class ErrorTest_Foo
@@ -0,0 +1,4 @@
{% extends 'invalid.twig' %}
+1
View File
@@ -0,0 +1 @@
{% include "include.twig" %}
-65
View File
@@ -1,65 +0,0 @@
<?php
namespace Twig\Tests\Node;
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Test\NodeTestCase;
class ExtendsTest extends NodeTestCase
{
public function testErrorFromArrayLoader()
{
$twig = new Environment(new ArrayLoader([
'index.twig' => '{% include "include.twig" %}',
'include.twig' => $include = <<<EOF
{% extends 'invalid.twig' %}
EOF,
]), ['debug' => true]);
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertSame('Template "invalid.twig" is not defined.', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}
public function testErrorFromFilesystemLoader()
{
$twig = new Environment(new FilesystemLoader([
$dir = dirname(__DIR__).'/Fixtures/templates',
]), ['debug' => true]);
$include = file_get_contents($dir.'/include.twig');
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Unable to find template "invalid.twig"', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}
public static function provideTests(): iterable
{
return [];
}
}