Unit testing is an essential part of software development, it allows developers to ensure code accuracy and improve the performance of their code. In this guide, we’ll explore how to write unit test cases for a Laravel CRUD (Create, Read, Update, Delete) system using the PEST PHP testing framework. Specifically, we’ll focus on the Product model to demonstrate the process effectively.
Prerequisites
Let’s start
Laravel Project
Set up a Laravel project with the necessary configurations, including the Product model and database migrations.
Install new project using composer
composer create-project laravel/laravel example-app
Create Model, Migration, and Controller with resource
php artisan make:model Product -mcr
PEST PHP
Ensure you have PEST PHP installed in your project. You can install it using Composer:
Install pest as a “dev” dependency
composer require pestphp/pest --dev --with-all-dependencies
To use PEST in the Laravel project, you need to install this package using Composer.
composer require pestphp/pest-plugin-laravel --dev
This will add Additional artisan commands and functions to the default PEST installation.
Initialize Pest in your project. This will create a PEST file in the test directory.
./vendor/bin/pest --init OR php artisan pest:install
To create a new Test case run this command, It will generate a test file in the Unit directory.
php artisan pest:test ProductTest --unit
Create a factory for the Product model, and execute this command.
php artisan make:factory ProductFactory
In the ProductTest file inside the Unit directory write these test cases to test crud operations.
<?php use App\Models\Product; it('can create a new product', function () { $productData = [ 'name' => 'New Product', 'price' => 100, ]; $product = Product::create($productData); expect($product->name)->toBe('New Product'); expect($product->price)->toBe(100); }); it('can retrieve a product', function () { $product = Product::factory()->create(); $retrievedProduct = Product::find($product->id); expect($retrievedProduct->name)->toBe($product->name); expect($retrievedProduct->price)->toBe($product->price); }); it('can update a product', function () { $product = Product::factory()->create(); $updatedData = [ 'name' => 'Updated Product Name', 'price' => 150, ]; $product->update($updatedData); expect($product->fresh()->name)->toBe('Updated Product Name'); expect($product->fresh()->price)->toBe(150); }); it('can delete a product', function () { $product = Product::factory()->create(); $product->delete(); expect(Product::find($product->id))->toBeNull(); });
Finally, Run your tests by executing the pest command
./vendor/bin/pest
OR Setup command in composer Script and then run this command.
composer test
Conclusion
Writing unit tests for a Laravel CRUD system using the PEST PHP testing framework is a powerful way to ensure that your code operates as expected. By testing the create, read, update, and delete operations on the Product model, you can confidently build robust and reliable applications. Remember that unit testing is an ongoing process, and as your project evolves, so should your test suite. Happy testing!