name: PHP Unit Tests
on:
  push:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

jobs:
  main_phpunit:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      max-parallel: 1
      matrix:
        php-versions: ['8.2']
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Setup PHP, with composer and extensions
      uses: shivammathur/setup-php@v2 #https://github.com/shivammathur/setup-php
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: mbstring, dom, fileinfo, mysql, gd, curl, zip, sqlite, xml, intl, bcmath, json, zlib, openssl, iconv
        # tools: phpunit #optional
    - name: Get composer cache directory
      id: composer-cache
      run: echo "::set-output name=dir::$(composer config cache-files-dir)"
    - name: Cache composer dependencies
      uses: actions/cache@v3
      with:
        path: ${{ steps.composer-cache.outputs.dir }}
        # Use composer.json for key, if composer.lock is not committed.
        # key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
        key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
        restore-keys: ${{ runner.os }}-composer-
    - name: Install Composer dependencies
      run: composer install --no-progress --prefer-dist --optimize-autoloader
    - name: Npm install
      uses: actions/setup-node@v3
      with:
        node-version: 16
        cache: 'npm'
        cache-dependency-path: ./package-lock.json
    - run: npm install
    - run: npm run build
    - name: Run PHPUnit
      run: php artisan test
    - name: Upload Screenshots
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: screenshots
        path: tests/Browser/screenshots
    - name: Upload Console Logs
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: console
        path: tests/Browser/console
    - name: Upload Laravel Storage Logs
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: storage-logs
        path: storage/logs
    - name: Upload Laravel config
      if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: config
        path: config
