test:unit:
  image: gitlab.wpdesk.dev:5050/wpdesk/docker-tests/php-box:8.3.2
  variables:
    PHPUNIT_OPTIONS: "--no-coverage"
  tags:
    - unit
  stage: tests
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - if: $IS_NPM_LIBRARY
      when: never
    - if: $CI_COMMIT_BRANCH == "devel"
      exists:
        - phpunit-unit.xml
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_TAG
      exists:
        - phpunit-unit.xml
      variables:
        PHPUNIT_OPTIONS: "--coverage-text --coverage-html tmp_artifacts/unit/coverage --colors=never"
    - exists:
        - phpunit-unit.xml
      when: manual
      allow_failure: true
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'
  artifacts:
    paths:
      - tmp_artifacts/unit
    reports:
      junit: tmp_artifacts/unit/report.xml
    expire_in: 3 mos
  interruptible: true
  before_script:
    - echo ${WPDESK_CI_VERSION}
    - php --version
    - echo "xdebug.mode=coverage" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
  script:
    - vendor/bin/phpunit -d memory_limit=-1 --configuration phpunit-unit.xml --log-junit tmp_artifacts/unit/report.xml $PHPUNIT_OPTIONS
  after_script:
    - 'if [[ ${REPORTPORTAL_AUTHORIZATION} && ${REPORTPORTAL_PROJECT} ]]; then curl -X POST "http://195.201.225.204:8080/api/v1/${REPORTPORTAL_PROJECT}/launch/import" -H "accept: */*" -H "Content-Type: multipart/form-data" -H "${REPORTPORTAL_AUTHORIZATION}" -F "file=file=@tmp_artifacts/unit/report.xml;type=text/xml;type=text/xml;filename=${CI_PROJECT_NAME}-${CI_COMMIT_REF_SLUG}-unit.xml"; fi'