P粉9523651432023-08-27 15:05:56
Looks like you can't use a schema dump file to operate an in-memory database while testing
https://laravel.com/docs/9.x/migrations#squashing-migrations
You can try this
DB::unprepared(file_get_contents("path/file.sql"));
Only try it as a last resort. Personally, it is recommended to migrate in the test environment. If you adopt this method, you should also add a check for migration in the test environment
P粉8212742602023-08-27 12:58:12
I finally figured it out.
The problem lies in the incorrect settings of the test environment. I didn't find the exact cause, but I found how to set up the test environment so that the dump file is found and loaded.
This describes the steps I took to find the solution.
database.php
I copied the test database instead of the normal databasedatabase.php
I have the main database connection: 'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => false, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ],
and test connection
'testing' => [ 'driver' => 'mysql', 'host' => env('DB_TEST_HOST', '127.0.0.1'), 'port' => env('DB_TEST_PORT', '3306'), 'database' => env('DB_TEST_DATABASE', 'forge'), 'username' => env('DB_TEST_USERNAME', 'forge'), 'password' => env('DB_TEST_PASSWORD', ''), ],
testing
connection data into a new mysql
connection just to see if I got the same results on the command line'mysql' => [ 'url' => env('DATABASE_URL'), 'driver' => 'mysql', 'host' => env('DB_TEST_HOST', '127.0.0.1'), 'port' => env('DB_TEST_PORT', '3306'), 'database' => env('DB_TEST_DATABASE', 'forge'), 'username' => env('DB_TEST_USERNAME', 'forge'), 'password' => env('DB_TEST_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => false, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ], /*'testing' => [ 'driver' => 'mysql', 'host' => env('DB_TEST_HOST', '127.0.0.1'), 'port' => env('DB_TEST_PORT', '3306'), 'database' => env('DB_TEST_DATABASE', 'forge'), 'username' => env('DB_TEST_USERNAME', 'forge'), 'password' => env('DB_TEST_PASSWORD', ''), ],*/
php artisan:migrate
phpunit.xml
, I will explain it nowphpunit.xml
phpunit.xml
looks like this (the complete file is not shown here):
<server name="QUEUE_CONNECTION" value="sync"/> <server name="SESSION_DRIVER" value="array"/> <server name="TELESCOPE_ENABLED" value="false"/> <env name="DB_CONNECTION" value="testing"/> </php> </phpunit>
phpunit.xml
became <server name="QUEUE_CONNECTION" value="sync"/> <server name="SESSION_DRIVER" value="array"/> <server name="TELESCOPE_ENABLED" value="false"/> <env name="DB_DATABASE" value="cinema_test"/> </php> </phpunit>
database.php
and removed the related obsolete variables from the .env
fileWhile I didn't find the actual cause of Laravel failing to load the dump file, I found a workaround by changing the database name just for testing instead of defining a completely new SQL connection for testing purposes. This fixed the issue and now the database dump file is loaded during testing.