Final speed up for your Laravel test suite

February 25, 2023
Zacharias Creutznacher
Zacharias Creutznacher
Chief Technology Officer (CTO)

In my current project I have about 200 migrations. Cause of incompatibility I can't use <code>sqllite<code>. Also I think it's always better to test against the database you're actually running on dev or production! So I went with <code>mysql<code>. I setted up the test suite with PHP Pest and started testing. After applying the <code>RefreshDatabase<code> trait to my tests I was shocked how slow it became. Now everytime I do a test, it migrates all the 200 tables from new. But since all Laravel tests are transaction-based and are getting rolledback after the test is finished, there is really no need to migrate the whole table structure from scratch every time you do a test.

RefreshDatabaseFast to the rescue

Therefore we now add a new <code>RefreshDatabaseFast<code> trait to our laravel-useful-additions package. The base idea comes from Mayahi and we now wrapped it slightly changed and improved into our package.

This trait makes the migration of your database in your test suite much, much faster! It basically only migrates your database if the migration files has changed. So the first <code>migrate:fresh<code> takes a while (depending on how many migrations you have), and then it's incredibly fast.

Optionally you can set <code>USEFUL_TRAITS_SEED_AFTER_FAST_DB_REFRESH<code> to <code>true<code> if you like to seed your database after the migration.

Also make sure to add the <code>.phpunit.database.checksum<code> to your <code>.gitignore<code> file!

Comparison

If we now compare the first test run with the next one it's much faster! Cause we do not need to remigrate the whole (already existing) database!

code

What's a Rich Text element?

The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.

Static and dynamic content editing

A rich text element can be used with static or dynamic content. For static content, just drop it into any page and begin editing. For dynamic content, add a rich text field to any collection and then connect a rich text element to that field in the settings panel. Voila!

How to customize formatting for each rich text

Headings, paragraphs, blockquotes, figures, images, and figure captions can all be styled after a class is added to the rich text element using the "When inside of" nested selector system.

H4

H5
H6

http://google.de

<code>test123<code>

<span style="color:red;">asdfasdf</span>

composer require spatie/laravel-artisan-dispatchable

Put your ideas into practice!
Today is the day to build the business of your dreams. Share your mission with the world - and inspire your customers.
Start now