Loading...
Loading...
Use when implementing any feature or bugfix, before writing implementation code
npx skill4agent add markhamsquareventures/essentials test-driven-developmentNO PRODUCTION CODE WITHOUT A FAILING TEST FIRSTtest('retries failed operations 3 times', function () {
$attempts = 0;
$operation = function () use (&$attempts) {
$attempts++;
if ($attempts < 3) {
throw new Exception('fail');
}
return 'success';
};
$result = retryOperation($operation);
expect($result)->toBe('success');
expect($attempts)->toBe(3);
});test('retry works', function () {
$mock = Mockery::mock(SomeService::class);
$mock->shouldReceive('call')
->times(3)
->andThrow(new Exception(), new Exception())
->andReturn('success');
retryOperation(fn () => $mock->call());
// Only verifies mock was called, not actual behavior
});php artisan test --filter="retries failed operations"function retryOperation(callable $fn, int $maxRetries = 3): mixed
{
for ($i = 0; $i < $maxRetries; $i++) {
try {
return $fn();
} catch (Exception $e) {
if ($i === $maxRetries - 1) {
throw $e;
}
}
}
}function retryOperation(
callable $fn,
int $maxRetries = 3,
string $backoff = 'exponential',
?callable $onRetry = null,
?LoggerInterface $logger = null,
): mixed {
// YAGNI - You Aren't Gonna Need It
}php artisan test --filter="retries failed operations"| Quality | Good | Bad |
|---|---|---|
| Minimal | One thing. "and" in name? Split it. | |
| Clear | Name describes behavior | |
| Shows intent | Demonstrates desired API | Obscures what code should do |
| Excuse | Reality |
|---|---|
| "Too simple to test" | Simple code breaks. Test takes 30 seconds. |
| "I'll test after" | Tests passing immediately prove nothing. |
| "Tests after achieve same goals" | Tests-after = "what does this do?" Tests-first = "what should this do?" |
| "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run. |
| "Deleting X hours is wasteful" | Sunk cost fallacy. Keeping unverified code is technical debt. |
| "Keep as reference, write tests first" | You'll adapt it. That's testing after. Delete means delete. |
| "Need to explore first" | Fine. Throw away exploration, start with TDD. |
| "Test hard = design unclear" | Listen to test. Hard to test = hard to use. |
| "TDD will slow me down" | TDD faster than debugging. Pragmatic = test-first. |
| "Manual test faster" | Manual doesn't prove edge cases. You'll re-test every change. |
| "Existing code has no tests" | You're improving it. Add tests for existing code. |
test('rejects empty email', function () {
$response = $this->postJson('/api/users', [
'email' => '',
'name' => 'Test User',
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['email']);
});$ php artisan test --filter="rejects empty email"
FAIL: Expected status 422, got 200// app/Http/Requests/StoreUserRequest.php
class StoreUserRequest extends FormRequest
{
public function rules(): array
{
return [
'email' => ['required', 'email'],
'name' => ['required', 'string'],
];
}
}$ php artisan test --filter="rejects empty email"
PASStest('calculate order total includes tax', function () {
$order = Order::factory()->create([
'subtotal' => 10000, // $100.00 in cents
]);
$action = new CalculateOrderTotalAction();
$result = $action->execute($order, taxRate: 0.08);
expect($result->total)->toBe(10800);
expect($result->tax)->toBe(800);
});$ php artisan test --filter="calculate order total"
FAIL: Class CalculateOrderTotalAction not found// app/Actions/CalculateOrderTotalAction.php
class CalculateOrderTotalAction
{
public function execute(Order $order, float $taxRate): OrderTotal
{
$tax = (int) round($order->subtotal * $taxRate);
return new OrderTotal(
subtotal: $order->subtotal,
tax: $tax,
total: $order->subtotal + $tax,
);
}
}$ php artisan test --filter="calculate order total"
PASStest('users can only view their own orders', function () {
$user = User::factory()->create();
$otherUser = User::factory()->create();
$order = Order::factory()->for($otherUser)->create();
$this->actingAs($user);
expect($user->can('view', $order))->toBeFalse();
});
test('users can view their own orders', function () {
$user = User::factory()->create();
$order = Order::factory()->for($user)->create();
$this->actingAs($user);
expect($user->can('view', $order))->toBeTrue();
});// app/Policies/OrderPolicy.php
class OrderPolicy
{
public function view(User $user, Order $order): bool
{
return $user->id === $order->user_id;
}
}test('active scope returns only active users', function () {
User::factory()->count(3)->create(['active' => true]);
User::factory()->count(2)->create(['active' => false]);
$activeUsers = User::active()->get();
expect($activeUsers)->toHaveCount(3);
expect($activeUsers->pluck('active')->unique()->all())->toBe([true]);
});// app/Models/User.php
public function scopeActive(Builder $query): Builder
{
return $query->where('active', true);
}use Illuminate\Support\Facades\Queue;
test('order completion dispatches notification job', function () {
Queue::fake();
$order = Order::factory()->create();
$action = new CompleteOrderAction();
$action->execute($order);
Queue::assertPushed(SendOrderCompletionNotification::class, function ($job) use ($order) {
return $job->order->id === $order->id;
});
});class CompleteOrderAction
{
public function execute(Order $order): void
{
$order->update(['status' => 'completed']);
SendOrderCompletionNotification::dispatch($order);
}
}use Illuminate\Support\Facades\Event;
test('user registration fires UserRegistered event', function () {
Event::fake([UserRegistered::class]);
$action = new RegisterUserAction();
$user = $action->execute([
'email' => 'test@example.com',
'password' => 'password',
]);
Event::assertDispatched(UserRegistered::class, function ($event) use ($user) {
return $event->user->id === $user->id;
});
});class RegisterUserAction
{
public function execute(array $data): User
{
$user = User::create([
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
event(new UserRegistered($user));
return $user;
}
}describedescribe('OrderPolicy', function () {
test('owners can view their orders', function () {
// ...
});
test('owners can cancel pending orders', function () {
// ...
});
test('owners cannot cancel shipped orders', function () {
// ...
});
});beforeEachbeforeEach(function () {
$this->user = User::factory()->create();
$this->actingAs($this->user);
});
test('can create order', function () {
// $this->user is available
});dataset('invalid_emails', [
'empty string' => [''],
'missing @' => ['invalidemail.com'],
'missing domain' => ['test@'],
'spaces' => ['test @example.com'],
]);
test('rejects invalid email formats', function (string $email) {
$response = $this->postJson('/api/users', [
'email' => $email,
'name' => 'Test',
]);
$response->assertJsonValidationErrors(['email']);
})->with('invalid_emails');test('homepage loads successfully')
->get('/')
->assertOk();
test('guests cannot access dashboard')
->get('/dashboard')
->assertRedirect('/login');php artisan test# Run all tests
php artisan test
# Run specific test by name
php artisan test --filter="rejects empty email"
# Run specific test file
php artisan test tests/Feature/OrderTest.php
# Run tests in parallel
php artisan test --parallel
# Run with coverage
php artisan test --coverage
# Run and stop on first failure
php artisan test --stop-on-failure
# Run only dirty tests (changed files)
php artisan test --dirty| Problem | Solution |
|---|---|
| Don't know how to test | Write wished-for API. Write assertion first. Ask your human partner. |
| Test too complicated | Design too complicated. Simplify interface. |
| Must mock everything | Code too coupled. Use dependency injection. |
| Test setup huge | Use factories, traits, helpers. Still complex? Simplify design. |
| Database slow | Use |
Production code → test exists and failed first
Otherwise → not TDD