laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.54k stars 11.02k forks source link

Automatically database creation on testing with new Laravel app not working #44806

Closed schonhoff closed 2 years ago

schonhoff commented 2 years ago

Description:

Hello everyone,

I tried to change my testing database to sqlite instead of sqlite in-memory because we have a lot of migrations and want to use the dumped version of it instead to execute all migrations. After changing my .env.testing file to use the sqlite database driver from the original config, I tried to run my tests. The following error occurred:

[...]
Warning: TTY mode is not supported on Windows platform.
ParaTest v6.6.5 upon PHPUnit 9.5.26 by Sebastian Bergmann and contributors.

In Connection.php line 760:

  Database (laravel_untouched) does not exist. (SQL: PRAGMA foreign_keys = ON;)

In SQLiteConnector.php line 34:

  Database (laravel_untouched) does not exist.

I did not create any sqlite files because in the documentation for testing in parallel it mentioned (https://laravel.com/docs/9.x/testing#parallel-testing-and-databases):

Laravel automatically handles creating and migrating a test database for each parallel process that is running your tests.
he test databases will be suffixed with a process token which is unique per process.
For example, if you have two parallel test processes,
Laravel will create and use your_db_test_1 and your_db_test_2 test databases.

I thought the database will be created and the dump and/or migrations will be run.

Possible solutions

@timacdonald added a prompt for installing the sqlite databse if it is doesn't exists in the PR https://github.com/laravel/framework/pull/43867 Maybe something like this would be helpful? Or should the test databases be created in the storage/framework/testing directory without any prompt? I don't know the expected behavior because I can't execute it myself.

Not using parallel testing will throw this error:

Tests\Feature\Auth\RegistrationTest > new users can register
Illuminate\Database\QueryException 

Database (laravel_untouched) does not exist. (SQL: PRAGMA foreign_keys = ON;)

Steps To Reproduce:

Here is a Laravel app test repo where only the Installation step of the bootcamp is done (https://bootcamp.laravel.com/blade/installation) and 2 other small changes: https://github.com/schonhoff/chirper

Other things to know:

If there are any questions feel free to ask.

driesvints commented 2 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

schonhoff commented 2 years ago

Hello @driesvints ,

the tests did not create a test database automatically like it is mentioned in the docs. I would suggest this is a bug and not a discussion nor a question. I suggested possible solutions to the problem and updated my original post and the title to make it clear. I hope this show my bug report more clearly. Also I found other tickets like this https://github.com/laravel/framework/issues/40488 but as I mentioned I tested it in a newly created Laravel app and had the error.

schonhoff commented 2 years ago

Improtant: The error occurs only if the database engines between testing and local are different. .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

phpunit.xml:

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <!-- <env name="DB_DATABASE" value=":memory:"/> -->
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>

No .env.testing needed for this.

driesvints commented 2 years ago

So there isn't really a bug here but rather a config issue. You need to make sure your env files are properly pointing towards your test environment and as you say above by default in the skeleton it's not configured for sqlite. If you want to use a file base sqlite testing environment you need to define the proper database in your phpunit.xml.

And you also need to create that main database up front as it's needed for migrations. Only parallel databases during testing are created. I've sent in a PR to the docs to clarify this: https://github.com/laravel/docs/pull/8323

schonhoff commented 2 years ago

@driesvints Thanks for the clarification. My thought was that parallel testing will create its one databases and will migrate them. But that they need a "core" database wasn't clear for me. Hopefully the clarification in the docs helps out other people too! Thanks and have a good night/day :)