I
I
igortelmenko2017-07-02 17:00:07
Software testing
igortelmenko, 2017-07-02 17:00:07

How to work with the database in Laravel tests?

Welcome all!
I'm doing functional tests for a Laravel application. Several questions emerged.
I state. :)
1. Differences between make and create factory methods

factory(App\User::class)->make();
factory(App\User::class)->create();

If I understand correctly, then create() writes to the database table, and make() only creates a model object. Correctly?
But if so, can you give a real-life example of what make() might be needed for?
2. Do I understand correctly that when you run even one test, for example, phpunit --filter testNameSingleTest, the framework rolls back all migrations, and then (immediately before running the test) performs them again. I somehow had an error in the down() method of one migration and when I ran the test I got an error. Which led me to these thoughts. Isn't it wasteful to re-run all migrations before one test? And maybe it can be turned off somehow? Let only the records created by the test itself be cleared (deleted).
3. Difference in two database rollback approaches DatabaseMigrations and DatabaseTransactions. On the one hand, it is clear that the first approach uses migrations, and the second wraps each test in a transaction.
But the problem is in the details.
a) Which method is used by default? By default, it turns out that all records remain in the database after the test. Correctly?
b) I tried to explicitly specify DatabaseMigrations and got a database after the test without records created by the test. In the form that can be obtained immediately after the migrations.
c) I tried to explicitly specify DatabaseTransactions and specify the list of connections in the special parameter (there are 2 of them so far), but after the test, the entries in the database for some reason remained. Is it correct? Or I did something wrong. What could be the reason?
d) It is written that cleaning occurs after each test. Is the entire test class considered a test? Or one method? A question of terminology.
Can you tell me something...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Semenko, 2017-07-02
@abler98

If I understand correctly, then create() writes to the database table, and make() only creates a model object. Correctly?

Yes exactly.
Perhaps someday it will be necessary to create a model without the need to write to the database, can't this be?
Migrations/transactions are started inside the setUp method of the base class (TestCase), I think this should answer your question.
By default, a regular write to the database occurs.
This is how it should be, when using DatabaseMigrations in tests, migrations are rolled before the start of the test (the migrate command is executed), and after completion - migrate:rollback. I usually use migrations + separate db for tests. I advise you to look in the phpunit.xml file, where you can override ENV values.
The cleanup happens in the tearDown method, which means it happens for every test method. The same goes for running migrations/transactions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question