sudo: required
dist: trusty
language: php
php:
  - 7.1
  - 7.2
  - 7.3

addons:
  apt:
    packages:
    - mysql-server-5.6
    - mysql-client-core-5.6
    - mysql-client-5.6
    - php5-pgsql
    - libxml2-utils

services:
  - docker
  - postgresql

env:
  global:
  - CORE_BRANCH=master
  - PHP_COVERAGE=FALSE
  - TEST_SUITE=TEST-PHP
  matrix:
  - DB=mysql TEST_SUITE=LINT-PHP
  - DB=mysql TEST_SUITE=TEST-PHP

matrix:
  include:
    - php: 7.3
      env: "DB=mysql TEST_SUITE=TEST-JS"
    - php: 7.3
      env: "DB=mysql TEST_SUITE=PACKAGE"
    - php: 7.1
      env: "DB=mysql PHP_COVERAGE=TRUE"
    - php: 7.3
      env: "DB=sqlite CORE_BRANCH=stable16"
    - php: 7.3
      env: "DB=pgsql CORE_BRANCH=v16.0.0RC1"
  fast_finish: true

branches:
  only:
  - master
  - "/^stable\\d+(\\.\\d+)?$/"
  - /^v\d++(\.\d+)?+(\.\d+)?+(\.\d+)?$/
  - refactor/vue

cache:
  directories:
  - "$HOME/.composer/cache/files"
  - "$HOME/.npm"

before_install:
  - php --info

  # Install Krankerl
  - wget https://github.com/ChristophWurst/krankerl/releases/download/v0.10.1/krankerl_0.10.1_amd64.deb
  - sudo dpkg -i krankerl_0.10.1_amd64.deb

  # Set up DB
  - if [[ "$DB" == 'pgsql' ]]; then createuser -U travis -s oc_autotest; fi
  - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e 'create database oc_autotest;'; fi
  - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e "CREATE USER 'oc_autotest'@'localhost' IDENTIFIED BY '';"; fi
  - if [[ "$DB" == 'mysql' ]]; then mysql -u root -e "GRANT ALL ON oc_autotest.* TO 'oc_autotest'@'localhost';"; fi

  - composer self-update
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then make install-composer-deps-dev; fi"
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then make start-imap-docker; fi"
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then make start-smtp-docker; fi"
  - cd ..
  - git clone https://github.com/nextcloud/server.git --recursive --depth 1 -b $CORE_BRANCH core
  - mv mail core/apps/

before_script:
  # Set up core
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then php -f core/occ maintenance:install --database-name oc_autotest --database-user oc_autotest --admin-user admin --admin-pass admin --database $DB --database-pass=''; fi"

  # Set up app
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then php -f core/occ app:enable mail; fi"
  # Enable app twice to check occ errors of registered commands
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then php -f core/occ app:enable mail; fi"
  - cd core/apps/mail
  - sh -c "if [ '$TEST_SUITE' = 'TEST-JS' ]; then npm install -g npm@latest; fi"
  - sh -c "if [ '$TEST_SUITE' = 'TEST-JS' ]; then make dev-setup; fi"

  # Prepare IMAP test docker
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then make add-imap-account; fi"

  # XDebug is only needed if we report coverage -> speeds up other builds
  - if [[ "$PHP_COVERAGE" = "FALSE" ]]; then phpenv config-rm xdebug.ini; fi

  # Print running docker images, just to verify they did not crash
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then docker ps; fi"

script:
  # Check info.xml schema validity
  - wget https://apps.nextcloud.com/schema/apps/info.xsd
  - xmllint appinfo/info.xml --schema info.xsd --noout
  - rm info.xsd

  # Check PHP syntax errors
  - sh -c "if [ '$TEST_SUITE' = 'LINT-PHP' ]; then composer run lint; fi"

  # Run server's app code checker
  - sh -c "if [ '$TEST_SUITE' = 'LINT-PHP' ]; then php ../../occ app:check-code mail; fi"

  # Run JS tests
  - sh -c "if [ '$TEST_SUITE' = 'TEST-JS' ]; then npm test; fi"
  # - sh -c "if [ '$TEST_JS' = 'TRUE' ]; then cat ./coverage/*/lcov.info | ./node_modules/coveralls/bin/coveralls.js; fi"

  # Run JS lint
  - sh -c "if [ '$TEST_SUITE' = 'TEST-JS' ]; then npm run lint; fi"

  # Run PHP tests
  - sh -c "if [ '$TEST_SUITE' = 'TEST-PHP' ]; then composer run test; fi"
  - if [[ "$PHP_COVERAGE" = "TRUE" ]]; then wget https://scrutinizer-ci.com/ocular.phar;
    fi
  - if [[ "$PHP_COVERAGE" = "TRUE" ]]; then php ocular.phar code-coverage:upload --format=php-clover
    tests/clover.xml; fi

  # Test packaging
  - if [[ "$TEST_SUITE" = "PACKAGE" ]]; then make appstore; fi