Laravel 12 Pest Login Testing: Step-by-Step Guide

Pest login testing

Introduction

Testing is a crucial aspect of software development, ensuring that your application works as expected. Laravel 12 comes with built-in support for Pest, a simple and powerful testing framework. In this guide, we will walk through setting up Pest for testing the login functionality in a Laravel 12 Pest Login Testing application.

Prerequisites

Before starting, ensure that you have:

  • Laravel 12 installed
  • Composer installed
  • A working Laravel authentication setup

Installing Pest in Laravel

To install Pest in your Laravel project, run:

composer require pestphp/pest --dev

Then, initialize Pest using:

php artisan pest:install

This command will configure Pest in your Laravel project and set up example tests.

Writing Tests for Login Functionality

Creating a Test File

Navigate to the tests/Feature directory and create a new file named LoginTest.php:

touch tests/Feature/LoginTest.php

Implementing Login Tests

Open the LoginTest.php file and add the following test cases:

<?php

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use function Pest\Laravel\{post, assertAuthenticated, assertGuest};

beforeEach(function () {
    $this->artisan('migrate:fresh');
});

// Successful Login
it('allows a user to log in with valid credentials', function () {
    $user = User::factory()->create([
        'email' => '[email protected]',
        'password' => Hash::make('password123'),
    ]);

    $response = post('/login', [
        'email' => '[email protected]',
        'password' => 'password123',
    ]);

    assertAuthenticated();
    $response->assertRedirect('/home');
});

// Invalid Password
it('rejects login with incorrect password', function () {
    $user = User::factory()->create([
        'email' => '[email protected]',
        'password' => Hash::make('password123'),
    ]);

    $response = post('/login', [
        'email' => '[email protected]',
        'password' => 'wrongpassword',
    ]);

    assertGuest();
    $response->assertSessionHasErrors('email');
});

// Invalid Email
it('rejects login with incorrect email', function () {
    $user = User::factory()->create([
        'email' => '[email protected]',
        'password' => Hash::make('password123'),
    ]);

    $response = post('/login', [
        'email' => '[email protected]',
        'password' => 'password123',
    ]);

    assertGuest();
    $response->assertSessionHasErrors('email');
});

Running the Tests

Execute the following command to run your tests:

php artisan test

or

./vendor/bin/pest

This will execute all defined tests and display the results.

tests/Feature/Auth/LoginTest.php

<?php

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use function Pest\Laravel\{post, assertAuthenticated, assertGuest, actingAs};

// Before each test, ensure database is reset
beforeEach(function () {
    $this->artisan('migrate:fresh');
});

// 1. Successful Login
it('allows a user to log in with valid credentials', function () {
    $user = User::factory()->create([
        'email' => '[email protected]',
        'password' => Hash::make('password123'),
    ]);

    $response = post('/login', [
        'email' => '[email protected]',
        'password' => 'password123',
    ]);

    assertAuthenticated();
    $response->assertRedirect('/home'); // Adjust redirect path as needed
});

// 2. Invalid Password
it('rejects login with incorrect password', function () {
    $user = User::factory()->create([
        'email' => '[email protected]',
        'password' => Hash::make('password123'),
    ]);

    $response = post('/login', [
        'email' => '[email protected]',
        'password' => 'wrongpassword',
    ]);

    assertGuest();
    $response->assertSessionHasErrors('email'); // Laravel usually flashes errors to session
});

// 3. Invalid Email
it('rejects login with incorrect email', function () {
    $user = User::factory()->create([
        'email' => '[email protected]',
        'password' => Hash::make('password123'),
    ]);

    $response = post('/login', [
        'email' => '[email protected]',
        'password' => 'password123',
    ]);

    assertGuest();
    $response->assertSessionHasErrors('email');
});

// 4. Non-Existing User
it('rejects login for non-existing user', function () {
    $response = post('/login', [
        'email' => '[email protected]',
        'password' => 'password123',
    ]);

    assertGuest();
    $response->assertSessionHasErrors('email');
});

// 5. Empty Credentials
it('rejects login with empty credentials', function () {
    $response = post('/login', [
        'email' => '',
        'password' => '',
    ]);

    assertGuest();
    $response->assertSessionHasErrors(['email', 'password']);
});

// 6. Invalid Email Format
it('rejects login with invalid email format', function () {
    $response = post('/login', [
        'email' => 'invalid-email',
        'password' => 'password123',
    ]);

    assertGuest();
    $response->assertSessionHasErrors('email');
});

// 7. Ensure authenticated users are redirected away from login page
it('redirects authenticated users away from login page', function () {
    $user = User::factory()->create();

    actingAs($user)
        ->get('/login')
        ->assertRedirect('/home'); // Adjust to match your app's behavior
});

// 8. Ensure guests cannot access protected routes
it('restricts guests from accessing authenticated routes', function () {
    $response = $this->get('/dashboard'); // Change to a protected route

    $response->assertRedirect('/login'); // Ensure guests are redirected to login
});

What is Pest in Laravel?

Pest is a PHP testing framework that provides a simpler and more expressive way to write tests in Laravel applications.

Why use Pest for login testing in Laravel 12?

Pest offers a clean and readable syntax, making it easier to write and maintain tests for authentication and other features in Laravel.

Leave a Reply

Your email address will not be published. Required fields are marked *