Repository navigation
Cached config causes tests to overwrite live data #55367
Laravel Version12.3.0 PHP Version8.4.5 Database Driver & Versionsqlite DescriptionWhen config is cached, PHPUnit tests don't honor the :memory: setting for Sqlite. Tests fail because they appear to be partially using live data and partially :memory: data. phpunit.xml <env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>Tests use: use RefreshDatabase;Steps To ReproduceRun: php artisan config:clear
php artisan testAll tests successful. database.sqlite timestamp unchanged. Then run: php artisan config:cache
php artisan testTests fail. database.sqlite timestamp modified. Opening up the file for examination reveals all previous data gone. The tests fail because (I think) they're creating a user (in the case of user test, for instance) in one database but checking the count in the other. Not sure, but that's my guess. |
Replies: 4 comments
|
This can be prevented with something like laravel/laravel#6598 |
|
Sure, that looks good. But doesn't this mean that something in the config isn't getting cached, something that's present when the config isn't cached? Or it supposed to be that way? If not, I was thinking more along the lines of the underlying issue rather than a workaround. I'm new to Laravel but didn't see anything about ensuring that config isn't cached before running tests. Apologies if I missed it. |
|
I think I get it now. phpunit.xml sets environment values which are then referenced inside config files. But when the config is cached, the one-file-config in bootstrap/cache/config.php doesn't reference the environment any longer. The values have been extracted to their literal values and are no longer affected by what phpunit.xml sets. I think that's it, anyway. So, yes. Ensuring at the platform level with your PR that cache is cleared before tests will be helpful. I'm still in the dev stage so wiping my data by mistake isn't a heart breaker. Meanwhile, I made a small shell script to clear config cache/ run tests. |
I think I get it now. phpunit.xml sets environment values which are then referenced inside config files. But when the config is cached, the one-file-config in bootstrap/cache/config.php doesn't reference the environment any longer. The values have been extracted to their literal values and are no longer affected by what phpunit.xml sets. I think that's it, anyway.
So, yes. Ensuring at the platform level with your PR that cache is cleared before tests will be helpful. I'm still in the dev stage so wiping my data by mistake isn't a heart breaker. Meanwhile, I made a small shell script to clear config cache/ run tests.