# This script is used by Travis CI to run automatically Continuous test integration
# from Dolibarr GitHub repository.
# For syntax, see http://about.travis-ci.org/docs/user/languages/php/

# We use dist: trusty to have php 5.4+ available
dist: trusty
sudo: required

language: php

# Start on every boot
services:
- memcached

addons:
  mariadb: '10.0'
  postgresql: '9.3'
  apt:
    sources:
    # To use the last version of pgloader, we add repo of postgresql
    - postgresql
    - sourceline: 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main'
    - key_url: 'https://www.postgresql.org/media/keys/ACCC4CF8.asc'
    packages:
    # We need a webserver to test the webservices
    # Let's install Apache with.
    - apache2
    # mod_php is not supported by Travis. Add fcgi. We install FPM later on.
    - libapache2-mod-fastcgi
    # We need pgloader for import mysql database into pgsql
    - pgloader

php:
- '5.4'
- '5.5'
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- nightly

env:
  global:
  # Set to true for very verbose output
  - DEBUG=false
  matrix:
  # MariaDB overrides MySQL installation so it's not possible to test both yet
  #- DB=mysql
  - DB=mariadb
  - DB=postgresql
  # TODO
  #- DB=sqlite
  # See https://docs.travis-ci.com/user/languages/php/#Apache-%2B-PHP
  #- WS=apache
  # See https://github.com/DracoBlue/travis-ci-nginx-php-fpm-test
  #- WS=nginx

matrix:
  fast_finish: true
  allow_failures:
  - php: nightly
  # We exclude some combinations not usefull to save Travis CPU
  exclude:
  - php: '5.5'
    env: DB=mariadb
  - php: '5.6'
    env: DB=mariadb
  - php: '7.0'
    env: DB=mariadb
  - php: '7.1'
    env: DB=mariadb
  - php: '7.2'
    env: DB=mariadb
  - php: '5.5'
    env: DB=postgresql
  - php: '5.6'
    env: DB=postgresql
  - php: '7.0'
    env: DB=postgresql
  - php: '7.1'
    env: DB=postgresql
  - php: '7.2'
    env: DB=postgresql
  - php: nightly
    env: DB=postgresql

notifications:
  email:
    on_success: never # [always|never|change] default: change
    on_failure: never # [always|never|change] default: always
  irc:
    channels:
    - "chat.freenode.net#dolibarr"
    on_success: change
    on_failure: always
    use_notice: true

before_install:
- |
  echo "Disabling Xdebug for composer"
  export PHP_VERSION_NAME=$(phpenv version-name)
  cp ~/.phpenv/versions/$PHP_VERSION_NAME/etc/conf.d/xdebug.ini /tmp/xdebug.ini
  phpenv config-rm xdebug.ini
  echo

- |
  if [ "$DB" = 'postgresql' ]; then
    echo "Check pgloader version"
    pgloader --version
    echo
  fi

install:
- |
  echo "Updating Composer"
  rm $TRAVIS_BUILD_DIR/composer.json
  rm $TRAVIS_BUILD_DIR/composer.lock
  composer self-update
  composer -n init
  composer -n config vendor-dir htdocs/includes
  echo

- |
  echo "Installing Parallel Lint"
  composer -n require jakub-onderka/php-parallel-lint ^0
  composer -n require jakub-onderka/php-console-highlighter ^0
  echo

- |
  echo "Installing PHP Unit"
  if [ "$TRAVIS_PHP_VERSION" = '5.4' ] || [ "$TRAVIS_PHP_VERSION" = '5.5' ]; then
    composer -n require phpunit/phpunit ^4
  fi
  if [ "$TRAVIS_PHP_VERSION" = '5.6' ] || [ "$TRAVIS_PHP_VERSION" = '7.0' ] || [ "$TRAVIS_PHP_VERSION" = '7.1' ]; then
    composer -n require phpunit/phpunit ^5
  fi
  if [ "$TRAVIS_PHP_VERSION" = '7.2' ] || [ "$TRAVIS_PHP_VERSION" = '7.3' ] || [ "$TRAVIS_PHP_VERSION" = 'nightly' ]; then
    composer -n require phpunit/phpunit ^5
  fi
  echo

- |
  echo "Installing PHP CodeSniffer"
  composer -n require squizlabs/php_codesniffer ^3
  echo

- |
  echo "Adding path of binaries tools installed by composer to the PATH"
  export PATH="$TRAVIS_BUILD_DIR/htdocs/includes/bin:$PATH"
  echo



