mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-08 16:36:58 +00:00
Move some tests
This commit is contained in:
@@ -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' %}
|
||||
@@ -0,0 +1 @@
|
||||
{% include "include.twig" %}
|
||||
@@ -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 [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user