Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alons182's avatar

Github Actions Testing Fails when ValidationException is thrown inside a DB:Transaction

I have the following error in Github Actions Testing

 FAILED  Tests\Feature\Cashier\TransactionsControllerTest > it currency of…   
  Session is missing expected key [errors].
Failed asserting that false is true.

The following exception occurred during the last request:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist in /home/runner/work/opking/opking/vendor/laravel/framework/src/Illuminate/Database/Concerns/ManagesTransactions.php:298

in local environment works fine. It is related to transactions because the block of code that generates the error is when a throw ValidationException occurs within the DB::transaction (the test validates that ValidationException is thrown). If I remove that code the test passes both locally and in Github Actions.. Does anyone know what could be happening ?

 return DB::transaction(function () use ($data) {

            $transactions = [];

            foreach ($data['amounts'] as $index => $amountData) {

                $account = Account::findOrFail($amountData['account_id']);
                $currency = Currency::where('code', $amountData['currency_code'])->firstOrFail();

                if ($account->currency_code !== $currency->code) {
	 				// This generate the error in github actions but works locally
                    throw ValidationException::withMessages([
                        'amounts.'.$index.'.account_id' => 'La cuenta seleccionada tiene diferente moneda de la transacción a realizar',
                    ]);
                }

		 // others stuff

This is my workflow yml

# GithHub Actions Workflow generated with Ghygen
# Original configuration: https://ghygen.hi-folks.dev?code=2775514f1bcc55dfdcac1be260942a28
name: Template - Laravel application (Mysql)
on:
  push:
    branches:
      - main
      - develop
      - features/**
  pull_request:
    branches:
      - main
      - develop

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:latest
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
          MYSQL_DATABASE:  db_test_laravel
        ## 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:
      fail-fast: false
      matrix:
        operating-system: [ubuntu-latest]
        php-versions: [ '8.2' ]
        dependency-stability: [ 'prefer-none' ]

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

    steps:
    - uses: actions/checkout@v3
    - 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@v3
      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@v3
      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
      if: steps.vendor-cache.outputs.cache-hit != 'true'
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

    - name: Update Dependencies with latest stable
      if: matrix.dependency-stability == 'prefer-stable'
      run: composer update --prefer-stable
    - name: Update Dependencies with lowest stable
      if: matrix.dependency-stability == 'prefer-lowest'
      run: composer update --prefer-stable --prefer-lowest

    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Run Migrations
# Set environment
      env:
        DB_CONNECTION: mysql
        DB_DATABASE: db_test_laravel
        DB_PORT: 33306
        DB_USER: root

      run: php artisan migrate

    - name: Show dir
      run: pwd
    - name: PHP Version
      run: php --version
    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '18.x'
    - name: Cache node_modules directory
      uses: actions/cache@v3
      id: node_modules-cache
      with:
        path: node_modules
        key: ${{ runner.OS }}-build-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
    - name: Install NPM packages
      if: steps.node_modules-cache.outputs.cache-hit != 'true'
      run: npm ci
    - name: Build frontend
      run: npm run build

# Code quality

    - name: Execute tests (Unit and Feature tests) via PestPHP
# Set environment
      env:
        DB_CONNECTION: mysql
        DB_DATABASE: db_test_laravel
        DB_PORT: 33306
        DB_USER: root

      run: vendor/bin/pest

0 likes
0 replies

Please or to participate in this conversation.