How to Set Up PHPUnit in Laravel .mp4


Here is a complete beginner-friendly, step-by-step guide on:

Download : How to Set Up PHPUnit in Laravel (Full Explanation).mp4

Testing is one of the most important parts of professional Laravel development. Laravel comes with PHPUnit pre-installed, but many beginners don’t know how to configure and use it properly.

This guide will explain everything from scratch.


🔰 What is PHPUnit?

PHPUnit is a testing framework for PHP applications.

It allows you to:

  • Test your application automatically

  • Verify that your code works as expected

  • Prevent bugs when making changes

  • Build reliable and stable applications

Laravel uses PHPUnit as its default testing tool.


✅ Step 1 – Install Laravel (If Not Installed)

Before setting up PHPUnit, you need a Laravel project.

Run this command to create a fresh Laravel project:

composer create-project laravel/laravel laravel-test-app

Then go inside the project folder:

cd laravel-test-app

✅ Step 2 – Verify PHPUnit is Already Installed

Laravel automatically installs PHPUnit when you create a project.

To confirm, check the composer.json file.

Open it and find:

"require-dev": {
    "phpunit/phpunit": "^10.0"
}

This means PHPUnit is already included!


✅ Step 3 – Understand the PHPUnit Configuration File

Laravel provides a pre-configured file named:

📁 phpunit.xml

This file controls how tests run.

Open phpunit.xml in your project root.

You will see something like:

<phpunit bootstrap="vendor/autoload.php"
         colors="true">

This file defines:

  • Test directories

  • Environment variables

  • Database configuration for testing


✅ Step 4 – Testing Directory Structure

Laravel already creates a folder for tests:

📁 tests/

Inside it, there are two folders:

FolderPurpose
FeatureFor testing application features
UnitFor testing small pieces of code

Example:

tests/
 ├── Feature/
 ├── Unit/
 ├── TestCase.php

✅ Step 5 – Running PHPUnit for the First Time

To run PHPUnit tests, simply execute:

php artisan test

OR

vendor/bin/phpunit

If everything is correct, you will see:

✔ All tests passed

This means PHPUnit is successfully set up.


✅ Step 6 – Creating Your First Test

Let’s create a sample test.

Run this command:

php artisan make:test ExampleTest

This creates:

📁 tests/Feature/ExampleTest.php

Open that file:

public function test_example()
{
    $response = $this->get('/');

    $response->assertStatus(200);
}

This test checks:

👉 “Is the homepage loading correctly?”


Run the Test

php artisan test

If your website home page works, the test will pass.


✅ Step 7 – Setting Up Testing Database

Testing should NOT use your real database.

Laravel allows a separate testing database.

Open .env file and create a testing database:

DB_DATABASE=laravel

Now open:

📁 phpunit.xml

Find this section:

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

This means:

✔ Laravel will use an in-memory database for tests
✔ Your real data will stay safe


✅ Step 8 – Writing Unit Tests

Unit tests check small parts of code.

Create a unit test:

php artisan make:test MathTest --unit

Open:

📁 tests/Unit/MathTest.php

Example test:

public function test_addition()
{
    $sum = 2 + 2;
    $this->assertEquals(4, $sum);
}

Run again:

php artisan test

✅ Step 9 – Testing Controllers

Example:

Create a controller test:

php artisan make:test UserControllerTest

Inside the test:

public function test_user_page()
{
    $response = $this->get('/users');

    $response->assertStatus(200);
}

This ensures:

✔ Your routes and controllers work correctly


✅ Step 10 – Testing Models

You can also test database models.

Example:

public function test_user_creation()
{
    $user = User::create([
        'name' => 'Test',
        'email' => 'test@example.com',
        'password' => bcrypt('password')
    ]);

    $this->assertDatabaseHas('users', [
        'email' => 'test@example.com'
    ]);
}

🎯 Why PHPUnit is Important in Laravel

Using PHPUnit helps you:

  • Catch bugs early

  • Improve code quality

  • Automate testing

  • Save development time

  • Build professional applications


🛠 Common PHPUnit Commands

CommandPurpose
php artisan testRun all tests
vendor/bin/phpunitRun PHPUnit manually
php artisan make:test TestNameCreate new test
php artisan make:test TestName --unitCreate unit test

🚀 Final Conclusion

Setting up PHPUnit in Laravel is very easy because:

✔ Laravel includes PHPUnit by default
✔ No extra installation needed
✔ Ready-to-use configuration
✔ Powerful testing features

By learning PHPUnit, you become a better Laravel developer.


Enjoy! Follow us for more... 

No comments:

Post a Comment

How to Set Up PHPUnit in Laravel .mp4

Here is a complete beginner-friendly, step-by-step guide on: Download :  How to Set Up PHPUnit in Laravel (Full Explanation).mp4 Testing is...