Description
Just recreated this on Symfony 3.1, also occurs on 2.7.
When a WebTestCase is run, any subsequent iterations of running that WebTestCase will have the same parameters set, even if the parameters.php
config file is using different values from previous test cases.
I have narrowed the problem as I see it, down to the instance of appTestDebugProjectContainer.php
which has an old set of parameters, but I can't see where it's retrieved them from. I suspect a rogue static var being used somewhere, perhaps in a Reflection class or similar.
Reproduction:
Create a new Symfony 3.1 project, and modify the following files:
app/config/config.yml
imports:
- { resource: parameters.yml }
- { resource: parameters.php } <-- add this line
app/config/parameters.yml
append to the end of the file
test_param: hello
app/config/parameters.php
<?php
echo "SETTING PARAM " . getenv('TEST_PARAM') ."\n";
$container->setParameter('test_param', getenv('TEST_PARAM'));
tests/AppBundle/EnvResetTest.php
<?php
namespace AppBundle;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class EnvResetTest extends WebTestCase
{
public function setUp()
{
`rm -rf var/cache/*`;
`rm -rf var/sessions/*`;
`php bin/console cache:warmup --env=test`;
}
public function testConfigSettingPart1()
{
echo "TEST ONE..\n";
putenv('TEST_PARAM=testone');
$param = static::createClient()->getKernel()->getContainer()->getParameter('test_param');
echo $this->getActualOutput();
$this->assertEquals('testone', $param); //passes
}
public function testConfigSettingPart2()
{
echo "TEST TWO..\n";
//no env, should use default value of "hello"
$param = static::createClient()->getKernel()->getContainer()->getParameter('test_param');
echo $this->getActualOutput();
$this->assertEquals('hello', $param); //fails
}
public function testConfigSettingPart3()
{
echo "TEST THREE..\n";
putenv('TEST_PARAM=testthree');
$param = static::createClient()->getKernel()->getContainer()->getParameter('test_param');
echo $this->getActualOutput();
$this->assertEquals('testthree', $param); //fails
}
}
The outcome varies depending on whether you enable the line php bin/console cache:warmup --env=test
in the test setUp
method, but only one of the tests will ever pass, regardless of this.
Test1 passes if the line is disabled, Test2 passes if the line is enabled. The other tests will fail.
This proves the parameter values are carried from one execution to the next, despite proving that the parameters.php file being executed for each test.
It feels like this shouldn't happen, I haven't seen any documentation to suggest this behaviour should be expected... If there is a workaround I can use in the meantime, like for resetting some statically cached parameters somewhere, that would be very much appreciated.
This is the last hurdle to overcome in order for my webTestCase scripts to enable testing of my application in different configurations, from a single test suite.