Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Environment::getGlobals() private #4075

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
39 changes: 22 additions & 17 deletions tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,84 +65,86 @@ 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([]));

// 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 +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));
}
}

Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed too.


$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