variables:
  WPDESK_CI_VERSION: "1.2"
  MYSQL_ROOT_PASSWORD: "mysql"
  MYSQL_DATABASE: "wptest"
  MYSQL_USER: "mysql"
  MYSQL_PASSWORD: "mysql"
  MYSQL_INNODB_LOG_BUFFER_SIZE: "32M"
  PHP_ERROR_REPORTING: "E_ALL"
  COMPOSER_ALLOW_SUPERUSER: "1"
  GIT_STRATEGY: "fetch"
  CONTRIBUTORS: "wpdesk,dyszczo,grola,potreb"
  MINIMAL_WP_TESTED_UP: "5.3"
  MINIMAL_WC_TESTED_UP: "3.9"

stages:
  - prepare
  - tools
  - tests
  - pre-deploy
  - deploy

.template: &job-test-template
  cache:
    key: ${CI_PROJECT_NAME}-vendor
    paths:
      - vendor/
  stage: tests
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'
  artifacts:
    reports:
      junit: tmp_artifacts/report.xml

.template: &job-test-integration-template
  <<: *job-test-template
  dependencies:
    - prepare prefixed vendor
  services:
    - mysql:5.6
  before_script:
    - export DEPENDENT_PLUGINS_DIR=${CI_PROJECT_DIR}/tests/dependent_plugins
    - echo ${WPDESK_CI_VERSION}
    - ls -l
    - php --version
    - cat /tmp/wordpress-develop/src/wp-includes/version.php
    - cat /tmp/woocommerce/woocommerce.php
    - COMPOSER_MEMORY_LIMIT=-1 composer install --no-progress --prefer-dist
    - ln -s $CI_PROJECT_DIR /tmp/wordpress-develop/src/wp-content/plugins/$CI_PROJECT_NAME
  script:
    - vendor/bin/phpunit -d memory_limit=-1 --configuration phpunit-integration.xml --coverage-text --colors=never --log-junit tmp_artifacts/report.xml
  only:
    - master
  except:
    variables:
      - $DISABLE_COVERAGE

.template: &job-test-integration-template-fast
  <<: *job-test-integration-template
  script:
    - vendor/bin/phpunit  -d memory_limit=-1 --configuration phpunit-integration.xml --no-coverage --log-junit tmp_artifacts/report.xml
  except:
    - tags
  only:

.template: &job-test-unit-template
  <<: *job-test-template
  script:
    - echo ${WPDESK_CI_VERSION}
    - ls -l
    - php --version
    - cat /tmp/wordpress-develop/src/wp-includes/version.php
    - cat /tmp/woocommerce/woocommerce.php
    - COMPOSER_MEMORY_LIMIT=-1 composer install --no-progress --prefer-dist
    - vendor/bin/phpunit  -d memory_limit=-1 --configuration phpunit-unit.xml --coverage-text --colors=never --log-junit tmp_artifacts/report.xml
  only:
    - master
  except:
    variables:
      - $DISABLE_COVERAGE

.template: &job-test-unit-template-fast
  <<: *job-test-template
  script:
    - echo ${WPDESK_CI_VERSION}
    - ls -l
    - php --version
    - cat /tmp/wordpress-develop/src/wp-includes/version.php
    - cat /tmp/woocommerce/woocommerce.php
    - COMPOSER_MEMORY_LIMIT=-1 composer install --no-progress --prefer-dist
    - vendor/bin/phpunit  -d memory_limit=-1 --configuration phpunit-unit.xml --no-coverage --log-junit tmp_artifacts/report.xml

.template: &job-deploy-template
  image: wpdesknet/amazon-svn-deploy
  stage: deploy
  dependencies:
    - build to deploy
    - unit test lastest coverage
    - integration test lastest coverage
  retry: 2
  when: manual
  only:
    - tags

.template: &docs-generation
  image:
    name: wpdesknet/apigen
  stage: pre-deploy
  artifacts:
    expire_in: 1 day
    name: "docs"
    paths:
      - docs/
  script:
    - echo ${WPDESK_CI_VERSION}
    - ls -l
    - /app/vendor/bin/apigen generate
    - php /app/hooks-docs.php ${CI_PROJECT_DIR}

before_script:
  - cd ${CI_PROJECT_DIR}

prepare prefixed vendor:
  image: wpdesknet/phpunit-woocommerce:0-0
  stage: prepare
  artifacts:
    name: "vendor_prefixed"
    paths:
      - vendor_prefixed
      - lang
      - assets
      - tests/dependent_plugins
  script:
    - export DEPENDENT_PLUGINS_DIR=${CI_PROJECT_DIR}/tests/dependent_plugins
    - echo ${DEPENDENT_PLUGINS_DIR}
    - echo ${WPDESK_CI_VERSION}
    - php --version
    - ls -l
    - COMPOSER_MEMORY_LIMIT=-1 && php -d memory_limit=-1 /usr/local/bin/composer install
    - mkdir -p ${DEPENDENT_PLUGINS_DIR}
    - if [[ -f tests/integration/prepare.sh ]]; then sh tests/integration/prepare.sh; fi
    - if [[ -f tests/codeception/prepare.sh ]]; then sh tests/codeception/prepare.sh; fi

