mirror of
https://github.com/twigphp/Twig.git
synced 2026-10-02 18:07:35 +00:00
Allow to call TemplateWrapper::unwrap() without arguments
This commit is contained in:
@@ -121,9 +121,11 @@ final class TemplateWrapper
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function unwrap(Environment $env): Template
|
||||
public function unwrap(?Environment $env = null): Template
|
||||
{
|
||||
if (!$this->isOwnedBy($env)) {
|
||||
if (null === $env) {
|
||||
trigger_deprecation('twig/twig', '3.29', 'Calling "%s()" without arguments is deprecated, pass the Twig environment instead.', __METHOD__);
|
||||
} elseif (!$this->isOwnedBy($env)) {
|
||||
throw new RuntimeError(\sprintf('A "%s" can only be used with the "%s" that created it.', self::class, Environment::class));
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,9 @@ namespace Twig\Tests;
|
||||
*/
|
||||
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
@@ -31,6 +33,8 @@ use Twig\TwigFunction;
|
||||
|
||||
class TemplateWrapperTest extends TestCase
|
||||
{
|
||||
use ExpectDeprecationTrait;
|
||||
|
||||
public function testUnwrapChecksTheEnvironment(): void
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader(['index' => 'content']));
|
||||
@@ -43,6 +47,20 @@ class TemplateWrapperTest extends TestCase
|
||||
$wrapper->unwrap(new Environment(new ArrayLoader()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
#[Group('legacy')]
|
||||
public function testUnwrapCanBeCalledWithoutArguments(): void
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader(['index' => 'content']));
|
||||
$wrapper = $twig->load('index');
|
||||
|
||||
$this->expectDeprecation('Since twig/twig 3.29: Calling "Twig\TemplateWrapper::unwrap()" without arguments is deprecated, pass the Twig environment instead.');
|
||||
|
||||
$this->assertInstanceOf(Template::class, $wrapper->unwrap());
|
||||
}
|
||||
|
||||
public function testGetDefaultEscapeStrategy(): void
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader(['index.js.twig' => 'content']), ['autoescape' => 'name']);
|
||||
|
||||
Reference in New Issue
Block a user