name: Code Style, Test and Build process

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        operating-system: [ 'ubuntu-latest' ]
        php-versions: [ '7.4', '8.0', '8.1', 'nightly' ]
        phpunit-versions: [ 'latest' ]

    steps:
      - uses: actions/checkout@v3

      - name: Setup PHP with PECL extension
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, intl
          ini-values: post_max_size=256M, max_execution_time=180
          coverage: xdebug
          tools: phpcs, phpunit:${{ matrix.phpunit-versions }}

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

      - name: Cache Composer packages
        id: composer-cache
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-

      - name: Install dependencies
        run: composer install --prefer-dist --no-progress

      - name: Run PHP Code Sniffer
        run: composer run-script codeStyle

      - name: Run test suite
        run: composer run-script test
