Home >Backend Development >PHP Tutorial >How to Create a Test Case in Laravel Using Pest
Testing your Laravel application is crucial to ensure that your code works as expected. Pest is a testing framework for PHP, designed to be minimalistic and user-friendly. In this blog post, we'll walk through creating a test case in Laravel using Pest, focusing on an example where we test the creation of an Employer record, including uploading a logo.
Prerequisites
If you haven't installed Pest yet, you can do so by following the official Pest installation guide.
Ensure your User and Employer models are correctly set up with the necessary relationships.
User Model (app/Models/User.php):
namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Database\Eloquent\Relations\HasOne; class User extends Authenticatable { public function employer(): HasOne { return $this->hasOne(Employer::class); } }
Employer Model (app/Models/Employer.php):
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; class Employer extends Model { protected $fillable = ['name', 'email', 'phone', 'address', 'city', 'website', 'user_id', 'logo']; public function user(): BelongsTo { return $this->belongsTo(User::class); } }
Create factories for generating test data.
User Factory (database/factories/UserFactory.php):
namespace Database\Factories; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; class UserFactory extends Factory { protected $model = User::class; public function definition() { return [ 'name' => $this->faker->name(), 'email' => $this->faker->unique()->safeEmail(), 'email_verified_at' => now(), 'password' => bcrypt('password'), // password 'remember_token' => Str::random(10), ]; } }
Employer Factory (database/factories/EmployerFactory.php):
namespace Database\Factories; use App\Models\Employer; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Http\UploadedFile; class EmployerFactory extends Factory { protected $model = Employer::class; public function definition() { return [ 'name' => $this->faker->company, 'email' => $this->faker->companyEmail, 'phone' => $this->faker->phoneNumber, 'address' => $this->faker->address, 'city' => $this->faker->city, 'website' => $this->faker->url, UploadedFile::fake()->image('logo.png') ]; } }
Create a controller method to handle the creation of an Employer.
Employer Controller (app/Http/Controllers/EmployerController.php):
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Employer; use Illuminate\Support\Facades\Storage; class EmployerController extends Controller { public function __construct() { } public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email', 'phone' => 'required|string', 'address' => 'nullable|string', 'city' => 'nullable|string', 'website' => 'nullable|url', 'logo' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); if ($request->hasFile('logo')) { $path = $request->file('logo')->store('logos', 'public'); $validated['logo'] = $path; } $employer = $request->user()->employer()->create($validated); return response()->json($employer, 201); } }
Create test files and write test cases to verify the creation of an Employer, including uploading a logo.
Create the Test File:
php artisan pest:test EmployerControllerTest
Write the Test Cases (tests/Feature/EmployerControllerTest.php):
<?php use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Auth; use App\Models\User; use App\Models\Employer; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; uses(RefreshDatabase::class); it('prevents guests from creating an employer', function () { // Define the data to be sent in the POST request $data = [ 'name' => 'Test Employer', 'email' => 'test@employer.com', 'phone' => '1234567890', 'address' => '123 Employer St', 'city' => 'Employer City', 'website' => 'https://www.employer.com', ]; // Send the POST request to create the employer as a guest (unauthenticated) $response = $this->post('/api/employers', $data); // Assert that the response status is 401 (Unauthorized) $response->assertStatus(401); // Optionally, check that the employer was not created $this->assertDatabaseMissing('employers', [ 'name' => 'Test Employer', 'email' => 'test@employer.com', ]); }); it('creates a new employer for authenticated user', function () { // Create a user and log them in $user = User::factory()->create(); Auth::login($user); // Define the data to be sent in the POST request $data = [ 'name' => 'Test Employer', 'email' => 'test@employer.com', 'phone' => '1234567890', 'address' => '123 Employer St', 'city' => 'Employer City', 'website' => 'https://www.employer.com', ]; // Send the POST request to create the employer $response = $this->post('/api/employers', $data); // Assert that the response status is 201 (Created) $response->assertStatus(201); // Assert that the employer was created $this->assertDatabaseHas('employers', [ 'name' => 'Test Employer', 'email' => 'test@employer.com', ]); }); it('creates a new employer with a logo', function () { // Create a user and log them in $user = User::factory()->create(); Auth::login($user); // Fake a storage disk for testing Storage::fake('public'); // Define the data to be sent in the POST request $data = [ 'name' => 'Test Employer', 'email' => 'test@employer.com', 'phone' => '1234567890', 'address' => '123 Employer St', 'city' => 'Employer City', 'website' => 'https://www.employer.com', 'logo' => UploadedFile::fake()->image('logo.png'), // Fake file for testing ]; // Send the POST request to create the employer $response = $this->post('/api/employers', $data); // Assert that the response status is 201 (Created) $response->assertStatus(201); // Optionally, check if the employer was actually created $this->assertDatabaseHas('employers', [ 'name' => 'Test Employer', 'email' => 'test@employer.com', ]); // Check that the file was uploaded Storage::disk('public')->assertExists('logos/logo.png'); // Adjust path as needed });
Step 5: Running Your Pest Tests
Run your Pest tests to ensure everything works as expected:
php artisan test --testsuit=Feature
By following these steps, you can create test cases in Laravel using Pest to verify the creation of an Employer record, including handling file uploads. This approach ensures that your application behaves as expected and helps catch any issues early in the development process. Happy testing!
The above is the detailed content of How to Create a Test Case in Laravel Using Pest. For more information, please follow other related articles on the PHP Chinese website!