phpmetric metrics:
  stage: tools
  image: wpdesknet/phpunit-woocommerce:0-0
  allow_failure: true
  when: manual
  artifacts:
    when: always
    expire_in: 1 month
    name: "metrics"
    paths:
      - ${CI_PROJECT_DIR}/phpmetric
  script:
    - echo ${WPDESK_CI_VERSION}
    - composer require phpmetrics/phpmetrics
    - composer install --no-progress --prefer-dist
    - php ./vendor/bin/phpmetrics --report-html=phpmetric .

churn metrics:
  stage: tools
  image: wpdesknet/phpunit-woocommerce:0-0
  allow_failure: true
  when: manual
  script:
    - echo ${WPDESK_CI_VERSION}
    - composer require bmitch/churn-php
    - composer install --no-progress --prefer-dist
    - vendor/bin/churn run classes inc

#code style test:
# stage: tests
#  image: wpdesknet/phpunit-woocommerce:0-0
#  allow_failure: true
#  script:
#    - echo ${WPDESK_CI_VERSION}
#    - composer install --no-progress --prefer-dist
#    - vendor/bin/phpcs

unit test lastest:
  <<: *job-test-unit-template-fast
  image: wpdesknet/phpunit-woocommerce:0-0

integration test lastest:
  <<: *job-test-integration-template-fast
  image: wpdesknet/phpunit-woocommerce:0-0
  except:
    variables:
      - $DISABLE_INTEGRATION_TESTS

unit test lastest coverage:
  <<: *job-test-unit-template
  image: wpdesknet/phpunit-woocommerce:0-0

integration test lastest coverage:
  <<: *job-test-integration-template
  image: wpdesknet/phpunit-woocommerce:0-0
  except:
    variables:
      - $DISABLE_INTEGRATION_TESTS
      - $DISABLE_COVERAGE

integration test php7-1 wc-1:
  <<: *job-test-integration-template-fast
  image: wpdesknet/phpunit-woocommerce:1-1
  except:
    variables:
      - $DISABLE_INTEGRATION_TESTS

integration test php7 wc-2:
  <<: *job-test-integration-template-fast
  image: wpdesknet/phpunit-woocommerce:2-2
  except:
    variables:
      - $DISABLE_INTEGRATION_TESTS

integration test php-7 wc-3:
  <<: *job-test-integration-template-fast
  image: wpdesknet/phpunit-woocommerce:2-3
  except:
    variables:
      - $DISABLE_INTEGRATION_TESTS

integration test php5-6:
  <<: *job-test-integration-template-fast
  image: wpdesknet/phpunit-woocommerce:3-0
  except:
    variables:
      - $DISABLE_INTEGRATION_TESTS

integration test php5-5:
  <<: *job-test-integration-template-fast
  image: wpdesknet/phpunit-woocommerce:4-0
  except:
    variables:
      - $DISABLE_PHP_5_5
      - $DISABLE_INTEGRATION_TESTS

integration test current woocommerce:
  <<: *job-test-integration-template-fast
  image: wpdesknet/phpunit-woocommerce:0-0
  allow_failure: true
  before_script:
    - export DEPENDENT_PLUGINS_DIR=${CI_PROJECT_DIR}/tests/dependent_plugins
    - cd ${CI_PROJECT_DIR}
    - mkdir -p .tmp
    - cd .tmp
    - if [[ ! -d woocommerce ]]; then git clone https://github.com/woocommerce/woocommerce.git; fi
    - cd woocommerce
    - git pull
    - ln -s ${CI_PROJECT_DIR}/.tmp/woocommerce /tmp/woocommerce
    - cat /tmp/wordpress-develop/src/wp-includes/version.php
    - cat /tmp/woocommerce/woocommerce.php
    - cd ${CI_PROJECT_DIR}
    - COMPOSER_MEMORY_LIMIT=-1 composer install --no-progress --prefer-dist
    - ln -s $CI_PROJECT_DIR /tmp/wordpress-develop/src/wp-content/plugins/$CI_PROJECT_NAME
  except:
    variables:
      - $DISABLE_INTEGRATION_TESTS

apigen docs:
  <<: *docs-generation
  allow_failure: true
  only:
    - tags

apigen docs refresh:
  <<: *docs-generation
  when: manual

