---
name: Quality Assurance
concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true
on:
  push:
    paths:
      - src/**/*.php
      - .github/workflows/quality-assurance.yml
    branches:
      - 3.x
      - 4.x
  pull_request:
    paths:
      - src/**/*.php
      - .github/workflows/quality-assurance.yml
    branches:
      - 3.x
      - 4.x
  schedule:
    - cron: "5 1 * * *"

env:
  FLYSYSTEM_AWS_S3_KEY: '${{ secrets.FLYSYSTEM_AWS_S3_KEY }}'
  FLYSYSTEM_AWS_S3_SECRET: '${{ secrets.FLYSYSTEM_AWS_S3_SECRET }}'
  FLYSYSTEM_AWS_S3_BUCKET: '${{ secrets.FLYSYSTEM_AWS_S3_BUCKET }}'
  MONGODB_URI: 'mongodb://127.0.0.1:27017/'
  FLYSYSTEM_TEST_DANGEROUS_THINGS: "yes"
  FLYSYSTEM_TEST_SFTP: "yes"

jobs:
  phpunit:
    name: PHPUnit tests on ${{ matrix.php }} ${{ matrix.composer-flags }}
    runs-on: ubuntu-latest
    continue-on-error: ${{ matrix.experimental }}
    strategy:
      fail-fast: false
      matrix:
        php: [ '8.0', '8.1' ]
        composer-flags: [ '' ]
        experimental: [false]
        phpstan: [true]
        phpunit-flags: [ '--coverage-text' ]
        include:
          - php: '8.0'
            composer-flags: '--prefer-lowest'
            experimental: false
            phpstan: false
            phpunit-flags: '--no-coverage'
          - php: '8.2'
            experimental: false
            phpstan: false
            phpunit-flags: '--no-coverage'
          - php: '8.3'
            experimental: false
            phpstan: false
            phpunit-flags: '--no-coverage'
          - php: '8.4'
            experimental: true
            phpstan: false
            phpunit-flags: '--no-coverage'
    steps:
      - uses: actions/checkout@v4
      - run: docker compose -f docker-compose.yml up -d
      - name: Start an SSH Agent
        uses: frankdejonge/use-ssh-agent@1.1.0
      - run: chmod 0400 ./test_files/sftp/id_*
      - id: ssh_agent
        run: ssh-add ./test_files/sftp/id_rsa
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: mongodb
          coverage: pcov
          tools: composer:v2
      - run: composer update --no-progress ${{ matrix.composer-flags }}
      - run: php test_files/wait_for_sftp.php
      - run: php test_files/wait_for_ftp.php 2121
      - run: php test_files/wait_for_ftp.php 2122
      - run: COMPOSER_OPTS='${{ matrix.composer-flags }}' vendor/bin/phpunit ${{ matrix.phpunit-flags }}
      - run: vendor/bin/phpstan analyse
        if: ${{ matrix.phpstan }}
      - run: vendor/bin/php-cs-fixer fix --diff --dry-run
        continue-on-error: true
        if: ${{ matrix.php == '8.0' }}

