name: Tests

on:
  pull_request:
  push:
    branches: [ master, main ]
jobs:
  test:
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        php: [7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4]
        stability: [prefer-lowest, prefer-stable]

    name: PHP ${{ matrix.php }} - ${{ matrix.stability }} Test

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: curl, sodium
          coverage: none
          tools: composer:v2

      - name: Install dependencies
        run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

      - name: Run lint
        run: ./vendor/bin/phplint ./ --exclude=vendor

      - name: Run unit tests
        run: composer exec phpunit tests/unit

      - uses: actions/checkout@v3
        with:
          repository: pusher/public_actions
          path: .github/actions

      - uses: ./.github/actions/install-pusher-cli

      - name: Run acceptance tests
        env:
          PUSHERAPP_APPID: ${{ secrets.CI_APP_ID }}
          PUSHERAPP_AUTHKEY: ${{ secrets.CI_APP_KEY }}
          PUSHERAPP_SECRET: ${{ secrets.CI_APP_SECRET }}
          PUSHERAPP_CLUSTER: ${{ secrets.CI_APP_CLUSTER }}
          PUSHER_API_KEY: ${{ secrets.CI_API_KEY }}
        run: |
          echo "$PUSHER_API_KEY" | pusher login
          export TEST_CHANNEL="test-${RANDOM}-${RANDOM}"
          echo $TEST_CHANNEL
          pusher channels apps subscribe --app-id "$PUSHERAPP_APPID" --channel $TEST_CHANNEL &
          sleep 5
          composer exec phpunit tests/acceptance
