twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.18k stars 1.25k forks source link

Fix testExtensionsAreNotInitializedWhenRenderingACompiledTemplate #4263

Closed derrabus closed 2 months ago

derrabus commented 2 months ago

I stumbled over this test while checking for PHPUnit deprecations. In this case, we need to do something about the setMethods() call which has been removed in PHPUnit 11.

However, we add an expectation for a method initExtensions() which has been removed a long time ago. So, if that method was actually called, we'd get an error anyway. Expecting this method not to be called feels a little redundant. This basically means, we can run this test against an unmocked Environment. But if we do so, the test does not really test what it's supposed to test which is why I decided to remove the whole test.

Update: Instead, I'm now registering an extension that throws if we try to initialize it.

stof commented 2 months ago

you could check that getFunctions or getFilters is not called on a registered extension as a way to detect that the initialization is not performed (which also does not require mocking the Environment AFAIK).

derrabus commented 2 months ago

I tried your suggestion:

$twig = new Environment($loader, $options);
$extension = $this->createMock(ExtensionInterface::class);
$extension->expects($this->never())->method($this->anything());
$twig->addExtension($extension);

With this, the test fails. My assumption is that the test guesses the cache key wrong.

derrabus commented 2 months ago

Adding the extension changes the cache key. 🤦🏻‍♂️ I'll try to work around that.

derrabus commented 2 months ago

I think, I've fixed the test. Thank you for your help, @stof.

fabpot commented 2 months ago

Thank you @derrabus.