defaults: &defaults
  docker:
    # specify the version you desire here (avoid latest except for testing)
    - image: mkeereman/drupal8_tests:8.7

    - image: selenium/standalone-chrome-debug:3.14.0-beryllium

    - image: mariadb:10.3
      environment:
        MYSQL_ALLOW_EMPTY_PASSWORD: 1
  working_directory: /var/www/html/modules/better_exposed_filters

# YAML does not support merging of lists. That means we can't have a default
# 'steps' configuration, though we can have defaults for individual step
# properties.

# We use the composer.json as a way to determine if we can cache our build.
restore_cache: &restore_cache
  keys:
    - v4-dependencies-{{ checksum "composer.json" }}-{{ checksum "../../composer.json" }}

# If composer.json hasn't changed, restore the Composer cache directory. We
# don't restore the lock file so we ensure we get updated dependencies.
save_cache: &save_cache
  paths:
    - /root/.composer/cache/files
  key: v4-dependencies-{{ checksum "composer.json" }}-{{ checksum "../../composer.json" }}

# Install composer dependencies into the workspace to share with all jobs.
update_dependencies: &update_dependencies
  <<: *defaults
  steps:
    - checkout

    - restore_cache: *restore_cache

    - run:
        working_directory: /var/www/html
        command: |
          ./update-dependencies.sh $CIRCLE_PROJECT_REPONAME

    - save_cache: *save_cache

    - persist_to_workspace:
        root: /var/www/html
        paths:
          - .

# Run Drupal unit and kernel tests as one job. This command invokes the test.sh
# hook.
unit_kernel_tests: &unit_kernel_tests
  <<: *defaults
  steps:
    - attach_workspace:
        at: /var/www/html

    - checkout

    - run:
        working_directory: /var/www/html
        command: |
          ./test.sh $CIRCLE_PROJECT_REPONAME

    - store_test_results:
        path: /var/www/html/artifacts/phpunit
    - store_artifacts:
        path: /var/www/html/artifacts

# Run Drupal functional and functional JS tests as one job. This command invokes
# the test-functional.sh and test-functional-js hooks.
functional_tests: &functional_tests
  <<: *defaults
  steps:
    - attach_workspace:
        at: /var/www/html

    - checkout

    - run:
        working_directory: /var/www/html
        command: |
          ./test-functional.sh $CIRCLE_PROJECT_REPONAME
          ./test-functional-js.sh $CIRCLE_PROJECT_REPONAME

    - store_test_results:
        path: /var/www/html/artifacts/phpunit
    - store_artifacts:
        path: /var/www/html/artifacts

# Run code quality tests. This invokes code-sniffer.sh.
code_sniffer: &code_sniffer
  <<: *defaults
  steps:
    - attach_workspace:
        at: /var/www/html

    - checkout

    # Don't exit builds in CI when warning/error occurs.
    # @todo fix errors so this becomes reliable
    - run:
        working_directory: /var/www/html
        command: |
          vendor/bin/phpcs --config-set ignore_warnings_on_exit 1
          vendor/bin/phpcs --config-set ignore_errors_on_exit 1
          ./code-sniffer.sh $CIRCLE_PROJECT_REPONAME

    - store_test_results:
        path: /var/www/html/artifacts/phpcs
    - store_artifacts:
        path: /var/www/html/artifacts

# Run code coverage tests. This invokes code-coverage-stats.sh.
code_coverage: &code_coverage
  <<: *defaults
  steps:
    - attach_workspace:
        at: /var/www/html

    - checkout

    - run:
        working_directory: /var/www/html
        command: |
          ./code-coverage-stats.sh $CIRCLE_PROJECT_REPONAME
    - store_artifacts:
        path: /var/www/html/artifacts

# Declare all of the jobs we should run.
version: 2
jobs:
  update-dependencies:
     <<: *update_dependencies
  run-unit-kernel-tests:
     <<: *unit_kernel_tests
  run-functional-tests:
     <<: *functional_tests
  run-code-sniffer:
     <<: *code_sniffer
  run-code-coverage:
     <<: *code_coverage

workflows:
  version: 2

  # Declare a workflow that runs all of our jobs in parallel.
  test_and_lint:
    jobs:
      - update-dependencies
      - run-unit-kernel-tests:
          requires:
            - update-dependencies
      - run-functional-tests:
          requires:
            - update-dependencies
      - run-code-sniffer:
          requires:
            - update-dependencies
      - run-code-coverage:
          requires:
            - update-dependencies
            - run-unit-kernel-tests
