version: '2.3'
services:
  mariadb10:
    image: mariadb:10
    environment:
      MYSQL_ROOT_PASSWORD: funcp
    tmpfs:
      - /var/lib/mysql/:rw,noexec,nosuid

  postgres10:
    image: postgres:10-alpine
    environment:
      POSTGRES_PASSWORD: funcp
      POSTGRES_USER: ${HOST_USER}
    tmpfs:
      - /var/lib/postgresql/data:rw,noexec,nosuid

  composer_update:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    volumes:
      - ${ROOT_DIR}:${ROOT_DIR}
    working_dir: ${ROOT_DIR}
    environment:
      COMPOSER_CACHE_DIR: ".cache/composer"
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP';
        if [ ${TYPO3_VERSION} -eq 11 ]; then
              composer req --dev --no-update \
                typo3/cms-composer-installers:^3.0
              composer req typo3/cms-core:^11.5 --no-update
        fi
        if [ ${TYPO3_VERSION} -eq 12 ]; then
            composer req --dev --no-update \
               "typo3/cms-composer-installers:^5.0" \
                typo3/cms-backend:^12.4 \
                typo3/cms-frontend:^12.4 \
                typo3/cms-extbase:^12.4 \
                typo3/cms-fluid:^12.4 \
                typo3/cms-install:^12.4
            composer req typo3/cms-core:^12.4 -W --no-update
        fi
        composer update --no-progress --no-interaction;
      "

  functional_mariadb10:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: ${HOST_UID}
    links:
      - mariadb10
    volumes:
      - ${ROOT_DIR}:${ROOT_DIR}
    environment:
      typo3DatabaseName: func_test
      typo3DatabaseUsername: root
      typo3DatabasePassword: funcp
      typo3DatabaseHost: mariadb10
    working_dir: ${ROOT_DIR}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        echo Waiting for database start...;
        while ! nc -z mariadb10 3306; do
          sleep 1;
        done;
        echo Database is up;
        php -v | grep '^PHP';
        if [ ${PHP_XDEBUG_ON} -eq 0 ]; then
          XDEBUG_MODE=\"off\" \
          .Build/bin/phpunit -c Build/FunctionalTests.xml ${EXTRA_TEST_OPTIONS} ${TEST_FILE};
        else
          DOCKER_HOST=`route -n | awk '/^0.0.0.0/ { print $$2 }'`
          XDEBUG_MODE=\"debug,develop\" \
          XDEBUG_TRIGGER=\"foo\" \
          XDEBUG_CONFIG=\"client_port=${PHP_XDEBUG_PORT} client_host=$${DOCKER_HOST}\" \
          .Build/bin/phpunit -c Build/FunctionalTests.xml ${EXTRA_TEST_OPTIONS} ${TEST_FILE};
        fi
      "

  functional_postgres10:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: ${HOST_UID}
    links:
      - postgres10
    volumes:
      - ${ROOT_DIR}:${ROOT_DIR}
    environment:
      typo3DatabaseDriver: pdo_pgsql
      typo3DatabaseName: bamboo
      typo3DatabaseUsername: ${HOST_USER}
      typo3DatabaseHost: postgres10
      typo3DatabasePassword: funcp
    working_dir: ${ROOT_DIR}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        echo Waiting for database start...;
        while ! nc -z postgres10 5432; do
          sleep 1;
        done;
        echo Database is up;
        php -v | grep '^PHP';
        if [ ${PHP_XDEBUG_ON} -eq 0 ]; then
          XDEBUG_MODE=\"off\" \
          .Build/bin/phpunit -c Build/FunctionalTests.xml ${EXTRA_TEST_OPTIONS} --exclude-group not-postgres ${TEST_FILE};
        else
          DOCKER_HOST=`route -n | awk '/^0.0.0.0/ { print $$2 }'`
          XDEBUG_MODE=\"debug,develop\" \
          XDEBUG_TRIGGER=\"foo\" \
          XDEBUG_CONFIG=\"client_port=${PHP_XDEBUG_PORT} client_host=$${DOCKER_HOST}\" \
          .Build/bin/phpunit -c Build/FunctionalTests.xml ${EXTRA_TEST_OPTIONS} --exclude-group not-postgres ${TEST_FILE};
        fi
      "

  functional_sqlite:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: ${HOST_UID}
    volumes:
      - ${ROOT_DIR}:${ROOT_DIR}
    tmpfs:
      - ${ROOT_DIR}/.Build/Web/typo3temp/var/tests/functional-sqlite-dbs/:rw,noexec,nosuid,uid=${HOST_UID}
    environment:
      typo3DatabaseDriver: pdo_sqlite
    working_dir: ${ROOT_DIR}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP';
        if [ ${PHP_XDEBUG_ON} -eq 0 ]; then
          XDEBUG_MODE=\"off\" \
          .Build/bin/phpunit -c Build/FunctionalTests.xml ${EXTRA_TEST_OPTIONS} --exclude-group not-sqlite ${TEST_FILE};
        else
          DOCKER_HOST=`route -n | awk '/^0.0.0.0/ { print $$2 }'`
          XDEBUG_MODE=\"debug,develop\" \
          XDEBUG_TRIGGER=\"foo\" \
          XDEBUG_CONFIG=\"client_port=${PHP_XDEBUG_PORT} client_host=$${DOCKER_HOST}\" \
          .Build/bin/phpunit -c Build/FunctionalTests.xml ${EXTRA_TEST_OPTIONS} --exclude-group not-sqlite ${TEST_FILE};
        fi
      "

  lint:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: ${HOST_UID}
    volumes:
      - ${ROOT_DIR}:${ROOT_DIR}
    working_dir: ${ROOT_DIR}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP';
        find . -name \\*.php ! -path "./.Build/\\*" -print0 | xargs -0 -n1 -P4 php -dxdebug.mode=off -l >/dev/null
      "

  unit:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: ${HOST_UID}
    volumes:
      - ${ROOT_DIR}:${ROOT_DIR}
    working_dir: ${ROOT_DIR}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP'
        if [ ${PHP_XDEBUG_ON} -eq 0 ]; then
          XDEBUG_MODE=\"off\" \
          .Build/bin/phpunit -c Build/UnitTests.xml ${EXTRA_TEST_OPTIONS} ${TEST_FILE};
        else
          DOCKER_HOST=`route -n | awk '/^0.0.0.0/ { print $$2 }'`
          XDEBUG_MODE=\"debug,develop\" \
          XDEBUG_TRIGGER=\"foo\" \
          XDEBUG_CONFIG=\"client_port=${PHP_XDEBUG_PORT} client_host=$${DOCKER_HOST}\" \
          .Build/bin/phpunit -c Build/UnitTests.xml ${EXTRA_TEST_OPTIONS} ${TEST_FILE};
        fi
      "
