Courses

Re-creating Booking.com API with Laravel and PHPUnit

Tests "Cleanup" with Factories

While reviewing our tests, I decided we can improve things a bit, by using factories more to have more reusable and shorter code in tests. Let's do exactly that, in this short lesson.

Example:

// Current
$owner = User::factory()->create(['role_id' => Role::ROLE_OWNER]);
 
// Replacing with Factory States
$owner = User::factory()->owner()->create();

The full lesson is only for Premium Members.
Want to access all 23 lessons of this course? (224 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord