language: php
php:
 - 8.1

# Configure different DB environments
env:
  - DB=mysql
  - DB=mongo
  
# jobs:
#   allow_failures:
#     - php: 8.0
  
branches:
  only:
    - dev
    - stable

before_script:
  # Update and install base system
  - sudo apt-get update
  - sudo apt-get install -y --force-yes apache2 libapache2-mod-fastcgi make libxml2-utils curl
  - sudo a2enmod headers
  # Enable php-fpm
  - sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf
  - 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
  - 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
  - sudo a2enmod rewrite actions fastcgi alias
  # Enable mongo extension
  - echo "extension = mongodb.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; 
  # Enable vhost
  - sudo cp -f Tests/build/travis-ci-apache /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
  # Configure Known
  - if [[ "$DB" == "pgsql" ]]; then echo "PROVISIONING WITH PostgreSQL"; fi
  - if [[ "$DB" == "pgsql" ]]; then sudo cp -f Tests/build/config_pgsql.ini $TRAVIS_BUILD_DIR/configuration/config.ini; fi
  - if [[ "$DB" == "pgsql" ]]; then psql -c 'create database known_unittest;' -U postgres; fi
  - if [[ "$DB" == "pgsql" ]]; then psql -U postgres -d known_unittest -a -f ./warmup/schemas/postgres/postgres.sql; fi
  - if [[ "$DB" == "pgsql" ]]; then psql -d known_unittest -c '\dt;' -U postgres; fi
  - if [[ "$DB" == "mysql" ]]; then echo "PROVISIONING WITH MySQL"; fi
  - if [[ "$DB" == "mysql" ]]; then sudo cp -f Tests/build/config_mysql.ini $TRAVIS_BUILD_DIR/configuration/config.ini; fi
  - if [[ "$DB" == "mysql" ]]; then mysql --version; fi
  - if [[ "$DB" == "mysql" ]]; then mysql -e "CREATE DATABASE known_unittest;" -uroot; fi
  - if [[ "$DB" == "mysql" ]]; then mysql -e "GRANT ALL ON known_unittest.* TO 'travis'@'127.0.0.1';" -uroot; fi
  - if [[ "$DB" == "mysql" ]]; then mysql -e "USE known_unittest; SOURCE ./warmup/schemas/mysql/mysql.sql; show table status; show index from entities;" -utravis; fi
  - if [[ "$DB" == "mongo" ]]; then echo "PROVISIONING WITH MongoDB (default)"; fi
  - if [[ "$DB" == "mongo" ]]; then sudo cp -f Tests/build/config_default.ini $TRAVIS_BUILD_DIR/configuration/config.ini; fi
  - sudo cp -f $TRAVIS_BUILD_DIR/warmup/webserver-configs/htaccess.dist $TRAVIS_BUILD_DIR/.htaccess
  # Restart services
  - ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm
  - sudo service apache2 restart
  # Ensure composer has installed dependancies
  - composer install --no-dev --ignore-platform-reqs

services:
  - mongodb
  - postgresql
  - mysql
  
addons:
  apt:
    sources:
      - mongodb-3.4-precise
    packages:
      - mongodb-org-server

after_failure:
  - sudo cat $TRAVIS_BUILD_DIR/apache-error.log

notifications:
  email:
    - hello@withknown.com
  irc:
    channels:
      - "irc.freenode.net#knownchat"
    on_success: change
    on_failure: always
