Courses

Laravel API Code Review and Refactor

Quick Fix for Test Factories

You're reading a FREE PREVIEW of a PREMIUM course.

Link to the repository

[Only for premium members]

This lesson will be a short and simple cleanup.

I've noticed the unnecessary two-line combination in SIX test methods:

tests/Feature/OrderCreateTest.php

// Create an order with the product
$order = Order::factory()->create();
$order->update(['user_id' => $user->id]);

So I've just changed those two-liners to one-liners:

tests/Feature/OrderCreateTest.php

// Refactored it in SIX places
$order = Order::factory()->create();
$order->update(['user_id' => $user->id]);
$order = Order::factory()->create(['user_id' => $user->id]);

This is another example of a feature implemented correctly but not using the Laravel feature: you can override any Factory field, passing it as an array.

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

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord