Hello and welcome!π In today's post, we'll be accelerating our PHP projects by integrating a continuous integration process with Pest.πΌ Pest is a great testing framework for PHP, and itβs admired for its simplicity and powerful features. Our task today is to integrate Pest into our CI/CD pipeline, a very efficient way for automatic testing. Let's do this!π
Step by Step:
Step 1: Install Pest in Your Project π¦
The first step is installing Pest in our Laravel project. We will be using Composer, an application-level package manager for the PHP programming language. Fire up your terminal and run the following command:
composer require pestphp/pest --dev
This will add Pest to your project as a dev dependency.
Step 2: Run a Pest Test π§ͺ
Now, let's run a Pest test to understand how it works.
Create a new file within the tests
directory, and let's name it ExampleTest.php
. In this file, we will write the following code:
<?php
it('has home page', function () {
$response = get('/');
$response->assertStatus(200);
});
Now, here is how to test it:
vendor/bin/pest
If everything set up properly, it should display that the tests have passed! π
Step 3: Set up Continuous Integration π οΈ
Once we have Pest working properly in our project, the next step is to integrate it into our CI/CD pipeline. There are various CI/CD tools out there; in this tutorial, we'll use GitHub Actions.
In your project's GitHub repository, go to the Actions
tab and create a new workflow. Inside the yaml configuration, make the following configuration:
name: Run Pest tests
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: none
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Run Pest tests
run: vendor/bin/pest
This workflow runs every time we make a push or pull request to the master
branch. It sets up PHP, installs our dependencies, and then runs our Pest tests.
That's pretty much it! We've just accelerated our PHP projects by integrating continuous integration with Pest. With this setup, we can now ensure our code works as expected every time we make changes to our code. This, in turn, improves our project's reliability and keeps our code maintainable. Hooray! π₯³
Dig Deeper π
Would you like to know more about the technologies weβve used today? Use the links below for reference, but please note that they could be outdated as technology evolves rapidly.
- Pest - Official Documentation
- Composer - Official Documentation
- GitHub Actions - Official Documentation
Thank you for reading through. See you in the next post! π