Home > Article > Backend Development > Integration of PHP unit testing and continuous delivery
Abstract: Improve PHP code quality and accelerate software delivery by integrating the PHPUnit unit testing framework and CI/CD pipeline. PHPUnit allows the creation of test cases to verify component functionality, and CI/CD tools such as GitLab CI and GitHub Actions can automatically run these tests. Example: Validate the authentication controller with test cases to ensure the login functionality works as expected.
In today’s rapidly evolving world of software development, automated testing and continuous delivery (CI/CD) have become crucial. This article describes how to integrate the PHPUnit unit testing framework with a CI/CD pipeline in your PHP project to improve code quality and accelerate software delivery.
What is PHPUnit?
PHPUnit is an open source unit testing framework for PHP applications. It allows you to write unit tests to verify that individual components of your application work as expected.
Create Test Case
To create a PHPUnit test case, create a PHP file ending with Test
and write the following code in it:
<?php namespace Tests; use PHPUnit\Framework\TestCase; class ExampleTest extends TestCase { public function testExample() { $this->assertTrue(true); } }
In this example, the ExampleTest
class inherits the TestCase
class and contains a test method named testExample
.
Use GitLab CI
GitLab CI is a popular CI/CD tool that can automatically build and test and deploy code. To integrate PHPUnit tests into GitLab CI, add the following content to the .gitlab-ci.yml
file:
image: php:7.4 stages: - test test: stage: test script: vendor/bin/phpunit
This configuration will use the PHP 7.4 image to build and run PHPUnit tests.
Using GitHub Actions
GitHub Actions is another CI/CD tool that supports running jobs on different platforms. To integrate PHPUnit tests into GitHub Actions, add the following content to the .github/workflows/main.yml
file:
name: CI on: push: branches: [ main ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: php-version: 7.4 - run: composer install - run: vendor/bin/phpunit
This configuration will run on an Ubuntu server using the PHP 7.4 image PHPUnit test.
Scenario: Validating the Authentication Controller
Consider a PHP web application where we want to test the authentication controller. The following test case can be used to verify the login method:
<?php namespace Tests; use PHPUnit\Framework\TestCase; use App\Http\Controllers\Auth\LoginController; class AuthControllerTest extends TestCase { public function testLogin() { // Mock request and response $request = Request::create('/login', 'POST', ['email' => 'john@example.com', 'password' => 'secret']); $controller = new LoginController(); $response = $controller->login($request); // Assert that the response contains a success message $this->assertStringContainsString('Login successful', $response->getContent()); } }
By integrating this unit test into the CI/CD pipeline, we can ensure that the authentication controller works as expected on every code change.
The above is the detailed content of Integration of PHP unit testing and continuous delivery. For more information, please follow other related articles on the PHP Chinese website!