Skip to content

Commit

Permalink
write first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosen authored Feb 14, 2020
1 parent e91a276 commit 278e448
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 20 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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 }}
8 changes: 0 additions & 8 deletions lib/stages/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/stages/coding_style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 3 additions & 11 deletions lib/stages/prepare_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down
9 changes: 9 additions & 0 deletions tests/module/composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
6 changes: 6 additions & 0 deletions tests/module/module.info.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions tests/module/module.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.example:
path: '/module/controller'
defaults:
_title: 'Example'
_controller: '\Drupal\module\Controller\ModuleController::build'
requirements:
_permission: 'access content'
4 changes: 4 additions & 0 deletions tests/module/module.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
module.service:
class: Drupal\module\Service
arguments: []
25 changes: 25 additions & 0 deletions tests/module/src/Controller/ModuleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Drupal\module\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
* Returns responses for module routes.
*/
class ModuleController extends ControllerBase {

/**
* Builds the response.
*/
public function build() {

$build['content'] = [
'#type' => 'item',
'#markup' => $this->t('It works!'),
];

return $build;
}

}
25 changes: 25 additions & 0 deletions tests/module/src/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Drupal\module;

/**
* The Service class.
*
* @package Drupal\module
*/
class Service {

/**
* Serve the service.
*
* @param int $number
* A number we do things with.
*
* @return string
* Information based on the given number.
*/
public function serve(int $number): string {
return 'You have been served a ' . $number;
}

}
40 changes: 40 additions & 0 deletions tests/module/tests/FunctionalJavascript/ModuleControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Drupal\Tests\module\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;

/**
* Class ModuleControllerTest.
*
* Javascript tests.
*
* @group module
*/
class ModuleControllerTest extends WebDriverTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = ['module'];

/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();

$this->drupalLogin($this->drupalCreateUser([
'access content',
]));
}

/**
* Test enhanced entity revision routes access.
*/
public function testControllerRoute(): void {
$this->drupalGet('/module/controller');
$this->assertSession()->pageTextContains('It works!');
}

}
50 changes: 50 additions & 0 deletions tests/module/tests/Unit/ModuleServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Drupal\Tests\module\Unit;

use Drupal\module\Service;
use Drupal\Tests\UnitTestCase;

/**
* @coversDefaultClass \Drupal\module\Service
* @group module
*/
class ModuleServiceTest extends UnitTestCase {
/**
* The entity permission provider.
*
* @var \Drupal\module\Service
*/
protected $service;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$this->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'],
];
}

}

0 comments on commit 278e448

Please sign in to comment.