-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
RefreshDatabase not refreshing DB #53758
Comments
<server name="DB_FIRST_DRIVER" value="sqlite"/>
<server name="DB_FIRST_DATABASE" value="file:first?mode=memory" />
<server name="DB_SECOND_DRIVER" value="sqlite"/>
<server name="DB_SECOND_DATABASE" value="file:second?mode=memory"/> Something like above. |
Possibly, if the connections are being disconnected at some point. The "table does not exist" errors seem to indicate that. FWIW the DSN string should possibly also include It's an old feature for improved read performance (but potentially inconsistent writes?) that isn't recommended anymore but every mention of |
@crynobone First of all that did NOT resolve the issue. I ran my unit tests with this flag and without. With: ..E....E.FEEEE.E.. errors unrelated to this Not to mention I'm getting this error after composer update: Could not scan for classes inside "/srv/vendor/laravel/framework/src/Illuminate/Queue/IlluminateQueueClosure.php" which does not appear to be a file nor a folder In ClassMapGenerator.php line 131: Could not scan for classes inside "/srv/vendor/laravel/framework/src/Illuminate/Queue/IlluminateQueueClosure.php" which does not appear to be a file nor a folder |
Hey there, thanks for reporting this issue. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.
Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
@crynobone In the "Steps to reproduce" I've shared full content of all the files changed to reproduce the issue. Can you first try to reproduce it with the information I provided? I did use the latest laravel installer. I would've submitted a PR straight away here but I don't have write access. |
I don't have a way to run a fresh Laravel installation and replicate this. When it exists and has nothing to do with the above "Step to reproduce" |
The PR is already part of latest Laravel, no need to change your composer dependencies. Just try what crynobone suggested. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Your phpunit.xml file uses Fix: Replace the <?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_FIRST_DRIVER" value="sqlite"/>
<env name="DB_FIRST_DATABASE" value=":memory:"/>
<env name="DB_SECOND_DRIVER" value="sqlite"/>
<env name="DB_SECOND_DATABASE" value=":memory:"/>
</php>
</phpunit> This ensures that Laravel correctly picks up the database configuration during testing. |
I think the issue with using the new config options from @stancl that @crynobone suggested is that they're incompatible with protected function usingInMemoryDatabase()
{
$default = config('database.default');
return config("database.connections.$default.database") === ':memory:';
} |
That's a method on a class you control as my PR mentions. Try overriding it to return: protected function usingInMemoryDatabase()
{
$default = config('database.default');
$database = config("database.connections.$default.database");
return $database === ':memory:' || str_contains($database, 'mode=memory');
} The PR adding the feature in is slim and focused on core Laravel logic rather than helper traits that can be easily modified by the user. If such overrides work well and people use them often the change can be submitted in another PR. |
Laravel Version
11.31
PHP Version
8.3
Database Driver & Version
:memory:
Description
After upgrading to latest Laravel 11, my feature tests started to fail due to RefreshDatabase trait not refreshing the DB.
Adding
RefreshDatabaseState::$migrated = false;
to this function fixed the issue. I'm not sure what changed in the framework from last version but running each test individually passes but together they fail on "table does not exist" error.Having this in the base tests/TestCase.php class fixes it as a workaround.
Steps To Reproduce
I think the problem arrizes when using multiple DB connections for different models. These models override default connection with
protected $connection = 'mysql1'; // this is default
protected $connection = 'mysql2'; // this is used in User model
And so in the tests, we have to use these hardcoded connections, which is why for testing we override just what matters
phpunit.xml
full example
.env
User model
config/database.php
migration
phpunit.xml
User factory
The text was updated successfully, but these errors were encountered: