language: php
dist: trusty
sudo: false

addons:
  apt:
    packages:
      - gdb

cache:
  directories:
    - ${HOME}/.cache/pip
    - ${HOME}/.composer/cache/files
    - ${HOME}/php-ext

env:
  global:
    - DRIVER_VERSION=1.5.0
    - SERVER_VERSION=4.0.0

matrix:
  fast_finish: true
  include:
    - php: 5.5
    - php: 5.6
    - php: 7.0
    - php: 7.1
    - php: 7.2
    - php: 7.0
      env:
        - SERVER_VERSION=3.0.15
    - php: 7.0
      env:
        - SERVER_VERSION=3.2.20
    - php: 7.0
      env:
        - SERVER_VERSION=3.4.15
    - php: 7.0
      env:
        - SERVER_VERSION=3.6.5

before_install:
  - pip install "mongo-orchestration>=0.6.7,<1.0" --user `whoami`
  - wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-${SERVER_VERSION}.tgz
  - tar zxf mongodb-linux-x86_64-${SERVER_VERSION}.tgz
  - export PATH=${PWD}/mongodb-linux-x86_64-${SERVER_VERSION}/bin/:${PATH}
  - mongod --version
  - mongo-orchestration --version
  - export MO_PATH=`python -c 'import mongo_orchestration; from os import path; print(path.dirname(mongo_orchestration.__file__));'`
  - |
    INI=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
    # tpecl is a helper to compile and cache php extensions
      tpecl () {
          local ext_name=$1
          local ext_so=$2
          local ext_dir=$(php -r "echo ini_get('extension_dir');")
          local ext_cache=~/php-ext/$(basename $ext_dir)/$ext_name
          if [[ -e $ext_cache/$ext_so ]]; then
              echo extension = $ext_cache/$ext_so >> $INI
          else
              mkdir -p $ext_cache
              echo yes | pecl install -f $ext_name &&
              cp $ext_dir/$ext_so $ext_cache
          fi
      }
      export -f tpecl

before_script:
  - mongo-orchestration start
  - pushd ${MO_PATH} && ${TRAVIS_BUILD_DIR}/.travis/mo.sh configurations/servers/clean.json start && popd
  - tpecl mongodb-${DRIVER_VERSION} mongodb.so
  - php --ri mongodb
  - composer install --no-interaction --no-progress --no-suggest
  - ulimit -c
  - ulimit -c unlimited -S

script:
  - vendor/bin/phpunit -v

before_cache:
  - rm -f ${HOME}/.cache/pip/log/debug.log

after_failure:
  - find . -name 'core*' -exec ${TRAVIS_BUILD_DIR}/.travis/debug-core.sh {} \;

after_script:
  - mongo-orchestration stop
