How to automate tests by Github Workflow with Laravel Pint and PHP Pest

maniruzzamanakash

Maniruzzaman Akash

Posted on May 13, 2023

How to automate tests by Github Workflow with Laravel Pint and PHP Pest

What is Github Workflow

GitHub Workflow is a powerful tool that allows developers to automate various tasks and processes related to software development, such as building, testing, and deploying applications.

What is Laravel Pint

Laravel Pint is an opinionated PHP code style fixer for minimalists. Pint is built on top of PHP-CS-Fixer and makes it simple to ensure that your code style stays clean and consistent.

What is PHP PEST

PHP Pest is a testing framework for PHP applications. It is inspired by Jest, a popular testing framework for JavaScript. Pest provides a clean and expressive syntax for writing tests and comes with a wide range of features to simplify the testing process.

We can say PHP Pest is a total alternative for PHPUnit. Pest is more simple and has more features than PHPUnit. Laravel has lots of support for this library.

How to create that Github workflow for Laravel Pint and PHP PEST

Create a file in your repository under .github/workflows/php-tests.yml

Where in the .github folder, we鈥檝e to create another folder workflows, then create a file php-test.yml. This file name could be anything. Let鈥檚 write the below codes in php-test.yml.



name: Laravel Tests
on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

jobs:
  laravel-tests:
    runs-on: ubuntu-latest
    # Service container Mysql mysql
    services:
      # Label used to access the service container
      mysql:
        # Docker Hub image (also with version)
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: secret
          MYSQL_DATABASE:  testing
        ## map the "external" 33306 port with the "internal" 3306
        ports:
          - 33306:3306
        # Set health checks to wait until mysql database has started (it takes some seconds to start)
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

    strategy:
      matrix:
        operating-system: [ubuntu-latest]
        php-versions: [ '8.1' ]
        dependency-stability: [ prefer-stable ]

    name: P${{ matrix.php-versions }} - L${{ matrix.laravel }} - ${{ matrix.dependency-stability }} - ${{ matrix.operating-system}}

    steps:
      - uses: actions/checkout@v2
      - name: Install PHP versions
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
      - name: Get Composer Cache Directory 2
        id: composer-cache
        run: |
          echo "::set-output name=dir::$(composer config cache-files-dir)"
      - uses: actions/cache@v2
        id: actions-cache
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-composer-
      - name: Cache PHP dependencies
        uses: actions/cache@v2
        id: vendor-cache
        with:
          path: vendor
          key: ${{ runner.OS }}-build-${{ hashFiles('**/composer.lock') }}
      - name: Copy .env
        run: php -r "file_exists('.env') || copy('.env.example', '.env');"
      - name: Install Dependencies
        run: composer install --no-interaction --prefer-dist --optimize-autoloader

      - name: Create storage folders
        run: mkdir -p storage/framework/{sessions,views,cache}
      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache

      - name: Show dir
        run: pwd
      - name: PHP Version
        run: php --version

      # Code quality
      - name: Execute tests (Unit and Feature tests) via PestPHP
        # Set environment
        env:
          DB_CONNECTION: mysql
          DB_DATABASE: testing
          DB_PORT: 33306
          DB_USER: root
          DB_PASSWORD: secret
        run: |
          php artisan key:generate
          vendor/bin/pest

      - name: Run Pint
        run: vendor/bin/pint


Enter fullscreen mode Exit fullscreen mode

For more and step by setup instruction and every line meaning, please check this article, it will be super helpful to clear your concept - https://devsenv.com/tutorials/how-to-setup-github-workflow-for-laravel-pint-and-php-pest-testing

Demo Github workflow response

Image description

馃挅 馃挭 馃檯 馃毄
maniruzzamanakash
Maniruzzaman Akash

Posted on May 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related