before_script:
  - |
    echo Start travis
    echo Current dir is `pwd`
    echo Home dir is `echo ~`
    echo TRAVIS_BUILD_DIR is $TRAVIS_BUILD_DIR

  - |
    echo "Re-enabling Xdebug for PHP CodeSniffer and PHP Unit"
    phpenv config-add /tmp/xdebug.ini
    echo

  - |
    echo "Setting up PHP"
    echo
    echo "Set timezone"
    echo 'date.timezone = "Europe/Paris"' >> ~/.phpenv/versions/$PHP_VERSION_NAME/etc/php.ini
    if [ "$TRAVIS_PHP_VERSION" = '5.4' ]; then
      # Documentation says it should be available for all PHP versions but it's not for 5.5 and 5.6, 7.0, 7.1, 7.2 and nightly!
      echo
      echo "Enabling Memcached for PHP <= 5.4"
      echo 'extension = memcached.so' >> ~/.phpenv/versions/$PHP_VERSION_NAME/etc/php.ini
    fi
    phpenv rehash
    echo

  - |
    echo "Versions information"
    # Check PHP
    echo "PHP version"
    php -i | head -
    # Check PHP CodeSniffer installation
    echo "PHPCS version"
    which phpcs
    phpcs --version | head -
    phpcs -i | head -
    # Check PHPUnit installation
    echo "PHPUnit version"
    which phpunit
    phpunit --version | head -
    # Check Apache version
    echo "Apache version"
    apache2 -v | head -
    # Check MariaDb
    echo "MariaDb version"
    mysql --version | head -
    mysql -e "SELECT VERSION();"  | head -
    echo

  - |
    echo "Setting up database"
    if [ "$DB" = 'mysql' ] || [ "$DB" = 'mariadb' ] || [ "$DB" = 'postgresql' ]; then
      echo "MySQL"
      mysql -e 'DROP DATABASE IF EXISTS travis;'
      mysql -e 'CREATE DATABASE IF NOT EXISTS travis;'
      mysql -e 'GRANT ALL PRIVILEGES ON travis.* TO travis@127.0.0.1;'
      mysql -e 'FLUSH PRIVILEGES;'
      mysql -D travis < dev/initdemo/mysqldump_dolibarr_3.5.0.sql
    fi
    if [ "$DB" = 'postgresql' ]; then
      #pgloader mysql://root:pass@127.0.0.1/dolibarr_9 postgresql://dolibarrowner:dolibarrownerpass@127.0.0.1/dolibarr_dev
      echo pgloader mysql://root@127.0.0.1/travis postgresql:///travis
      pgloader mysql://root@127.0.0.1/travis postgresql:///travis
      echo 'ALTER SEQUENCE llx_accountingaccount_rowid_seq RENAME TO llx_accounting_account_rowid_seq' | psql travis
      echo 'ALTER SEQUENCE llx_accounting_account_rowid_seq RESTART WITH 1000001;' | psql travis
      #echo 'select * from INFORMATION_SCHEMA.COLUMNS where table_name = 'llx_accountingaccount' | psql travis
      #echo 'select * from information_schema.table_constraints;' | psql travis
      #echo 'ALTER TABLE "llx_accounting_account" DROP CONSTRAINT "idx_16390_primary"' | psql travis
    fi
    # TODO: SQLite
    echo

  - |
    export CONF_FILE=htdocs/conf/conf.php
    echo "Setting up Dolibarr $CONF_FILE"
    echo '<?php' > $CONF_FILE
    echo '$'dolibarr_main_url_root=\'http://127.0.0.1\'';' >> $CONF_FILE
    echo '$'dolibarr_main_document_root=\'$TRAVIS_BUILD_DIR/htdocs\'';' >> $CONF_FILE
    echo '$'dolibarr_main_data_root=\'$TRAVIS_BUILD_DIR/documents\'';' >> $CONF_FILE
    echo '$'dolibarr_main_db_host=\'127.0.0.1\'';' >> $CONF_FILE
    echo '$'dolibarr_main_db_name=\'travis\'';' >> $CONF_FILE
    echo '$'dolibarr_main_db_user=\'travis\'';' >> $CONF_FILE
    if [ "$DB" = 'mysql' ] || [ "$DB" = 'mariadb' ]; then
      echo '$'dolibarr_main_db_type=\'mysqli\'';' >> $CONF_FILE
      echo '$'dolibarr_main_db_port=\'3306\'';' >> $CONF_FILE
    fi
    if [ "$DB" = 'postgresql' ]; then
      echo '$'dolibarr_main_db_type=\'pgsql\'';' >> $CONF_FILE
      echo '$'dolibarr_main_db_port=\'5432\'';' >> $CONF_FILE
    fi
    # TODO: SQLite
    echo '$'dolibarr_main_authentication=\'dolibarr\'';' >> $CONF_FILE
    cat $CONF_FILE
    echo

  - |
    echo "Create documents directory and set permissions"
    # and admin/temp subdirectory needed for unit tests
    mkdir -p $TRAVIS_BUILD_DIR/documents/admin/temp
    sudo chmod -R a+rwx $TRAVIS_BUILD_DIR/documents
    echo "***** First line of dolibarr.log" > $TRAVIS_BUILD_DIR/documents/dolibarr.log
    echo


  - echo "Setting up Apache + FPM"
  - sudo apt-get update
  - sudo apt-get install apache2 libapache2-mod-fastcgi
  # enable php-fpm
  - sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
  - |
    if [ "$TRAVIS_PHP_VERSION" = '7.0' ] || [ "$TRAVIS_PHP_VERSION" = '7.1' ] || [ "$TRAVIS_PHP_VERSION" = '7.2' ] || [ "$TRAVIS_PHP_VERSION" = '7.3' ] || [ "$TRAVIS_PHP_VERSION" = 'nightly' ]; then
      # Copy the included pool
      sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/www.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.d/www.conf
    fi
  - sudo a2enmod rewrite actions fastcgi alias
  - echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
  - sudo sed -i -e "s,www-data,travis,g" /etc/apache2/envvars
  - sudo chown -R travis:travis /var/lib/apache2/fastcgi
  - ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm
  # configure apache virtual hosts for precise
  #- sudo sed -e "s?%TRAVIS_BUILD_DIR%?$(pwd)?g" --in-place /etc/apache2/sites-available/default
  #- sudo cat /etc/apache2/sites-available/default
  # configure apache virtual hosts for trusty
  - sudo cp -f build/travis-ci/apache.conf /etc/apache2/sites-available/000-default.conf
  - sudo sed -e "s?%TRAVIS_BUILD_DIR%?$(pwd)?g" --in-place /etc/apache2/sites-available/000-default.conf
  - sudo cat /etc/apache2/sites-available/000-default.conf
  - sudo service apache2 restart



