Module 8: Testing & CI/CD

Pest PHP, TDD, and Deployment Pipelines

Pest PHP

Pest is a testing framework with a focus on simplicity and elegance. It's the default in Laravel 11.

Unit Tests

test('sum', function () {
    $result = 1 + 1;
    expect($result)->toBe(2);
});

Feature Tests

test('user can view dashboard', function () {
    $user = User::factory()->create();
 
    $response = this->actingAs($user)
                     ->get('/dashboard');
 
    $response->assertStatus(200);
});

Model Factories

Generate fake data for testing.

// UserFactory.php
public function definition(): array
{
    return [
        'name' => fake()->name(),
        'email' => fake()->unique()->safeEmail(),
        'password' => static::$password ??= Hash::make('password'),
    ];
}

// Usage in Test
$users = User::factory()->count(10)->create();

CI/CD Pipeline (GitHub Actions)

Automate your testing and deployment.

# .github/workflows/laravel.yml
name: Laravel

on: [push, pull_request]

jobs:
  laravel-tests:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.2'
    - name: Install Dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Execute tests (Pest)
      run: vendor/bin/pest

Deployment (Laravel Forge)

Forge is the easiest way to deploy Laravel applications to DigitalOcean, Linode, AWS, etc.