From 278e4482af0ba7bf87d7f8fe6c75e9538eab8874 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Fri, 14 Feb 2020 11:30:03 +0100 Subject: [PATCH] write first tests --- .github/workflows/test.yml | 60 +++++++++++++++++++ lib/stages/build.sh | 8 --- lib/stages/coding_style.sh | 2 +- lib/stages/prepare_build.sh | 14 +---- tests/module/composer.json | 9 +++ tests/module/module.info.yml | 6 ++ tests/module/module.routing.yml | 7 +++ tests/module/module.services.yml | 4 ++ .../src/Controller/ModuleController.php | 25 ++++++++ tests/module/src/Service.php | 25 ++++++++ .../ModuleControllerTest.php | 40 +++++++++++++ tests/module/tests/Unit/ModuleServiceTest.php | 50 ++++++++++++++++ 12 files changed, 230 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 tests/module/composer.json create mode 100644 tests/module/module.info.yml create mode 100644 tests/module/module.routing.yml create mode 100644 tests/module/module.services.yml create mode 100644 tests/module/src/Controller/ModuleController.php create mode 100644 tests/module/src/Service.php create mode 100644 tests/module/tests/FunctionalJavascript/ModuleControllerTest.php create mode 100644 tests/module/tests/Unit/ModuleServiceTest.php diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..5e32ca8 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,60 @@ +name: Run module tests + +on: + pull_request: + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + RUN: [1, 2, 3] + include: + - RUN: 1 + DRUPAL_TESTING_COMPOSER_PROJECT: 'drupal/recommended-project' + DRUPAL_TESTING_DRUPAL_VERSION: '~8.7.0' + - RUN: 2 + DRUPAL_TESTING_COMPOSER_PROJECT: 'drupal/recommended-project' + DRUPAL_TESTING_DRUPAL_VERSION: '*' + - RUN: 3 + DRUPAL_TESTING_COMPOSER_PROJECT: 'thunder/thunder-project' + DRUPAL_TESTING_DRUPAL_VERSION: '~8.8.0' + + steps: + - uses: actions/checkout@v1 + + - uses: shivammathur/setup-php@master + with: + coverage: none + php-version: '7.3' + + - name: Cache composer dependencies + uses: actions/cache@v1 + with: + path: ~/.composer/cache + key: ${{ runner.os }}-composer-cache-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer-cache- + + - name: Cache NPM dependencies + id: npm-cache + uses: actions/cache@v1 + with: + path: ~/.npm + key: ${{ runner.os }}-npm-cache + restore-keys: ${{ runner.os }}-npm-cache + + - name: Setup environment variables + run: | + echo "::add-path::$HOME/.composer/vendor/bin" + echo "::add-path::$GITHUB_WORKSPACE/vendor/bin" + echo "::add-path::$GITHUB_WORKSPACE/bin" + + - name: Run tests + run: test-drupal-project + env: + DRUPAL_TESTING_TEST_CODING_STYLES: false + DRUPAL_TESTING_PROJECT_BASEDIR: ${{ github.workspace }}/tests/module + DRUPAL_TESTING_COMPOSER_PROJECT: ${{ matrix.DRUPAL_TESTING_COMPOSER_PROJECT }} + DRUPAL_TESTING_DRUPAL_VERSION: ${{ matrix.DRUPAL_TESTING_DRUPAL_VERSION }} \ No newline at end of file diff --git a/lib/stages/build.sh b/lib/stages/build.sh index bfaf80f..0b2132f 100644 --- a/lib/stages/build.sh +++ b/lib/stages/build.sh @@ -19,14 +19,6 @@ _stage_build() { major_version="$(cut -d'.' -f1 <<<"${installed_version}")" minor_version="$(cut -d'.' -f2 <<<"${installed_version}")" - # When we are having Drupal 9 or if we are on at least Drupal 8.8, use core scaffold. Otherwise use the legacy scaffolding. - if [[ ${major_version} -gt 8 ]] || [[ ${minor_version} -gt 7 ]]; then - composer require drupal/core-composer-scaffold - else - composer require drupal-composer/drupal-scaffold - fi - composer drupal:scaffold - # Back to previous directory. cd - || exit diff --git a/lib/stages/coding_style.sh b/lib/stages/coding_style.sh index 06a95ec..3b98bc3 100644 --- a/lib/stages/coding_style.sh +++ b/lib/stages/coding_style.sh @@ -24,7 +24,7 @@ __test_php_coding_styles() { __test_javascript_coding_styles() { if ! [[ -f .eslintrc ]]; then - printf "%sNo .eslintrc file found. Skipping javascript coding style test.%s\n\n" "${YELLOW}" "${TRANSPARENT}" + printf "%bNo .eslintrc file found. Skipping javascript coding style test.%b\n\n" "${YELLOW}" "${TRANSPARENT}" return fi diff --git a/lib/stages/prepare_build.sh b/lib/stages/prepare_build.sh index 3d8b334..30aa7ef 100644 --- a/lib/stages/prepare_build.sh +++ b/lib/stages/prepare_build.sh @@ -13,15 +13,10 @@ _stage_prepare_build() { printf "Prepare composer.json\n\n" # Build is based on drupal project - if [[ ${DRUPAL_TESTING_COMPOSER_PROJECT} == "drupal/recommended-project" ]]; then - composer create-project "${DRUPAL_TESTING_COMPOSER_PROJECT}" "${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" "${DRUPAL_TESTING_DRUPAL_VERSION}" --no-interaction --no-install - else - composer create-project "${DRUPAL_TESTING_COMPOSER_PROJECT}" "${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" --no-interaction --no-install - - composer require drupal/core:"${DRUPAL_TESTING_DRUPAL_VERSION}" --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" - composer require drupal/core-recommended:"${DRUPAL_TESTING_DRUPAL_VERSION}" --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" - fi + composer create-project "${DRUPAL_TESTING_COMPOSER_PROJECT}" "${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" --no-interaction --no-install + composer require drupal/core:"${DRUPAL_TESTING_DRUPAL_VERSION}" --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" + composer require drupal/core-recommended:"${DRUPAL_TESTING_DRUPAL_VERSION}" --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" composer require drupal/core-dev:"${DRUPAL_TESTING_DRUPAL_VERSION}" --dev --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" # Add asset-packagist for projects, that require frontend assets @@ -37,9 +32,6 @@ _stage_prepare_build() { composer require drush/drush --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" - # Install without core-composer-scaffold until we know, what version of core is used. - composer remove drupal/core-composer-scaffold --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" - # Require phpstan. if ${DRUPAL_TESTING_TEST_DEPRECATION}; then composer require mglaman/phpstan-drupal:~0.12.0 --no-update --working-dir="${DRUPAL_TESTING_DRUPAL_INSTALLATION_DIRECTORY}" diff --git a/tests/module/composer.json b/tests/module/composer.json new file mode 100644 index 0000000..75d2081 --- /dev/null +++ b/tests/module/composer.json @@ -0,0 +1,9 @@ +{ + "name": "thunder/module", + "description": "Drupal module with all sorts of tests, to test capabilities of the drupal-testing package.", + "type": "drupal-module", + "license": "GPL-2.0-or-later", + "require": { + "php": ">=7.2.0" + } +} diff --git a/tests/module/module.info.yml b/tests/module/module.info.yml new file mode 100644 index 0000000..63ef6b5 --- /dev/null +++ b/tests/module/module.info.yml @@ -0,0 +1,6 @@ +name: Module +type: module +description: Drupal module with all sorts of tests, to test capabilities of the drupal-testing package. +package: Drupal Testing +core: 8.x +core_version_requirement: ^8 || ^9 diff --git a/tests/module/module.routing.yml b/tests/module/module.routing.yml new file mode 100644 index 0000000..56d7bc4 --- /dev/null +++ b/tests/module/module.routing.yml @@ -0,0 +1,7 @@ +module.example: + path: '/module/controller' + defaults: + _title: 'Example' + _controller: '\Drupal\module\Controller\ModuleController::build' + requirements: + _permission: 'access content' diff --git a/tests/module/module.services.yml b/tests/module/module.services.yml new file mode 100644 index 0000000..a22dbb3 --- /dev/null +++ b/tests/module/module.services.yml @@ -0,0 +1,4 @@ +services: + module.service: + class: Drupal\module\Service + arguments: [] diff --git a/tests/module/src/Controller/ModuleController.php b/tests/module/src/Controller/ModuleController.php new file mode 100644 index 0000000..8c6939c --- /dev/null +++ b/tests/module/src/Controller/ModuleController.php @@ -0,0 +1,25 @@ + 'item', + '#markup' => $this->t('It works!'), + ]; + + return $build; + } + +} diff --git a/tests/module/src/Service.php b/tests/module/src/Service.php new file mode 100644 index 0000000..f4c6850 --- /dev/null +++ b/tests/module/src/Service.php @@ -0,0 +1,25 @@ +drupalLogin($this->drupalCreateUser([ + 'access content', + ])); + } + + /** + * Test enhanced entity revision routes access. + */ + public function testControllerRoute(): void { + $this->drupalGet('/module/controller'); + $this->assertSession()->pageTextContains('It works!'); + } + +} diff --git a/tests/module/tests/Unit/ModuleServiceTest.php b/tests/module/tests/Unit/ModuleServiceTest.php new file mode 100644 index 0000000..72f8d2f --- /dev/null +++ b/tests/module/tests/Unit/ModuleServiceTest.php @@ -0,0 +1,50 @@ +service = new Service(); + } + + /** + * @covers ::serve + * + * @dataProvider numberProvider + */ + public function testServe($number, $serving): void { + $this->assertEquals($serving, $this->service->serve($number)); + } + + /** + * Data provider for testServe(). + * + * @return array + * A list of number and the servings they generate. + */ + public function numberProvider(): array { + return [ + [1, 'You have been served a 1'], + ]; + } + +}