executor-machine: &executor-machine 'ubuntu-2004:2022.04.1'

jobs:
    test_back_static_and_acceptance:
        machine:
            image: *executor-machine
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: No legacy translation format
                  command: PIM_CONTEXT=test make find-legacy-translations
            - run:
                  name: Load archived docker image
                  command: docker load -i php-pim-image.tar
            - run:
                  name: Static tests
                  command: PIM_CONTEXT=test make static-back
            - run:
                  name: Run deprecations detections
                  command: PIM_CONTEXT=test make deprecation-back
            - run:
                  name: Analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs
                  command: PIM_CONTEXT=test make lint-back
            - run:
                  name: Code Coupling Detection
                  command: PIM_CONTEXT=test make coupling-back
            - run:
                  name: Unit tests
                  command: PIM_CONTEXT=test make unit-back
            - run:
                  name: Acceptance tests
                  command: PIM_CONTEXT=test make acceptance-back
            - store_test_results:
                  path: var/tests
            - store_artifacts:
                  path: var/tests
            - store_artifacts:
                  path: var/logs

    test_back_phpunit:
        machine:
            image: *executor-machine
        parallelism: 20
        steps:
            - attach_workspace:
                  at: ~/
            - install_yq_v3
            - set_docker_gcp_mirror

            - run:
                  name: Change owner on project dir in order to archive the project into the workspace
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: Start containers
                  command: |
                      docker load -i php-pim-image.tar
                      APP_ENV=test C='fpm mysql elasticsearch object-storage pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Install database
                  command: APP_ENV=test make database
            - run:
                  name: PhpUnit Integration
                  command: PIM_CONTEXT=test make pim-integration-back
            - run:
                  name: PhpUnit End to end
                  command: PIM_CONTEXT=test make end-to-end-back
            - store_test_results:
                  path: var/tests/phpunit
            - store_artifacts:
                  path: var/tests/phpunit
            - store_artifacts:
                  path: var/logs

    cypress_sanity_checks:
        machine:
            image: *executor-machine
        resource_class: large
        steps:
            - attach_workspace:
                  at: ~/
            - install_yq_v3
            - set_docker_gcp_mirror

            - run:
                  name: Change owner on project dir in order to archive the project into the workspace
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: Create yarn cache folder
                  command: mkdir -p  ~/.cache/yarn
            - run:
                  name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                  command: sudo chown -R 1000:1000 ../project ~/.cache/yarn
            - run:
                  name: Start containers
                  command: |
                      docker load -i php-pim-image.tar
                      APP_ENV=prod C='fpm mysql elasticsearch httpd object-storage pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Install database
                  command: APP_ENV=prod O="--catalog src/Akeneo/Platform/Bundle/InstallerBundle/Resources/fixtures/icecat_demo_dev" make database
            - run:
                  name: Launch Cypress
                  command: CYPRESS_defaultCommandTimeout=10000 CYPRESS_requestTimeout=15000 CYPRESS_responseTimeout=50000 make end-to-end-front
            - store_artifacts:
                  path: cypress/screenshots
            - store_artifacts:
                  path: cypress/videos

    test_back_behat_legacy:
        machine:
            image: *executor-machine
        parallelism: 40
        steps:
            - attach_workspace:
                  at: ~/
            - install_yq_v3
            - set_docker_gcp_mirror

            - run:
                  name: Get Behat Suite name to run
                  command: |
                      TESTSUITE=$(echo $CIRCLE_BRANCH | sed -e 's/^.*-\([^-]*\)$/\1/g')
                      if ! [[ $TESTSUITE =~ ^(weasel|chipmunk|raccoon)$ ]] ; then
                        TESTSUITE="all"
                      fi
                      echo "Behat Suite to run: "$TESTSUITE
                      echo "export TESTSUITE=$TESTSUITE" >> $BASH_ENV
            - run:
                  name: Change owner on project dir in order to archive the project into the workspace
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: Start containers
                  command: |
                      docker load -i php-pim-image.tar
                      APP_ENV=behat C='fpm mysql elasticsearch httpd object-storage selenium pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Install database
                  command: APP_ENV=behat make database
            - run:
                  name: End to end Behat tests
                  command: PIM_CONTEXT=test make end-to-end-legacy SUITE=$TESTSUITE
            - run:
                  name: Gather Junit test result files in the same directory to improve the render of failing tests
                  command: |
                      set -e
                      cd var/tests/behat
                      sudo chmod -R 777 .
                      for subdir in */*; do mv "${subdir}" "${subdir/\//_}"; done
                  when: always
            - store_test_results:
                  path: var/tests/behat
            - store_artifacts:
                  path: var/tests/behat
            - store_artifacts:
                  path: var/logs
            - store_artifacts:
                  path: var/tests/screenshots

    pull_request_success:
        docker:
            - image: alpine/git
        steps:
            - run:
                  name: Success
                  command: echo "The build has run with success! Let's merge :)"

    test_front_static_acceptance_and_integration:
        machine:
            image: *executor-machine
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Create yarn cache folder
                  command: mkdir -p  ~/.cache/yarn
            - run:
                  name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                  command: sudo chown -R 1000:1000 ../project ~/.cache/yarn
            - run:
                  name: Front type checking
                  command: make javascript-dev-strict
            - run:
                  name: Front linter
                  command: PIM_CONTEXT=test make lint-front
            - run:
                  name: Front unit tests
                  command: PIM_CONTEXT=test make unit-front

    test_back_performance:
        machine:
            image: *executor-machine
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Change owner on project dir (docker needs uid 1000, circleci can be another uid)
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: Start containers
                  command: |
                      export ES_JAVA_OPTS='-Xms2g -Xmx2g'
                      docker load -i php-pim-image.tar
                      APP_ENV=test APP_DEBUG=false C='fpm mysql httpd elasticsearch object-storage blackfire pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Run performance tests
                  command: APP_ENV=test .circleci/run_performance_tests.sh
            - store_test_results:
                  path: var/tests/phpunit
            - store_artifacts:
                  path: var/tests/phpunit
            - store_artifacts:
                  path: var/logs

    test_back_missing_structure_migrations:
        machine:
            image: *executor-machine
        steps:
            -   attach_workspace:
                    at: ~/
            -   run:
                    name: Load php image
                    command: |
                        docker load -i php-pim-image.tar
                        APP_ENV=test C='mysql elasticsearch object-storage pubsub-emulator' make up
                        docker/wait_docker_up.sh
            -   run:
                    name: Restore permissions on files
                    command: git checkout -- .
            -   run:
                    name: Restore permissions on vendor files
                    command: |
                        cd vendor/akeneo/pim-community-dev
                        git checkout -- .
            -   run:
                    name: Change owner on project dir after restoring cache
                    command: sudo chown -R 1000:1000 ../project
            -   run:
                    name: Test missing database and index structure migrations
                    command: vendor/akeneo/pim-community-dev/.circleci/detect_structure_changes.sh $CIRCLE_BRANCH

    test_back_data_migrations:
        machine:
            image: *executor-machine
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Change owner on project dir in order to archive the project into the workspace
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: Start containers
                  command: |
                      docker load -i php-pim-image.tar
                      APP_ENV=test C='fpm mysql elasticsearch object-storage pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Install database
                  command: APP_ENV=test make database
            - run:
                  name: PhpUnit Migration
                  command: PIM_CONTEXT=test make migration-back
            - store_test_results:
                  path: var/tests/phpunit
            - store_artifacts:
                  path: var/tests/phpunit
            - store_artifacts:
                  path: var/logs

    test_onboarder_bundle:
        machine:
            image: *executor-machine
        environment:
            FLAG_ONBOARDER_ENABLED: 1
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Change owner on project dir in order to archive the project into the workspace
                  command: |
                      mkdir -p ~/.cache/yarn ~/.composer
                      sudo chown -R 1000:1000 ../project
                      sudo chown -R 1000:1000 ~/.composer
                      sudo chown -R 1000:1000 ~/.cache/yarn
            - run:
                  name: Create an empty service account
                  command: |
                      mkdir secret
                      echo '{"project_id":"dummy"}' > secret/serviceAccount.json
            - run:
                  name: Load php pim image
                  command: docker load -i php-pim-image.tar
            - run:
                  name: Load make commands
                  command: cp vendor/akeneo/pim-onboarder/onboarder.mk make-file/onboarder.mk
            - run:
                  name: Require onboarder tests dependencies
                  command: PIM_CONTEXT=onboarder make add-bundle-specific-dev-dependencies
            - run:
                  name: Composer update for tests dependencies
                  command: docker-compose run --rm php composer update --no-interaction
            - run:
                  name: Add configuration files to run the bundle tests from the PIM
                  command: |
                      rm -f docker-compose.override.yml
                      PIM_VERSION=master SETUP_FOR_CI=1 PIM_CONTEXT=onboarder make setup-onboarder-parameters
                      PIM_VERSION=master PIM_CONTEXT=onboarder make setup-onboarder-tests
                      docker-compose run --rm php php /usr/local/bin/composer dumpautoload --no-interaction
            - run:
                  name: Change owner of PIM as some files have been created with wrong owner
                  command: sudo chown -R 1000:1000 ~/project
            - run:
                  name: PHP coupling detector
                  command: PIM_CONTEXT=onboarder make test-coupling-detector
            - run:
                  name: Start containers
                  command: |
                      APP_ENV=test C='mysql elasticsearch object-storage pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Execute specifications
                  command: PIM_CONTEXT=onboarder make test-spec
            - run:
                  name: Install Akeneo PIM with Onboarder specific configuration (channel, attribute, ...)
                  command: ENVIRONMENT=test SETUP_FOR_CI=1 PIM_CONTEXT=onboarder make onboarder-install
            - run:
                  name: Execute acceptance tests
                  command: PIM_CONTEXT=onboarder make test-acceptance
            - run:
                  name: Execute PHPUnit integration tests
                  command: PIM_CONTEXT=onboarder make test-integration
            - run:
                  name: Start Apache/FPM and Selenium for End to End tests
                  command: PIM_CONTEXT=onboarder make up APP_ENV=behat C='fpm httpd selenium'
            - run:
                  name: Execute end-to-end tests
                  command: PIM_CONTEXT=onboarder make test-end-to-end
            - run:
                  name: Restart FPM with Onboarder feature turned off
                  command: PIM_CONTEXT=onboarder make up APP_ENV=behat FLAG_ONBOARDER_ENABLED=0 C='fpm'
            - run:
                  name: Execute end-to-end "Onboarder feature disabled" tests
                  command: PIM_CONTEXT=onboarder make test-end-to-end-onboarder-disabled
            - store_test_results:
                  path: ~/project/pim/var/tests
            - store_artifacts:
                  path: ~/project/pim/var/tests
            - store_artifacts:
                  path: ~/project/pim/var/logs

    test_database:
        machine:
            image: *executor-machine
        parallelism: 1
        steps:
            - attach_workspace:
                  at: ~/
            - run:
                  name: Change owner on project dir in order to archive the project into the workspace
                  command: sudo chown -R 1000:1000 ../project
            - run:
                  name: Start containers
                  command: |
                      docker load -i php-pim-image.tar
                      APP_ENV=test C='fpm mysql elasticsearch object-storage pubsub-emulator' make up
                      docker/wait_docker_up.sh
            - run:
                  name: Install database
                  command: APP_ENV=test make database
            - run:
                  name: Database test
                  command: APP_ENV=test PIM_CONTEXT=test make test-database-structure