pages:
  stage: deploy
  dependencies:
    - apigen docs
  script:
    - rm -rf public
    - mv docs/ public/
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Dokumentacja projektu ${CI_PROJECT_NAME} w wersji ${CI_COMMIT_REF_NAME} umieszczona w <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}/pages|pages> \", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  artifacts:
    expire_in: 1 day
    paths:
      - public
  only:
    - tags

build to deploy:
  image: wpdesknet/phpunit-woocommerce:3-0
  stage: pre-deploy
  dependencies:
    - prepare prefixed vendor
  artifacts:
    name: "production release"
    paths:
      - release
      - release.zip
  script:
    - echo ${WPDESK_CI_VERSION}
    - php --version
    - ls -l
    - /tmp/set_version.sh ${CI_COMMIT_REF_NAME}
    - rm -rf ${CI_PROJECT_DIR}/release ${CI_PROJECT_DIR}/release.zip /tmp/release
    - mkdir /tmp/release
    - mkdir -p ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - cp -rf ${CI_PROJECT_DIR}/* /tmp/release
    - cp -rf /tmp/release/* ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - cd ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - COMPOSER_MEMORY_LIMIT=-1 && php -d memory_limit=-1 /usr/local/bin/composer install --no-dev --no-progress --optimize-autoloader --prefer-dist
    - rm -rf build-coverage release tests docs .git .editorconfig .gitignore .gitlab-ci.yml apigen.neon phpunit.xml acceptance test_soap.php .gitlab
    - rm -rf composer.json phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml composer.phar wp-cli.phar functional tmp_artifacts .tmp tools codeception.dist.yml .env.testing wp-install.sh
    - rm -rf phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml composer.phar wp-cli.phar functional tmp_artifacts scoper.inc.php phpstan.dist

    - find ./vendor -type d -name "tests" -exec rm -rf {} \; 2>/dev/null || (exit 0)
    - find ./vendor -name "LICENSE" -delete
    - find ./vendor -name "README.md" -delete
    - find ./vendor -name ".gitlab-ci.yml" -delete
    - cd ../
    - zip -r -q ../release.zip ./
  only:
    - tags
  except:
    variables:
      - $IS_LIBRARY

deploy to shop:
  <<: *job-deploy-template
  script:
    - echo ${WPDESK_CI_VERSION}
    - /tmp/deploy_shop.sh ${CI_PROJECT_NAME} release.zip ${CI_PROJECT_NAME}.zip
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do sklepu w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  environment:
    name: wpdesk shop
    url: https://wpdeskplugin.s3.amazonaws.com/${CI_PROJECT_NAME}.zip
  except:
    variables:
      - $IS_LIBRARY

deploy to demo:
  <<: *job-deploy-template
  script:
    - echo ${WPDESK_CI_VERSION}
    - /tmp/deploy_demo.sh release/${CI_PROJECT_NAME} ${CI_PROJECT_NAME}
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do demo w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  environment:
    name: wpdesk demo
    url: https://demo.wpdesk.org
  except:
    variables:
      - $IS_LIBRARY

deploy to repository:
  <<: *job-deploy-template
  cache:
    key: ${CI_PROJECT_NAME}-svn
    paths:
      - svn-repository
  script:
    - echo ${WPDESK_CI_VERSION}
    - mkdir -p ${CI_PROJECT_DIR}/svn-repository
    - rm -f ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}/lang/*.po ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}/lang/*.mo
    - /tmp/deploy_repository.sh ${CI_PROJECT_NAME} ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME} ${CI_PROJECT_DIR}/svn-repository/${CI_PROJECT_NAME}
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do repozytorium WP w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
  environment:
    name: wordpress repository
    url: https://downloads.wordpress.org/plugin/${CI_PROJECT_NAME}.${CI_COMMIT_REF_NAME}.zip
  except:
    variables:
      - $IS_LIBRARY


library:
  image: wpdesknet/phpunit-woocommerce:3-0
  stage: deploy
  artifacts:
    expire_in: 1 year
    name: "${CI_PROJECT_NAME}-${CI_COMMIT_REF_NAME}"
    paths:
      - wp-desk
  script:
    - rm -rf ${CI_PROJECT_DIR}/wp-desk
    - mkdir -p /tmp/wp-desk
    - mkdir -p ${CI_PROJECT_DIR}/wp-desk/${CI_PROJECT_NAME}
    - cp -rf ${CI_PROJECT_DIR}/* /tmp/wp-desk
    - cp -rf /tmp/wp-desk/* ${CI_PROJECT_DIR}/wp-desk/${CI_PROJECT_NAME}
    - cd ${CI_PROJECT_DIR}/wp-desk/${CI_PROJECT_NAME}
    - COMPOSER_MEMORY_LIMIT=-1 && php -d memory_limit=-1 /usr/local/bin/composer install --no-dev --no-progress --optimize-autoloader --prefer-dist
    - rm -rf build-coverage wp-desk release tests docs .git .editorconfig .gitignore .gitlab-ci.yml apigen.neon phpunit.xml acceptance test_soap.php .gitlab
    - rm -rf composer.json composer.lock phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml composer.phar wp-cli.phar functional tmp_artifacts .tmp tools
  only:
    variables:
      - $IS_LIBRARY

.template: &job-codeception-test
  image: wpdesknet/wordpresscli:22
  cache:
    key: ${CI_PROJECT_NAME}-vendor
    paths:
      - vendor/
  variables:
    WP_CLI_CACHE_DIR: /cache/wp-cli
    APACHE_DOCUMENT_ROOT: ${CI_PROJECT_DIR}/tests/wordpress
    CI_DEBUG_SERVICES: trace
    SCREEN_WIDTH: 1200
    SCREEN_HEIGHT: 1600
    JAVA_OPTS: -Xmx1024m
  services:
    - name: mysql:5.6
      alias: mysqltests
    - name: wpdesknet/wordpress:49
      alias: wootests
    - name: selenium/standalone-chrome:latest
      alias: chrome
  artifacts:
    when: always
    expire_in: 1 month
    name: "acceptance logs"
    paths:
      - tests/codeception/tests/_output
      - ${APACHE_DOCUMENT_ROOT}
    reports:
      junit: tests/codeception/tests/_output/report.xml
  stage: tests
  except:
    variables:
      - $DISABLE_CODECEPTION
  allow_failure: false
  before_script:
    - export DEPENDENT_PLUGINS_DIR=${CI_PROJECT_DIR}/tests/dependent_plugins
    - mkdir -p /cache/wp-cli
    - export MYSQL_IP=$(awk '/^[[:space:]]*($|#)/{next} /mysqltests/{print $1; exit}' /etc/hosts)
    - export WOOTESTS_IP=$(awk '/^[[:space:]]*($|#)/{next} /wootests/{print $1; exit}' /etc/hosts)
    - cd ${CI_PROJECT_DIR}
    - pwd
    - composer install --prefer-dist --no-progress --no-dev
    - mkdir -p /tmp/project
    - cp -r ${CI_PROJECT_DIR}/. /tmp/project
    - composer install --prefer-dist --no-progress
    - mkdir -p ${APACHE_DOCUMENT_ROOT}
    - cd ${APACHE_DOCUMENT_ROOT}
    - wp core download ${WORDPRESS_CLI_PARAMETERS} --allow-root
    - wp config create --dbhost=${MYSQL_IP} --dbname=wptest --dbuser=mysql --dbpass=mysql --allow-root
    - wp config set WP_DEBUG true --raw --type=constant --allow-root
    - cp -r /tmp/project/. ${APACHE_DOCUMENT_ROOT}/wp-content/plugins/${CI_PROJECT_NAME}
    - chmod a+x ${CI_PROJECT_DIR}/tests/codeception/bootstrap.sh
    - cd ${CI_PROJECT_DIR}
    - . ./tests/codeception/bootstrap.sh
    - rm ${APACHE_DOCUMENT_ROOT}/wp-content/debug.log
    - chmod o+w ${APACHE_DOCUMENT_ROOT}/wp-content
    - chmod g+w ${APACHE_DOCUMENT_ROOT}/wp-content
    - mkdir -p ${CI_PROJECT_DIR}/tests/codeception/tests/_output
    - mkdir -p ${CI_PROJECT_DIR}/tests/codeception/tests/acceptance
    - mkdir -p ${CI_PROJECT_DIR}/tests/codeception/tests/functional
    - mkdir -p ${CI_PROJECT_DIR}/tests/codeception/tests/unit
    - chmod -R a+w ${APACHE_DOCUMENT_ROOT}/wp-content/uploads
    - echo "End before"
  script:
    - vendor/bin/codecept run --steps --xml --html --debug

codeception test:
  <<: *job-codeception-test
  script:
    - vendor/bin/codecept run --steps --xml --html --debug

codeception test latest woocommerce:
  <<: *job-codeception-test
  variables:
    WP_CLI_CACHE_DIR: /cache/wp-cli
    APACHE_DOCUMENT_ROOT: ${CI_PROJECT_DIR}/tests/wordpress
    CI_DEBUG_SERVICES: trace
    SCREEN_WIDTH: 1200
    SCREEN_HEIGHT: 1600
    WOOCOMMERCE_VERSION: github
  allow_failure: true
  script:
    - vendor/bin/codecept run --steps --xml --html --debug

