name: Integration Tests

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  integration-tests:


    name: Integration Tests
    runs-on: ubuntu-latest

    # We want integration tests only to run on the original repo and not on internal PRs
    if: (github.repository_owner == 'kreait' || github.repository_owner == 'beste-access') && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository)

    env:
      php-version: '8.1'
      extensions: ctype, dom, grpc, json, mbstring, openssl, xml, zip, zlib
      key: cache-integration-7x-${{ matrix.php }}-v1

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

      - name: Setup cache environment
        id: extcache
        uses: shivammathur/cache-extensions@v1
        with:
          php-version: ${{ env.php-version }}
          extensions: ${{ env.extensions }}
          key: ${{ env.key }}

      - name: Cache extensions
        uses: actions/cache@v3.0.11
        with:
          path: ${{ steps.extcache.outputs.dir }}
          key: ${{ steps.extcache.outputs.key }}
          restore-keys: ${{ steps.extcache.outputs.key }}

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ env.php-version }}
          extensions: ${{ env.extensions }}
          tools: composer, pecl
          coverage: xdebug

      - name: Setup problem matchers for PHP
        run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

      - uses: "ramsey/composer-install@v2"

      - name: Setup Problem Matchers for PHPUnit
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - name: Run Integration Tests
        env:
          GOOGLE_APPLICATION_CREDENTIALS: ${{secrets.GOOGLE_APPLICATION_CREDENTIALS}}
          TEST_FIREBASE_APP_ID: ${{secrets.TEST_FIREBASE_APP_ID}}
          TEST_FIREBASE_RTDB_URI: ${{secrets.TEST_FIREBASE_RTDB_URI}}
          TEST_FIREBASE_TENANT_ID: ${{secrets.TEST_FIREBASE_TENANT_ID}}
          TEST_REGISTRATION_TOKENS: ${{secrets.TEST_REGISTRATION_TOKENS}}
          XDEBUG_MODE: coverage
        run: vendor/bin/phpunit --testsuite=integration --coverage-clover=coverage.xml --log-junit=test-report.xml --testdox --verbose

      - name: Upload code coverage
        uses: codecov/codecov-action@v3
        with:
          flags: integration
        if: github.repository_owner == 'kreait'
