jobs:
- job: php
  displayName: PHP
  pool:
    vmImage: 'Ubuntu-16.04'
  strategy:
    matrix:
      '7.1':
        phpVersion: 7.1
      '7.2':
        phpVersion: 7.2
  variables:
    APP_ENV: testing
    DB_CONNECTION: mysql
    DB_DATABASE: test
    DB_USERNAME: root
    DB_PASSWORD: root

  steps:
  - script: |
      sudo update-alternatives --set php /usr/bin/php$(phpVersion)
      sudo update-alternatives --set phar /usr/bin/phar$(phpVersion)
      sudo update-alternatives --set phpdbg /usr/bin/phpdbg$(phpVersion)
      sudo update-alternatives --set php-cgi /usr/bin/php-cgi$(phpVersion)
      sudo update-alternatives --set phar.phar /usr/bin/phar.phar$(phpVersion)
      php -version
    displayName: 'Use PHP version $(phpVersion)'

  - task: DownloadPipelineArtifact@0
    inputs:
      artifactName: php vendor
      targetPath: vendor

  - script: composer install --no-interaction --prefer-dist
    displayName: 'composer install'

  - script: |
      cp .env.testing .env
      php artisan key:random
      php artisan salt:random
      mysql -uroot -proot -e 'CREATE DATABASE IF NOT EXISTS test;'
    displayName: 'Prepare testing environment'

  - script: ./vendor/bin/phpunit --log-junit junit.xml
    displayName: Test

  - task: PublishTestResults@2
    condition: succeededOrFailed()
    inputs:
      testRunner: JUnit
      testResultsFiles: './junit.xml'

  - task: PublishPipelineArtifact@0
    inputs:
      artifactName: php vendor
      targetPath: vendor

- job: javascript
  displayName: JavaScript
  pool:
    vmImage: 'Ubuntu-16.04'
  steps:
  - task: DownloadPipelineArtifact@0
    inputs:
      artifactName: node_modules
      targetPath: node_modules

  - script: yarn
    displayName: Install dependencies

  - script: |
      yarn lint
      yarn test --ci --reporters=default --reporters=jest-junit --coverage
    displayName: 'Run Test'

  - task: PublishTestResults@2
    condition: succeededOrFailed()
    inputs:
      testRunner: JUnit
      testResultsFiles: './junit.xml'

  - task: PublishPipelineArtifact@0
    inputs:
      artifactName: node_modules
      targetPath: node_modules

- job: build
  displayName: Build & Compile
  condition: ne(variables['Build.Reason'], 'PullRequest')
  dependsOn:
    - php
    - javascript
  pool:
    vmImage: 'Ubuntu-16.04'
  steps:
    - script: yarn
      displayName: Install dependencies
    - script: mkdir dist
    - script: yarn release dist/blessing-skin-server.zip
      displayName: Generate package
    - task: PublishPipelineArtifact@0
      inputs:
        artifactName: 'Package file'
        targetPath: dist