version: 2.1
defaults: &defaults
  docker:
    - image: circleci/php:7.4-cli
  working_directory: ~/repo
aliases:
  - &composer-cache
      v4-composer-cache
commands:
  start-project:
    steps:
      - run: sudo apt-get update && sudo apt-get install -y libpng-dev libjpeg62-turbo-dev
      - run:
          name: Install PHP Extensions
          command: sudo docker-php-ext-install gd
      - run:
          name: Disable Xdebug PHP extension
          command: sudo rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - checkout
      - restore_cache:
          keys:
            - *composer-cache
  install-dependencies:
    steps:
      - run: composer install -n --prefer-dist
      - save_cache:
          key: *composer-cache
          paths:
            - ~/.composer/cache
  create-drupal-project:
    parameters:
      project:
        type: string
        default: 'drupal/recommended-project:^8.8@alpha'
    steps:
      - run: composer create-project << parameters.project >> /tmp/drupal --no-interaction --prefer-dist --ignore-platform-reqs
      - run: composer require zaporylie/composer-drupal-optimizations:^1.1 --dev --working-dir=/tmp/drupal
  local-require:
    steps:
      - run:
          name: Add as local
          command: |
            cd /tmp/drupal
            composer require --dev drupal/core-dev:^8.8
            composer config repositories.1 '{"type": "path", "url": "'${CIRCLE_WORKING_DIRECTORY}'", "options": { "symlink": false }}'
            composer require --dev mglaman/phpstan-drupal "*" phpstan/extension-installer
            cat composer.json
            cp ~/repo/tests/fixtures/config/drupal-phpstan.neon /tmp/drupal/phpstan.neon
            ./vendor/bin/phpstan --version
jobs:
  build:
    <<: *defaults
    steps:
      - start-project
      - install-dependencies
      - run:
          name: phpspec/prophecy-phpunit fix
          command: composer require --dev phpspec/prophecy-phpunit:^2
      - run:
          name: CodeSniffer
          command: ./vendor/bin/phpcs src
      - run:
          name: PHPStan Analyze
          command: php -dmemory_limit=-1 vendor/bin/phpstan.phar
      - run:
          name: PHPUnit
          command: ./vendor/bin/phpunit
  test_drupal:
    <<: *defaults
    steps:
      - start-project
      - create-drupal-project:
          project: 'drupal/legacy-project:^8.8'
      - local-require
      - run:
          name: Run against a file
          command: |
            cd /tmp/drupal
            ./vendor/bin/phpstan analyze core/install.php --debug
      - run:
          name: Run against a module
          command: |
            cd /tmp/drupal
            ./vendor/bin/phpstan analyze --memory-limit=256M core/modules/dynamic_page_cache --debug
  test_drupal_project:
    <<: *defaults
    steps:
      - start-project
      - create-drupal-project
      - local-require
      - run:
          name: Run against a file
          command: |
            cd /tmp/drupal
            ./vendor/bin/phpstan analyze web/core/install.php --debug
      - run:
          name: Run against a module
          command: |
            cd /tmp/drupal
            ./vendor/bin/phpstan analyze --memory-limit=256M web/core/modules/dynamic_page_cache --debug
  test_upgrade_status:
    <<: *defaults
    steps:
      - start-project
      - create-drupal-project:
          project: 'drupal/legacy-project:^8@alpha'
      - local-require
      # Composer constraints prevent requiring via compser, but this helps test drupalci's phpstan build step as well.
      - run:
          name: Add upgrade_status
          command: |
            cd /tmp/drupal
            composer require phpstan/phpstan-deprecation-rules drupal/git_deploy
            curl -L https://ftp.drupal.org/files/projects/upgrade_status-8.x-1.x-dev.tar.gz | tar -zx -C /tmp/drupal/modules
      - run:
          name: Start builtin
          command: php -S 127.0.0.1:8080 -t /tmp/drupal
          background: true
      - run:
          name: Wait for web server
          command: dockerize -wait http://127.0.0.1:8080 -timeout 120s
      - run:
          name: Upgrade Status PHPUnit
          command: |
            cp ~/repo/tests/fixtures/config/circleci-phpunit.xml /tmp/drupal/core/phpunit.xml
            cd /tmp/drupal
            ./vendor/bin/phpunit -c core modules/upgrade_status --debug --stop-on-failure
workflows:
  version: 2
  tests:
    jobs:
      - build
      - test_drupal
      - test_drupal_project
      # - test_upgrade_status
  weekly:
    triggers:
      - schedule:
          # Every thursday to run soon after patch and security updates on wednesdays.
          cron: "0 0 * * 4"
          filters:
            branches:
              only:
                - master
    jobs:
      - build
      - test_drupal
      - test_drupal_project
