name: "Integrate"

on:
  pull_request: null
  push:
    branches:
      - "main"

jobs:
  coding-standards:
    name: "Coding Standards"

    runs-on: "ubuntu-latest"

    strategy:
      matrix:
        php-version:
          - "7.4"

        dependencies:
          - "locked"

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2.3.4"

      - name: "Install PHP"
        uses: "shivammathur/setup-php@2.9.0"
        with:
          coverage: "none"
          php-version: "${{ matrix.php-version }}"

      - name: "Validate composer.json and composer.lock"
        run: "composer validate --strict"

      - name: "Determine composer cache directory"
        id: "determine-composer-cache-directory"
        run: "echo \"::set-output name=directory::$(composer config cache-dir)\""

      - name: "Cache dependencies installed with composer"
        uses: "actions/cache@v2.1.3"
        with:
          path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
          key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
          restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

      - name: "Install lowest dependencies from composer.json"
        if: "matrix.dependencies == 'lowest'"
        run: "composer update --no-interaction --no-progress --no-suggest --prefer-lowest"

      - name: "Install locked dependencies from composer.lock"
        if: "matrix.dependencies == 'locked'"
        run: "composer install --no-interaction --no-progress --no-suggest"

      - name: "Install highest dependencies from composer.json"
        if: "matrix.dependencies == 'highest'"
        run: "composer update --no-interaction --no-progress --no-suggest"

      - name: "Run friendsofphp/php-cs-fixer"
        run: "vendor/bin/php-cs-fixer fix --config=.php_cs --diff --diff-format=udiff --dry-run --verbose"

  tests:
    name: "Tests"

    runs-on: "ubuntu-latest"

    strategy:
      matrix:
        php-version:
          - "7.1"
          - "7.2"
          - "7.3"
          - "7.4"
          - "8.0"

        dependencies:
          - "locked"
          - "lowest"
          - "highest"

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2.3.4"

      - name: "Install PHP"
        uses: "shivammathur/setup-php@2.9.0"
        with:
          coverage: "none"
          php-version: "${{ matrix.php-version }}"

      - name: "Determine composer cache directory"
        id: "determine-composer-cache-directory"
        run: "echo \"::set-output name=directory::$(composer config cache-dir)\""

      - name: "Cache dependencies installed with composer"
        uses: "actions/cache@v2.1.3"
        with:
          path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
          key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
          restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

      - name: "Install lowest dependencies from composer.json"
        if: "matrix.dependencies == 'lowest'"
        run: "composer update --no-interaction --no-progress --no-suggest --prefer-lowest"

      - name: "Install locked dependencies from composer.lock"
        if: "matrix.dependencies == 'locked'"
        run: "composer install --no-interaction --no-progress --no-suggest"

      - name: "Install highest dependencies from composer.json"
        if: "matrix.dependencies == 'highest'"
        run: "composer update --no-interaction --no-progress --no-suggest"

      - name: "Run tests with phpunit/phpunit"
        run: "vendor/bin/phpunit --configuration=phpunit.xml.dist"
