From 9458c8a33d5dc03fd1c5fb9c90b53efff52a55b5 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 4 May 2024 19:46:29 +0200 Subject: [PATCH] Make Environment::getGlobals() private --- src/Environment.php | 4 +--- tests/EnvironmentTest.php | 39 ++++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index ea1d10ec5..a47226942 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -812,11 +812,9 @@ public function addGlobal(string $name, $value) } /** - * @internal - * * @return array */ - public function getGlobals(): array + private function getGlobals(): array { if ($this->extensionSet->isInitialized()) { if (null === $this->resolvedGlobals) { diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index 4eb22036e..67c16d1c6 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -65,45 +65,47 @@ public function testGlobals() $loader = $this->createMock(LoaderInterface::class); $loader->expects($this->any())->method('getSourceContext')->willReturn(new Source('', '')); + $getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals'); + // globals can be added after calling getGlobals $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); // globals can be modified after a template has been loaded $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->load('index'); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); // globals can be modified after extensions init $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); // globals can be modified after extensions and a template has been loaded $arrayLoader = new ArrayLoader(['index' => '{{foo}}']); $twig = new Environment($arrayLoader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); $twig->load('index'); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); $twig = new Environment($arrayLoader); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->addGlobal('foo', 'bar'); $template = $twig->load('index'); $this->assertEquals('bar', $template->render([])); @@ -111,38 +113,38 @@ public function testGlobals() // globals cannot be added after a template has been loaded $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->load('index'); try { $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } // globals cannot be added after extensions init $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); try { $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } // globals cannot be added after extensions and a template has been loaded $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); $twig->load('index'); try { $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } // test adding globals after a template has been loaded without call to getGlobals @@ -152,7 +154,7 @@ public function testGlobals() $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } } @@ -280,13 +282,16 @@ public function testAddExtension() $twig = new Environment($this->createMock(LoaderInterface::class)); $twig->addExtension(new EnvironmentTest_Extension()); + $getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals'); + $getGlobals->setAccessible(true); + $this->assertArrayHasKey('test', $twig->getTokenParsers()); $this->assertArrayHasKey('foo_filter', $twig->getFilters()); $this->assertArrayHasKey('foo_function', $twig->getFunctions()); $this->assertArrayHasKey('foo_test', $twig->getTests()); $this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators()); $this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators()); - $this->assertArrayHasKey('foo_global', $twig->getGlobals()); + $this->assertArrayHasKey('foo_global', $getGlobals->invoke($twig)); $visitors = $twig->getNodeVisitors(); $found = false; foreach ($visitors as $visitor) {