name: PHP Composer

on:
  push:
    branches: 
      - master
      - develop
    tags:
      - "*"
  pull_request:
    branches: 
      - master
      - develop

jobs:
  tests_unit:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        php-versions: ['7.3', '7.4', '8.0']
        phpunit-versions: ['8.5.15']
        include:
        - php-versions: '5.6'
          phpunit-versions: '5.7.27'
        - php-versions: '7.0'
          phpunit-versions: '6.5.14'
    
    name: Php Version ${{matrix.php-versions }} / php Unit ${{ matrix.phpunit-versions }}
    steps:
    - uses: actions/checkout@v2
    - name: Setup PHP with PECL extension
      uses: shivammathur/setup-php@v2
      with: 
        php-version: ${{ matrix.php-versions }}
        tools: phpunit:${{ matrix.phpunit-versions }}
    
    - name: php version
      run: php -v

    - name: Run test suite
      run: phpunit --bootstrap tests/config.php tests --configuration tests/phpunit.xml

  sonarcloud:
      runs-on: ubuntu-latest
      continue-on-error: true
      steps:
      - uses: actions/checkout@v3
        with:
          # Disabling shallow clone is recommended for improving relevancy of reporting
          fetch-depth: 0
      - name: SonarCloud Scan
        uses: sonarsource/sonarcloud-github-action@master
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        with:
          projectBaseDir: .
          args: >
            -Dsonar.organization=${{ vars.SONAR_ORGANIZATION }}
            -Dsonar.projectKey=github-payplug-payplug-php
            -Dsonar.sources=lib/
            -Dsonar.test.exclusions=tests/**
            -Dsonar.tests=tests/
            -Dsonar.verbose=true
  
  release:

    runs-on: ubuntu-latest
    needs: tests_unit
    if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
    steps:
      - uses: actions/checkout@master
        with:
          token: ${{ secrets.GITHUB_TOKEN}}
      - name: Fetch library version
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
      - name: Apply new version from tag
        run: |
          echo  $RELEASE_VERSION
          sed -i "s/LIBRARY_VERSION = '*.*'/LIBRARY_VERSION = '${{ env.RELEASE_VERSION }}'/" lib/Payplug/Core/Config.php
          git config user.email actions@github.com
          git config user.name "GitHub Actions"
          git add .
          git commit -m "[Automated Release Action]"
          git push origin HEAD:master

