stages:
  - build
  - test
  - analysis

variables:
  APP_ENV: staging
  SSH_PRIVATE_KEY: $SSH_PRIVATE_KEY
  XDEBUG_MODE: coverage

cache:
  paths:
    - ./vendor
    - ./composer.lock
    - ~/.composer/cache

build_image:
  stage: build
  image: docker:20.10
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker push $CI_REGISTRY_IMAGE:latest

.default_config: &default_config
  image: $CI_REGISTRY_IMAGE:latest
  before_script:
    - composer config -g cache-dir .composer-cache
    - HOST=ci composer install --prefer-dist --no-progress --no-interaction

unit:
  <<: *default_config
  stage: test
  script:
    - php vendor/phpunit/phpunit/phpunit --configuration phpunit.xml.dist --colors=never --no-interaction --testsuite=unit
    #- php vendor/phpunit/phpunit/phpunit --configuration phpunit.xml.dist --colors=never --no-interaction --no-coverage --testsuite=application

  artifacts:
    paths:
      - ./coverage/
    when: always
    reports:
      junit: ./coverage/junit-report.xml

  coverage: '/^\s*Lines:\s*\d+.\d+\%/'

#doctrine:
#  <<: *default_config
#  stage: test
#  script:
#    - php bin/console d:s:v

code_quality:
  <<: *default_config
  stage: analysis
  script:
    - php vendor/bin/phpstan analyse -c phpstan.neon.dist --error-format gitlab > phpstan.json || true    # Display code quality in MR
    - php vendor/bin/phpstan analyse -c phpstan.neon.dist
  artifacts:
    when: always
    reports:
      codequality: phpstan.json

code_style:
  <<: *default_config
  stage: analysis
  before_script:
    - composer config -g cache-dir .composer-cache
    - HOST=ci composer install --prefer-dist --no-progress --no-interaction
  script:
    - php vendor/bin/php-cs-fixer check --config=.php-cs-fixer.dist.php
