Open captbaritone opened 6 years ago
You can also use RefreshDatabase Trait
I would advice not using this technique. Transactions should already be used to prevent data corruption during normal application usage.
Using transactions during testing only works if the database supports nested transactions, and MySQL doesn't.
So the official documentation is wrong?
The MysSQL 5.7 documentation describes Statements That Cause an Implicit Commit. Under Transaction-control and locking statements it mentions START TRANSACTION
, saying:
Transactions cannot be nested. This is a consequence of the implicit commit performed for any current transaction when you issue a START TRANSACTION statement or one of its synonyms.
The new version 8 handles this the same way.
Kwebble, I mean laravel's official documentation. The official documentation recommends using this trait.
It seems to me that this trait is smart enough to detect your database driver and decide wether or not it should use a database transaction.
OK, I understand.
I haven't seen the Laravel implementation of this feature. Perhaps it is possible by using a separate database connection for the test changes and configuring the normal connection to allow dirty reads of uncommitted changes.
But then the testing environment differs from production.
Also, I know the Laravel documentation is not complete or always clear. In fact I've written about that: http://kwebble.com/blog/2017-05-17_14:26:52
If that's the case (laravel's official documentation not being accurate) have you submitted a pull request? (Not a rhetorical question)
No, I did not. There's no time for me at work to do that. And privately I don't use Laravel.
If I would contribute documentation it would be PHPDoc to describe all method arguments and return values, not the website. Having fully documented code looks more important to me.
Once I found out the site does not describe all features I only use it as starting point, not as ultimate reference.
I think Laravel's Transaction support nesting https://github.com/laravel/framework/issues/1686
@kwebble I found out that it doesn't matter if your database (mysql in this case) doesn't support Nested Transactions. PDO will make use of SAVEPOINTS to emulate native nested transactions.
Here are some links if you want to read further
https://secure.php.net/manual/en/pdo.begintransaction.php#116669
https://stackoverflow.com/a/11941449/1132836
That's why Laravel's documentation recommends using RefreshDatabase Trait.
Comments left on this issue will be shown live on the post: https://jordaneldredge.com/blog/speed-up-laravel-tests-with-database-transactions/