In this lesson, we will look into migrating our PHPUnit tests to Pest which is growing in popularity. It can be done in two ways:
Manual migrations can be huge but Pest is designed to make it as easy as possible. It can run a combination of tests - PHPUnit and Pest automatically. This means that you can migrate your tests one by one, or all at once.
Just follow the installation instructions and start writing new tests in Pest!
Semi-automatic migrations can be done using Laravel Shift. It doesn't cost that much (at the time of writing, $9 to convert one repository) and provides a Pull Request with the changes:
It also adds quite a few comments to your tests to help you understand what's going on:
We've tried to use Shift for this course and let's look at what it gave us.
Notice: I want you to understand correctly: you don't have to use Shift as we did, it's totally fine if you decide to perform the following changes yourself manually, for free.
The first thing Shift did was prepared our codebase for Pest:
pestphp/pest
package to our composer.json
filetests/Pest.php
file with no contentThe second thing Shift did was migrated our tests:
This means that we didn't have to change our files manually. Shift did it for us, and it did quite a good job converting PHPUnit classes to Pest files.
You can view all the changes in the Shift Commit.
The last thing Shift did was to convert our PHPUnit assertions to Pest expectations:
This means that our code was adapted to use Pest's expectations and follow Pest's syntax.
And as the last step, Shift has cleaned up the codebase:
Removed leftover use
statements
Implemented Pest configuration
After the code was fully in place, there was a comment that warned us about a helper we need to migrate:
We're talking about the create_apartment()
method here. The solution is to remove it from tests/Feature/BookingsTest.php and add it to the main Pest file:
tests/Pest.php
/** @link https://pestphp.com/docs/custom-helpers */ function create_apartment(): Apartment{ $owner = User::factory()->owner()->create(); $cityId = City::value('id'); $property = Property::factory()->create([ 'owner_id' => $owner->id, 'city_id' => $cityId, ]); return Apartment::create([ 'name' => 'Apartment', 'property_id' => $property->id, 'capacity_adults' => 3, 'capacity_children' => 2, ]);}
That's it - We are done with the migration to Pest, on a simple minimal level. But Pest has many more features we can use.
So, in the next lesson, we will look into optimizing our tests and using more Pest features!
Pull Request made by Shift: https://github.com/LaravelDaily/Booking-Com-Simulation-Laravel/pull/8