-
Notifications
You must be signed in to change notification settings - Fork 487
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
Create a dedicated testing database #388
Conversation
Brilliant! Just what I needed! Thanks @jessarcher |
I think it would be clearer. Postgres and MySQL appear to support up to 63 and 64 character names, respectively, so there is a chance (albeit slim) for an overflow. It also complicates the PHPUnit configuration, but it should be easy enough. |
Would there be a need for people to customize the setup scripts? |
@driesvints Potentially. It's a little tricky to publish because we'd need to update the path in the users However, because they do own the |
Looks good to me! We could explore that DB_DATABASE_testing approach if we get some more community feedback. |
If you have a separate database for testing make sense to have a mysql in-memory option too |
Just tried it and This line should be updated to replace What do you think @taylorotwell ? |
@kichetof Laravel uses See the related PR here: laravel/laravel#5765 |
@driesvints Thanks for your quick feedback! My project has been created on Laravel 8 and was updated to Laravel 9, maybe that's the issue with my phpunit |
I'd just change it to |
The problem
In a default sail installation, running tests with the
RefreshDatabase
trait will empty the user's local database and log them out. This is because the trait callsmigrate:fresh
.To reproduce:
laravel new --git sail-test cd sail-test php artisan sail:install ./vendor/bin/sail up -d ./vendor/bin/sail artisan migrate
RefreshDatabase
trait)./vendor/bin/sail test
Using SQLite for tests can be a good solution, but the lack of feature parity with MySQL et al. can be a confusing source of bugs.
The solution
This PR configures the database containers to create an additional
testing
database. The PHPUnit configuration is updated accordingly.I have tested this with MySQL, MariaDB, and PostgreSQL.
Note that MySQL and Maria DB need an init script to set the appropriate permissions for the user. This is not required for PostgreSQL because the
POSTGRES_USER
has superuser privileges.Notes
phpunit.xml
. I can make this a lot smarter at the cost of code (and regex) complexity.testing
. This will conflict if the usersDB_USERNAME
is already set to that, which would occur when runninglaravel new testing
. It doesn't cause any failures, however, they will only get a single database which won't solve the above issue. We could use a more obscure name (e.g.sail_testing
) or attempt to make it dynamic (e.g.${DB_DATABASE}_testing
)