script:
- |
  echo "Checking webserver availability by a wget -O - http://127.0.0.1"
  # Ensure we stop on error with set -e
  set +e
  # The wget should return a page with line '<meta name="generator" content="Dolibarr installer">
  wget -O - http://127.0.0.1 > test.html
  head test.html
  sudo cat /var/log/apache2/travis_error_log
  set +e
  echo

- |
  echo "Checking PHP syntax errors"
  # Ensure we catch errors
  set -e
  #parallel-lint --exclude htdocs/includes --blame .
  parallel-lint --exclude htdocs/includes/sabre --exclude htdocs/includes/phpoffice/phpexcel/Classes/PHPExcel/Shared --exclude htdocs/includes/sebastian --exclude htdocs/includes/squizlabs/php_codesniffer/tests --exclude htdocs/includes/jakub-onderka/php-parallel-lint/tests --exclude htdocs/includes/mike42/escpos-php/example --exclude htdocs/includes/phpunit/php-token-stream/tests --exclude htdocs/includes/composer/autoload_static.php --blame .
  set +e
  echo

- |
  echo "Checking coding style"
  # Ensure we catch errors
  set -e
  # Exclusions are defined in the ruleset.xml file
  #phpcs -s -n -p -d memory_limit=-1 --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 .
  phpcs -s -p -d memory_limit=-1 --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --runtime-set ignore_warnings_on_exit true .
  set +e
  echo

# TODO: Check Javascript (jshint?)

# TODO: Check CSS (csslint?)

