Chapter 6: Continuous Integration with PestPHP

Chapter 6: Continuous Integration with PestPHP

In this chapter, we'll explore how PestPHP integrates with continuous integration (CI) systems, allowing you to seamlessly incorporate testing into your automated build pipelines. Continuous integration ensures that your tests are run automatically whenever changes are made to your codebase, providing quick feedback on the health of your application.

Understanding Continuous Integration

Continuous integration is a software development practice where code changes are automatically tested and integrated into the main codebase. CI systems monitor version control repositories and trigger automated builds and tests whenever changes are pushed.

Setting Up PestPHP in a CI Environment

To integrate PestPHP into your CI environment, follow these general steps:

  1. Install Dependencies: Ensure that the necessary dependencies, including PestPHP, are installed as part of your CI build process. This typically involves running Composer to install dependencies.

     composer install
    
  2. Run PestPHP Tests: Execute PestPHP tests as part of your CI build script. The exact command may vary depending on your CI system.

     ./vendor/bin/pest
    

CI Configuration Examples

GitHub Actions

Here's an example GitHub Actions workflow configuration (.github/workflows/laravel_test.yml) that runs PestPHP tests:

name: Run Tests

on:
  push:
    branches:
      - v2

jobs:
   runs-on: ubuntu-latest
   steps:
     - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
       with:
         php-version: '8.3'
     - uses: actions/checkout@v2
     - name: Install Dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
     - name: Copy .env
       run: php -r "file_exists('.env') || copy('.env.example', '.env');"
      - name: Generate key
        run: php artisan key:generate
      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache
      - name: Create Database
        run: |
          mkdir -p database
          touch database/database.sqlite
      - name: Execute tests (Unit and Feature tests) via pest
        env:
          DB_CONNECTION: sqlite
          DB_DATABASE: database/database.sqlite
        run: vendor/bin/pest

GitLab CI

For GitLab CI, you can use a .gitlab-ci.yml file:

tages:
  - test

test:
  stage: test
  script:
    - composer install
    - ./vendor/bin/pest

Other CI Systems

Adapting PestPHP to other CI systems involves a similar approach. Ensure that Composer dependencies are installed and execute the PestPHP command as part of your build script.

Conclusion

Integrating PestPHP with continuous integration systems ensures that your tests are automatically executed whenever changes are made to your codebase. This practice promotes early detection of issues, enhances code quality, and provides developers with rapid feedback on the impact of their changes. By seamlessly incorporating PestPHP into your CI workflows, you can maintain a robust and reliable testing process throughout the development lifecycle. In the final chapter, we'll summarize the key concepts covered in this guide and provide additional resources for further exploration.

Did you find this article valuable?

Support Kasenda's Blog by becoming a sponsor. Any amount is appreciated!