Skip to content

Commit

Permalink
Make Environment::getGlobals() private
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed May 4, 2024
1 parent 00c43a9 commit ecc0936
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,9 @@ public function addGlobal(string $name, $value)
}

/**
* @internal
*
* @return array<string, mixed>
*/
public function getGlobals(): array
private function getGlobals(): array
{
if ($this->extensionSet->isInitialized()) {
if (null === $this->resolvedGlobals) {
Expand Down
40 changes: 23 additions & 17 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,84 +65,87 @@ public function testGlobals()
$loader = $this->createMock(LoaderInterface::class);
$loader->expects($this->any())->method('getSourceContext')->willReturn(new Source('', ''));

$getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals');
$getGlobals->setAccessible(true);

// 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([]));

// 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
Expand All @@ -152,7 +155,7 @@ public function testGlobals()
$twig->addGlobal('bar', 'bar');
$this->fail();
} catch (\LogicException $e) {
$this->assertArrayNotHasKey('bar', $twig->getGlobals());
$this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig));
}
}

Expand Down Expand Up @@ -280,13 +283,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) {
Expand Down

0 comments on commit ecc0936

Please sign in to comment.