Skip to content
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

Comments: Speed up Laravel tests with database transactions #9

Open
captbaritone opened this issue Nov 11, 2017 · 10 comments
Open

Comments: Speed up Laravel tests with database transactions #9

captbaritone opened this issue Nov 11, 2017 · 10 comments

Comments

@captbaritone
Copy link
Owner

Comments left on this issue will be shown live on the post: https://jordaneldredge.com/blog/speed-up-laravel-tests-with-database-transactions/

@captbaritone captbaritone changed the title Comments Nov 11, 2017
@gmansilla
Copy link

You can also use RefreshDatabase Trait

@kwebble
Copy link

kwebble commented May 24, 2018

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.

@gmansilla
Copy link

So the official documentation is wrong?

@kwebble
Copy link

kwebble commented May 27, 2018

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.

@gmansilla
Copy link

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.

@kwebble
Copy link

kwebble commented May 27, 2018

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

@gmansilla
Copy link

If that's the case (laravel's official documentation not being accurate) have you submitted a pull request? (Not a rhetorical question)

@kwebble
Copy link

kwebble commented May 28, 2018

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.

@foggyfrost
Copy link

I think Laravel's Transaction support nesting
laravel/framework#1686

@gmansilla
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants