# Select image from https://hub.docker.com/_/php/
#image: php:7.3
# Use a prepared Hubzilla image to optimise pipeline duration
image: registry.gitlab.com/dawnbreak/hubzilla/core:php7.3

stages:
  - test
  - deploy


# Select what we should cache
cache:
  paths:
  - vendor/
  - .cache/


# global variables for all jobs, if no job specific variables
variables:
  # Tell composer to use the project workspace .cache folder
  COMPOSER_CACHE_DIR: "$CI_PROJECT_DIR/.cache/composer"
  # Ignore a Composer warning
  COMPOSER_ALLOW_SUPERUSER: 1
  # Configure MySQL/MariaDB service (https://hub.docker.com/_/mysql/, https://hub.docker.com/_/mariadb/)
  MYSQL_DATABASE: hello_world_test
  MYSQL_ROOT_PASSWORD: mysql
  # Configure PostgreSQL service (https://hub.docker.com/_/postgres/)
  POSTGRES_DB: ci-db
  POSTGRES_USER: ci-user
  POSTGRES_PASSWORD: ci-pass


before_script:
# pecl and composer do not work with PHP production restrictions (from Hubzilla Docker image)
- if [ -f /usr/local/etc/php/conf.d/z_prod.ini ]; then mv /usr/local/etc/php/conf.d/z_prod.ini /usr/local/etc/php/conf.d/z_prod.ini.off; fi
# Install & enable Xdebug for code coverage reports
- pecl install xdebug
- docker-php-ext-enable xdebug
# Install composer
- curl -sS https://getcomposer.org/installer | php
# Install dev libraries from composer
- php ./composer.phar install --no-progress
# php.ini settings
- echo 'xdebug.mode=coverage' >> /usr/local/etc/php/php.ini

# hidden job definition with template for MySQL/MariaDB
.job_template_mysql: &job_definition_mysql
  stage: test
  script:
  - echo "USE $MYSQL_DATABASE; $(cat ./install/schema_mysql.sql)" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"
  - echo "SHOW DATABASES;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"
  - echo "USE $MYSQL_DATABASE; SHOW TABLES;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql "$MYSQL_DATABASE"
  - vendor/bin/phpunit --configuration tests/phpunit.xml --coverage-text

# hidden job definition with template for PostgreSQL
.job_template_postgres: &job_definition_postgres
  stage: test
  services:
  - postgres:latest
  script:
  - export PGPASSWORD=$POSTGRES_PASSWORD
  - psql --version
  - psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT VERSION();"
  # Import hubzilla's DB schema
  - psql -h "postgres" -U "$POSTGRES_USER" -v ON_ERROR_STOP=1 --quiet "$POSTGRES_DB" < ./install/schema_postgres.sql
  # Show databases and relations/tables of hubzilla's database
  #- psql -h "postgres" -U "$POSTGRES_USER" -l
  #- psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "\dt;"
  # Run the actual tests
  - vendor/bin/phpunit --configuration tests/phpunit-pgsql.xml --testdox

# hidden job definition with artifacts config template
.artifacts_template:
  artifacts: &artifacts_template
    expire_in: 1 week
    # Gitlab should show the results, but has problems parsing PHPUnit's junit file.
    reports:
      junit: tests/results/junit.xml
    # Archive test results (coverage, testdox, junit)
    name: "$CI_COMMIT_REF_SLUG-$CI_JOB_NAME"
    paths:
      - tests/results/


# PHP7.3 with MySQL 5.7
php7.3_mysql5.7:
  <<: *job_definition_mysql
  services:
  - mysql:5.7


# PHP7.3 with MySQL 8 (latest)
php7.3_mysql8:
  <<: *job_definition_mysql
  services:
  - name: mysql:8
    command: ["--default-authentication-plugin=mysql_native_password"]


# PHP7.3 with MariaDB 10.2
php7.3_mariadb10.2:
  <<: *job_definition_mysql
  services:
  - name: mariadb:10.2
    alias: mysql


# PHP7.3 with MariaDB 10.3 (latest)
php7.3_mariadb10.3:
  <<: *job_definition_mysql
  image: registry.gitlab.com/dawnbreak/hubzilla/core:php7.3
  services:
  - name: mariadb:10.3
    alias: mysql


# PHP7.3 with PostgreSQL latest (11)
php7.3_postgres11:
  <<: *job_definition_postgres
  artifacts: *artifacts_template


# PHP7.3 with PostgreSQL latest (11)
php7.3_postgres11:
  <<: *job_definition_postgres
  image: registry.gitlab.com/dawnbreak/hubzilla/core:php7.3
  artifacts: *artifacts_template


# Generate Doxygen API Documentation and deploy it as GitLab pages
pages:
  stage: deploy
  cache: {}
  image: php:7-cli-alpine
  before_script:
    - apk update
    - apk add doxygen ttf-freefont graphviz
  script:
    - doxygen util/Doxyfile
    - mv doc/html/ public/
    - echo "API documentation should be accessible at https://hubzilla.frama.io/core/ soon"
  artifacts:
    paths:
      - public
  only:
    # Only generate it on main repo's master branch
    - master@hubzilla/core