Deploying a new Laravel site on Github, test runner reports: "SQLSTATE[HY000] [2002] No such file or directory"

GitHub Testing

When pushing a new Laravel repo to GitHub that has tests enabled, you might encounter the following error on the "Execute the tests" section:

SQLSTATE[HY000] [2002] No such file or directory

A full screenshot of what this looks like is this:

Screenshot 2024-02-16 at 07.55.32.png 79.68 KB
Basically what's happening is your Laravel application is trying to access MySQL but MySQL is not set up.

This beg a few questions, starting with "what does you .github/workflows/run-tests.yml file look like?"

In this example, the file looks like this:

name: tests

on:
  push:
  pull_request:

jobs:
  tests:
    runs-on: ubuntu-22.04

    strategy:
      fail-fast: true
      matrix:
        php: ['8.2']
        laravel: [10]

    name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: dom, curl, libxml, mbstring, zip, bcmath
          ini-values: error_reporting=E_ALL
          tools: composer:v2
          coverage: none

      - name: Install composer dependencies
        run: |
          composer require "illuminate/contracts=^${{ matrix.laravel }}" --no-update
          composer update --prefer-dist --no-interaction --no-progress

      - name: Copy .env.example and generate the application key
        run: |
          cp .env.example .env
          php artisan key:generate

      - name: Execute the tests
        run: vendor/bin/phpunit
lang-yml

The file is super simple and solely caters for testing a Laravel 10 app on PHP 8.2 and Ubuntu 22.04 as the operating system.

Clearly, there is no MySQL running.

You next instinct might be to deploy MySQL on the test runner. However, this is quite complicated, so what other options exist?

In my opinion it's often the best option to use SQLite as it's much faster and much less overhead on your test runner meaning you have quicker tests returning.