dist: xenial # see https://travis-ci.community/t/postgresql-fails-to-start-unrecognized-operating-system/4558

language: php

install:
    - composer install

jobs:
    include:
        -
            stage: test
            name: "SQLite"
            php: '7.2'
            script:
                - vendor/bin/phpunit

        # PostgreSQL
        - &POSTGRES_JOB
            stage: test
            name: "PostgreSQL"
            services:
                # https://docs.travis-ci.com/user/database-setup/#postgresql
                - postgresql
            env:
                - DB_ENGINE=pdo_pgsql
                - DB_HOST=localhost
                - DB_NAME=orm_behaviors_test
                - DB_USER=root
                - DB_PASSWD=root
                - DB_MEMORY=false
            php: '7.2'
            install:
                - composer update --prefer-lowest
            before_script:
                - psql -c 'CREATE DATABASE orm_behaviors_test' -U postgres
                # see https://www.guru99.com/postgresql-create-alter-add-user.html step 5)
                - psql orm_behaviors_test -c "CREATE USER root WITH PASSWORD 'root'" -U postgres
                - psql orm_behaviors_test -c 'CREATE EXTENSION cube' -U postgres
                - psql orm_behaviors_test -c 'CREATE EXTENSION earthdistance' -U postgres
            script:
                - vendor/bin/phpunit

        -
            <<: *POSTGRES_JOB
            php: '7.3'

        -
            <<: *POSTGRES_JOB
            php: '7.4'

        # MySQL
        - &MYSQL_JOB
            stage: test
            name: "MySQL"
            services:
                # https://docs.travis-ci.com/user/database-setup/#mysql
                - mysql
            env:
                - DB_ENGINE=pdo_mysql
                - DB_HOST=localhost
                - DB_NAME=orm_behaviors_test
                - DB_USER=root
                - DB_PASSWD=""
                - DB_MEMORY=false
            php: '7.2'
            install:
                - composer update --prefer-lowest
            before_script:
                - mysql -e 'create database IF NOT EXISTS orm_behaviors_test' -uroot
            script:
                - vendor/bin/phpunit

        -
            <<: *MYSQL_JOB
            php: '7.3'

        -
            <<: *MYSQL_JOB
            php: '7.4'

cache:
    directories:
        - $HOME/.composer/cache
