Writing PHPUnit Tests in Laravel: A Beginner's Guide
Ihor Chyshkala
Fintech Code Alchemist: Transmuting Ideas into Reality with JS & PHP. DevOps Wizard: Transforming Infrastructure into Cloud Gold | Orchestrating CI/CD Magic | Crafting Automation Elixirs
Introduction
Unit testing is a crucial part of modern software development. It ensures that your codebase is clean and works as expected. Laravel, one of the most popular PHP frameworks, comes with PHPUnit out-of-the-box to facilitate testing. This article aims to guide you through the basics of writing PHPUnit tests in a Laravel application.
Setting Up
Assuming you've already installed Laravel, the phpunit.xml file should be in your project root. This is the configuration file for PHPUnit tests in Laravel. Run the following command to check if PHPUnit is working as expected:
./vendor/bin/phpunit
If you see a green bar with tests executed, you're good to go!
Directory Structure
Tests are stored in the tests directory. It has two sub-directories:
Writing Your First Test
Navigate to the tests/Unit directory, and create a new test file, let's say ExampleTest.php.
Here's how a simple test to add numbers looks like:
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function test_addition()
{
$this->assertEquals(2, 1 + 1);
}
}
Run the test using:
./vendor/bin/phpunit --filter ExampleTest
You should see a green bar, indicating that the test passed.
领英推荐
Laravel Specific Tests
For more Laravel-specific features, you would generally extend Tests\TestCase class, instead of PHPUnit's TestCase.
Here's an example of a test that checks if a user can be created:
<?php
namespace Tests\Feature;
use App\Models\User;
use Tests\TestCase;
class UserTest extends TestCase
{
/**
* Test creating a user.
*
* @return void
*/
public function test_create_user()
{
$user = User::factory()->create();
$this->assertDatabaseHas('users', ['email' => $user->email]);
}
}
Run the test using:
./vendor/bin/phpunit --filter UserTest
Advanced Concepts
Mocking
Sometimes you need to mock certain aspects of the system to isolate the code you are testing. Laravel and PHPUnit offer powerful mocking capabilities.
public function test_notification_is_sent()
{
$user = User::factory()->create();
Notification::fake();
// ... perform actions leading to notification
Notification::assertSentTo($user, SomeNotification::class);
}
Data Providers
Data providers allow you to run a test multiple times with different sets of data.
/**
* @dataProvider additionProvider
*/
public function test_addition_with_data_provider($a, $b, $expected)
{
$this->assertEquals($expected, $a + $b);
}
public function additionProvider()
{
return [
[0, 0, 0],
[0, 1, 1],
[1, 0, 1],
[1, 1, 2]
];
}
PHPUnit in Laravel offers an elegant and powerful way to write tests ensuring your application works as expected. From simple assertions to complex mocking, Laravel provides a convenient way to maintain a healthy, robust codebase.
Whether you are a beginner or an experienced developer, testing should be an integral part of your development process. Happy testing!