skillfed

laravel-tdd

Laravel-TDD guides you through test-driven development for Laravel applications, covering PHPUnit and Pest frameworks alongside model factories, HTTP controller testing, and Sanctum authentication verification. Learn the red-green-refactor cycle with practical examples for unit tests, feature tests, and JSON API testing, plus techniques for mocking external services and validating authorization.

Laravel-TDD teaches test-driven development patterns for Laravel apps using PHPUnit, Pest, factories, and HTTP testing.

AI-generated summary based on this skill's SKILL.md

234,207 35,692 MIT updated by affaan-m

Install

affaan-m/ECC/laravel-tdd · repository language: JavaScript

git clone https://github.com/affaan-m/ECC
cp -r ECC/skills/laravel-tdd ~/.claude/skills/laravel-tdd
npx skillfed install affaan-m/ECC/laravel-tdd

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How to write tests in laravel using PHPUnit and Pest?

Laravel-TDD teaches both PHPUnit and Pest frameworks for writing tests in Laravel. PHPUnit is the default testing framework included with Laravel, while Pest offers a more elegant, expressive syntax built on top of PHPUnit. Laravel-TDD covers writing unit tests for models and business logic, feature tests for HTTP requests and controllers, and JSON API tests. You'll learn the red-green-refactor cycle: write a failing test, implement code to pass it, then refactor. Both frameworks integrate seamlessly with Laravel's testing utilities like factories, faking, and assertion helpers.

What does Laravel-TDD cover for testing Eloquent models?

Laravel-TDD provides comprehensive guidance on testing Eloquent models, including model factory examples to generate test data efficiently. You'll learn how to test model relationships, scopes, mutators, and custom methods. The skill covers setting up database testing with factories, using seeders for test data, and writing assertions that validate model behavior. Laravel-TDD emphasizes isolating model logic in unit tests before testing them through feature tests that exercise the full application stack.

How does Laravel-TDD approach testing Laravel controllers and APIs?

Laravel-TDD guides you through writing feature tests for controllers and HTTP endpoints using Laravel's testing utilities. You'll learn to test controller actions, validate HTTP responses, and verify JSON API responses. The skill covers testing form validation, checking status codes, inspecting response data, and following redirects. For APIs specifically, Laravel-TDD teaches JSON API testing patterns, including testing Sanctum authentication, verifying authorization policies, and mocking external service calls within API tests.

How can I test Sanctum API authentication in Laravel?

Laravel-TDD covers testing Sanctum authentication and API authorization comprehensively. You'll learn to create authenticated test requests using Sanctum tokens, verify that protected endpoints reject unauthenticated requests, and test token expiration and revocation. The skill includes testing authorization policies to ensure users can only access resources they're permitted to use. Laravel-TDD demonstrates mocking authenticated users, testing role-based access control, and validating API responses for both successful and unauthorized scenarios.

What techniques does Laravel-TDD teach for mocking external services?

Laravel-TDD covers mocking external services to isolate your tests and avoid real API calls during testing. You'll learn to use Laravel's faking utilities for common services like mail, queues, and HTTP requests. The skill teaches how to mock third-party APIs, verify that your code makes correct calls to external services, and test error handling when external services fail. Laravel-TDD emphasizes using factories and fakes to create predictable test environments, ensuring tests run fast and reliably without external dependencies.

How does Laravel-TDD help achieve code coverage targets?

Laravel-TDD guides you through measuring test effectiveness and achieving code coverage goals. You'll learn to run coverage reports using PHPUnit, identify untested code paths, and write tests strategically to reach coverage targets. The skill emphasizes that high coverage alone doesn't guarantee quality—Laravel-TDD teaches writing meaningful tests that validate behavior, not just line coverage. You'll learn to balance unit and feature tests, prioritize critical paths, and use coverage metrics to guide your test-driven development workflow.

SKILL.md

rendered from the published skill — quoted content, verbatim

Laravel Testing with TDD

Test-driven development for Laravel applications using PHPUnit, Pest, Laravel factories, and testing helpers.

When to Activate

  • Writing new Laravel applications or features
  • Implementing API endpoints with Sanctum or Passport authentication
  • Testing Eloquent models, relationships, scopes, and accessors
  • Setting up testing infrastructure for Laravel projects
  • Writing feature tests for HTTP controllers and form requests
  • Mocking external services (queues, mail, notifications, HTTP)

TDD Workflow for Laravel

Red-Green-Refactor Cycle
// Step 1: RED — Write a failing test
public function test_a_product_can_be_created(): void
{
    $product = Product::factory()->create(['name' => 'Test Product']);
    $this->assertDatabaseHas('products', ['name' => 'Test Product']);
}

// Step 2: GREEN — Write the migration, model, and factory
// Step 3: REFACTOR — Improve while keeping tests green

Setup

PHPUnit Configuration

```xml <?xml

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
skills/laravel-tdd/SKILL.md

Related skills

Tags

test-automation code-quality api-validation mock-services database-seeding auth-testing coverage-metrics integration-tests behavior-driven