- |
  echo "Upgrading Dolibarr"
  # Ensure we catch errors. Set this to +e if you want to go to the end to see log files.
  set +e
  cd htdocs/install
  php upgrade.php 3.5.0 3.6.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade350360.log
  php upgrade2.php 3.5.0 3.6.0 > $TRAVIS_BUILD_DIR/upgrade350360-2.log
  php step5.php 3.5.0 3.6.0 > $TRAVIS_BUILD_DIR/upgrade350360-3.log
  php upgrade.php 3.6.0 3.7.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade360370.log
  php upgrade2.php 3.6.0 3.7.0 > $TRAVIS_BUILD_DIR/upgrade360370-2.log
  php step5.php 3.6.0 3.7.0 > $TRAVIS_BUILD_DIR/upgrade360370-3.log
  php upgrade.php 3.7.0 3.8.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade370380.log
  php upgrade2.php 3.7.0 3.8.0 > $TRAVIS_BUILD_DIR/upgrade370380-2.log
  php step5.php 3.7.0 3.8.0 > $TRAVIS_BUILD_DIR/upgrade370380-3.log
  php upgrade.php 3.8.0 3.9.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade380390.log
  php upgrade2.php 3.8.0 3.9.0 > $TRAVIS_BUILD_DIR/upgrade380390-2.log
  php step5.php 3.8.0 3.9.0 > $TRAVIS_BUILD_DIR/upgrade380390-3.log
  php upgrade.php 3.9.0 4.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade390400.log
  php upgrade2.php 3.9.0 4.0.0 > $TRAVIS_BUILD_DIR/upgrade390400-2.log
  php step5.php 3.9.0 4.0.0 > $TRAVIS_BUILD_DIR/upgrade390400-3.log
  php upgrade.php 4.0.0 5.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade400500.log
  php upgrade2.php 4.0.0 5.0.0 > $TRAVIS_BUILD_DIR/upgrade400500-2.log
  php step5.php 4.0.0 5.0.0 > $TRAVIS_BUILD_DIR/upgrade400500-3.log
  php upgrade.php 5.0.0 6.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade500600.log
  php upgrade2.php 5.0.0 6.0.0 > $TRAVIS_BUILD_DIR/upgrade500600-2.log
  php step5.php 5.0.0 6.0.0 > $TRAVIS_BUILD_DIR/upgrade500600-3.log
  php upgrade.php 6.0.0 7.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade600700.log
  php upgrade2.php 6.0.0 7.0.0 > $TRAVIS_BUILD_DIR/upgrade600700-2.log
  php step5.php 6.0.0 7.0.0 > $TRAVIS_BUILD_DIR/upgrade600700-3.log
  php upgrade.php 7.0.0 8.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade700800.log
  php upgrade2.php 7.0.0 8.0.0 > $TRAVIS_BUILD_DIR/upgrade700800-2.log
  php step5.php 7.0.0 8.0.0 > $TRAVIS_BUILD_DIR/upgrade700800-3.log
  php upgrade.php 8.0.0 9.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade800900.log
  php upgrade2.php 8.0.0 9.0.0 > $TRAVIS_BUILD_DIR/upgrade800900-2.log
  php step5.php 8.0.0 9.0.0 > $TRAVIS_BUILD_DIR/upgrade800900-3.log
  # Enable modules not enabled into original dump
  php upgrade2.php 0.0.0 0.0.0 MAIN_MODULE_API,MAIN_MODULE_SUPPLIERPROPOSAL,MAIN_MODULE_WEBSITE,MAIN_MODULE_TICKETSUP,MAIN_MODULE_ACCOUNTING > $TRAVIS_BUILD_DIR/enablemodule.log
  echo $?
  cd -
  set +e
  echo
  #cat /tmp/dolibarr_install.log
  cat $TRAVIS_BUILD_DIR/enablemodule.log

- |
  echo "Unit testing"
  # Ensure we catch errors. Set this to +e if you want to go to the end to see dolibarr.log file.
  set -e
  phpunit -d memory_limit=-1 -c test/phpunit/phpunittest.xml test/phpunit/AllTests.php
  phpunitresult=$?
  echo "Phpunit return code = $phpunitresult"
  set +e

after_script:
- |
  echo "After script - Output lines of dolibarr.log"
  ls $TRAVIS_BUILD_DIR/documents
  #cat $TRAVIS_BUILD_DIR/documents/dolibarr.log
  sudo tail -n 50 $TRAVIS_BUILD_DIR/documents/dolibarr.log

after_success:
- |
  echo Success

after_failure:
- |
  echo Failure detected, so we show samples of log to help diagnose
  # This part of code is executed only if previous command that fails are enclosed with set +e
  # Upgrade log files
  for ficlog in `ls $TRAVIS_BUILD_DIR/*.log`
  do
    echo "Debugging informations for file $ficlog"
    #cat $ficlog
  done
  # Apache log file
  echo "Debugging informations for file apache error.log"
  sudo cat /var/log/apache2/travis_error_log
  if [ "$DEBUG" = true ]; then
    # Dolibarr log file
    echo "Debugging informations for file dolibarr.log (latest 50 lines)"
    tail -n 50 $TRAVIS_BUILD_DIR/documents/dolibarr.log
    # MariaDB log file
    echo "Debugging informations for file mysql error.log"
    sudo tail -n 50 /var/log/mysql/error.log
    # TODO: PostgreSQL log file
    echo
  fi
