# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2.1
executors:
  plb:
    parameters:
      php:
        type: string
        default: '8.1'
      mariadb:
        type: string
        default: '10.6'
    docker:
      - image: cimg/php:<< parameters.php >>
      - image: cimg/mariadb:<< parameters.mariadb >>
        environment:
          MYSQL_ROOT_PASSWORD: rootplb
          MYSQL_DATABASE: planningb
          MYSQL_USER: plb
          MYSQL_PASSWORD: plb

jobs:
  install-and-test:
    parameters:
      e:
        type: executor
    executor: << parameters.e >>
    steps:
      - checkout

      # For google chrome.
      - run: sudo sh -c 'echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google-chrome.list'
      - run: wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

      - run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
      - run: sudo -E install-php-extensions zip mysqli pdo_mysql
      - run: sudo apt install -y google-chrome-stable
      # MT41778: Install and remove to get dependencies
      - run: sudo apt remove -y google-chrome-stable
      - run: sudo apt install -y mariadb-client
      - run: sudo pecl install xdebug

      # MT41778 : Download Google Chrome from an archive to be sure to have version v114
      - run: sudo apt install -y libu2f-udev
      - run: wget -O /tmp/googlechrome.zip https://github.com/PlanningBiblio/googlechrome/archive/refs/heads/v114.zip
      - run: unzip -o /tmp/googlechrome.zip
      - run: sudo dpkg -i googlechrome-114/google-chrome-stable_current_amd64.deb
      - run: rm -rf googlechrome-114

      # Chrome driver.
      - run: wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
      - run: sudo unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

      # Download and cache dependencies
      - restore_cache:
          keys:
            # "composer.lock" can be used if it is committed to the repo
            - v1-dependencies-{{ checksum "composer.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: zcat data/planno_24.04.xx.sql.gz | mysql --host=127.0.01 --port=3306 -u plb --password=plb planningb
      - run: cp .circleci/.env.test.local .env.test.local
      - run: cp .circleci/.env.local .env.local
      - run: rm -rf vendor
      - run: composer install -n --prefer-dist
      - run: php public/setup/updatedatabase.php
      - run: vendor/bin/bdi detect drivers

      - save_cache:
          key: v1-dependencies-{{ checksum "composer.json" }}
          paths:
            - ./vendor

      # run tests with phpunit or codecept
      - run: echo 'xdebug.mode=coverage' | sudo tee -a /etc/php.d/circleci.ini
      - run: ./vendor/bin/phpunit --coverage-text --coverage-clover coverage.xml --bootstrap tests/bootstrap.php tests/
      - run: bash <(curl -s https://codecov.io/bash)

workflows:
  version: 2
  build:
    jobs:
      - install-and-test:
          name: php:8.1
          e:
            name: plb
            php: '8.1'
      - install-and-test:
          name: php:8.3:mariadb10.11
          e:
            name: plb
            php: '8.3'
            mariadb: '10.11'
