version: '2.3'
services:
  chrome:
    image: ${DOCKER_SELENIUM_IMAGE}
    tmpfs:
      - /dev/shm:rw,nosuid,nodev,noexec,relatime

  mariadb10:
    # not using mariadb:10 for the time being, because 10.5.7 (currently latest) is broken
    image: mariadb:10.5.6
    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

  web:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    stop_grace_period: 1s
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    environment:
      TYPO3_PATH_ROOT: ${CORE_ROOT}/.Build/Web/typo3temp/var/tests/acceptance
      TYPO3_PATH_APP: ${CORE_ROOT}/.Build/Web/typo3temp/var/tests/acceptance
    command: >
      /bin/sh -c "
        if [ ${PHP_XDEBUG_ON} -eq 0 ]; then
          XDEBUG_MODE=\"off\" \
          php -S web:8000 -t ${CORE_ROOT}/.Build/Web
        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}\" \
          php -S web:8000 -t ${CORE_ROOT}/.Build/Web
        fi
      "

  acceptance_backend_mariadb10:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    links:
      - mariadb10
      - chrome
      - web
    environment:
      typo3DatabaseName: func_test
      typo3DatabaseUsername: root
      typo3DatabasePassword: funcp
      typo3DatabaseHost: mariadb10
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    working_dir: ${CORE_ROOT}/.Build
    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';
        mkdir -p Web/typo3temp/var/tests/
        COMMAND=\"vendor/codeception/codeception/codecept run Application -d -c Web/typo3conf/ext/{{EXTENSION_KEY}}/Tests/codeception.yml ${TEST_FILE}\"
        if [ ${PHP_XDEBUG_ON} -eq 0 ]; then
          XDEBUG_MODE=\"off\" \
          $${COMMAND};
        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}\" \
          $${COMMAND};
        fi
      "

  cgl:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    working_dir: ${CORE_ROOT}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP';
        if [ ${PHP_XDEBUG_ON} -eq 0 ]; then
          php -dxdebug.mode=off \
            .Build/bin/php-cs-fixer fix \
              -v \
              ${CGLCHECK_DRY_RUN} \
              --config=Build/php-cs-fixer.php \
              --using-cache=no .
        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}\" \
          PHP_CS_FIXER_ALLOW_XDEBUG=1 \
          .Build/bin/php-cs-fixer fix \
            -v \
            ${CGLCHECK_DRY_RUN} \
            --config=Build/php-cs-fixer.php \
            --using-cache=no .
        fi
      "

  composer_update:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    working_dir: ${CORE_ROOT}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP';
        COMPOSER_HOME=${CORE_ROOT}/.Build/.composer composer update --no-progress --no-interaction;
      "

  composer_validate:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    working_dir: ${CORE_ROOT}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP';
        composer validate;
      "

  functional_mariadb10:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    links:
      - mariadb10
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    environment:
      typo3DatabaseName: func_test
      typo3DatabaseUsername: root
      typo3DatabasePassword: funcp
      typo3DatabaseHost: mariadb10
    working_dir: ${CORE_ROOT}/.Build
    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\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/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}\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/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:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    environment:
      typo3DatabaseDriver: pdo_pgsql
      typo3DatabaseName: bamboo
      typo3DatabaseUsername: ${HOST_USER}
      typo3DatabaseHost: postgres10
      typo3DatabasePassword: funcp
    working_dir: ${CORE_ROOT}/.Build
    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\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/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}\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/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:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    tmpfs:
      - ${CORE_ROOT}/.Build/Web/typo3temp/var/tests/functional-sqlite-dbs/:rw,noexec,nosuid,uid=${HOST_UID}
    environment:
      typo3DatabaseDriver: pdo_sqlite
    working_dir: ${CORE_ROOT}/.Build
    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\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/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}\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/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:
      - ${CORE_ROOT}:${CORE_ROOT}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    working_dir: ${CORE_ROOT}
    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
      "

  phpstan:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    working_dir: ${CORE_ROOT}
    command: >
      /bin/sh -c "
        if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
          set -x
        fi
        php -v | grep '^PHP';
        php -dxdebug.mode=off .Build/bin/phpstan analyze -c Build/${PHPSTAN_CONFIG_FILE} --no-progress --no-interaction
      "

  unit:
    image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
    user: "${HOST_UID}"
    volumes:
      - ${CORE_ROOT}:${CORE_ROOT}
      - ${HOST_HOME}:${HOST_HOME}
      - /etc/passwd:/etc/passwd:ro
      - /etc/group:/etc/group:ro
    working_dir: ${CORE_ROOT}/.Build
    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\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/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}\" \
          bin/phpunit -c Web/typo3conf/ext/{{EXTENSION_KEY}}/Build/UnitTests.xml ${EXTRA_TEST_OPTIONS} ${TEST_FILE};
        fi
      "
