on:
  - pull_request
  - push

name: build

jobs:
  tests:
    name: PHP ${{ matrix.php }}-${{ matrix.os }}
    env:
      extensions: curl, cubrid, mbstring, memcached, dom, intl, json, libxml, pdo, pdo_mysql, pdo_pgsql, pdo_sqlite, xml, xmlwriter, mcrypt
      key-php5: cache-v1
      key-php7: cache-v2

    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os:
          - ubuntu-latest

        php:
          - "5.3"
          - "5.4"
          - "5.5"
          - "5.6"
          - "7.0"
          - "7.1"
          - "7.2"
          - "7.3"
          - "7.4"
          - "8.0"
          - "8.1"
          - "8.2"

    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_USER: test
          MYSQL_PASSWORD: test
          MYSQL_DATABASE: yii
          MYSQL_ROOT_PASSWORD: test
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
      postgres:
        image: postgres:9.6
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: yii
        ports:
          - 5432:5432
        options: --name=postgres --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3
      redis:
        image: redis
        ports:
          - 6379:6379
        options:
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Install service memcached
        uses: niden/actions-memcached@v7

      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup cache environment 5.3 - 5.6
        if: matrix.php < '7.0'
        id: cache-env-php5
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ matrix.php }}
          extensions: ${{ env.extensions-php5 }}
          key: ${{ env.key-php5 }}

      - name: Setup cache environment 7.0 - 8.0
        if: matrix.php >= '7.0'
        id: cache-env-php7
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ matrix.php }}
          extensions: ${{ env.extensions-php7 }}
          key: ${{ env.key-php7 }}

      - name: Cache extensions 5.3 - 5.6
        if: matrix.php < '7.0'
        uses: actions/cache@v3
        with:
          path: ${{ steps.cache-env-php5.outputs.dir }}
          key: ${{ steps.cache-env-php5.outputs.key }}
          restore-keys: ${{ steps.cache-env-php5.outputs.key }}

      - name: Cache extensions 7.0 - 8.0
        if: matrix.php >= '7.0'
        uses: actions/cache@v3
        with:
          path: ${{ steps.cache-env-php7.outputs.dir }}
          key: ${{ steps.cache-env-php7.outputs.key }}
          restore-keys: ${{ steps.cache-env-php7.outputs.key }}

      - name: Install PHP with extensions
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: ${{ env.extensions }}
          ini-values: date.timezone='UTC'
          coverage: xdebug
          tools: composer:v2, pecl

      - name: Determine composer cache directory on Linux
        run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

      - name: Determine composer cache directory on Windows
        if: matrix.os == 'windows-latest'
        run: echo "COMPOSER_CACHE_DIR=~\AppData\Local\Composer" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

      - name: Cache dependencies installed with composer
        uses: actions/cache@v3
        with:
          path: ${{ env.COMPOSER_CACHE_DIR }}
          key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
          restore-keys: |
            php${{ matrix.php }}-composer-

      - name: Install dependencies with composer
        run: composer update --no-interaction --no-progress --optimize-autoloader --ansi

      - name: Run tests with phpunit
        run: |
          cd tests
          ../vendor/bin/phpunit --verbose --colors=always --no-globals-backup --exclude-group mssql,oci,cubrid framework
        env:
          # The hostname used to communicate with the Redis service container
          REDIS_HOST: localhost
          # The default Redis port
          REDIS_PORT: 6379
