Skip to content

Commit

Permalink
chore: working out testing with gh actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pgautier404 committed Oct 10, 2023
1 parent 174f448 commit d55fe51
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 0 deletions.
160 changes: 160 additions & 0 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
name: On Pull Request

on:
pull_request:
branches: [ main ]

env:
SIMPLETEST_DB: "mysql://drupal:[email protected]:3306/drupal"
SIMPLETEST_BASE_URL: "http://127.0.0.1:8080"
DRUPAL_MODULE_NAME: "drupal-cache"
DRUPAL_CORE_VERSION: 9.4.x
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php-version: ['7.4'] # , '8.0', '8.1']

services:
mariadb:
image: mariadb:10.9
env:
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
MYSQL_DATABASE: drupal
MYSQL_ROOT_PASSWORD: drupal
ports:
- 3306:3306

steps:

- uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Setup PHP with composer v2
uses: shivammathur/setup-php@v2
with:
php-version: '${{ matrix.php-version }}'
extensions: grpc
tools: composer:v2

# The next two steps exposes $DRUPAL_ROOT and $MODULE_FOLDER environment
# variables.
#
# They have to be set in separate steps because $DRUPAL_ROOT variable
# won't be accessible in currently active step.
# You can access them either with $DRUPAL_ROOT and $MODULE_FOLDER or
# ${{ env.DRUPAL_ROOT }} and ${{ env.MODULE_FOLDER }}.
- name: Define DRUPAL_ROOT env variable
run: echo "DRUPAL_ROOT=$HOME/drupal" >> $GITHUB_ENV

# Change the module folder according to your needs, it's probably either
# modules/contrib/$DRUPAL_MODULE_NAME or modules/custom/$DRUPAL_MODULE_NAME.
- name: Set module folder
run: |
echo "MODULE_FOLDER=$DRUPAL_ROOT/modules/contrib/$DRUPAL_MODULE_NAME" \
>> $GITHUB_ENV
# Clone Drupal core into $DRUPAL_ROOT folder.
# Core version can be set by changing $DRUPAL_CORE_VERSION.
- name: Clone drupal
run: |
git clone --depth 1 --branch "$DRUPAL_CORE_VERSION" \
http://git.drupal.org/project/drupal.git/ $DRUPAL_ROOT
# Override the platform.php config with currently active PHP version.
# As of writing this, the composer.json shipped with core sets
# platform.php version to 7.3.0, meaning we possibly get dependencies
# that won't work with php 8+.

- name: Override the platform.php version
working-directory: ${{ env.DRUPAL_ROOT }}
run: composer config platform.php ${{ matrix.php-version }}

# Set the module folder as a composer repository, so the latest code
# is symlinked from $GITHUB_WORKSPACE to modules/ folder.
- name: Install the module
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
composer config repositories.0 path $GITHUB_WORKSPACE
composer require momentohq/$DRUPAL_MODULE_NAME
# PHP 8+ requires newer phpunit, use core's composer script
# to upgrade it.
- name: Upgrade phpunit
working-directory: ${{ env.DRUPAL_ROOT }}
run: composer run-script drupal-phpunit-upgrade

# We use drush's build-in webserver to run tests. Make sure
# Drush is installed.
- name: Install drush
working-directory: ${{ env.DRUPAL_ROOT }}
run: composer require "drush/drush ^11.0"

- name: Install PHPCS
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
composer config --no-plugins \
allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev "drupal/coder"
# Install Drupal using minimal installation profile and enable the module.
- name: Install Drupal
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
php -d sendmail_path=$(which true); vendor/bin/drush --yes -v \
site-install minimal --db-url="$SIMPLETEST_DB"
vendor/bin/drush pm-list --type=module
vendor/bin/drush en $DRUPAL_MODULE_NAME -y
- name: Run PHPCS
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal \
--extensions=php,module,inc,install,test,info
- name: Start Drush webserver and chromedriver
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
vendor/bin/drush runserver $SIMPLETEST_BASE_URL > /dev/null 2>&1 &
chromedriver --port=4444 > /dev/null 2>&1 &
# Wait for drush server to start.
for i in {1..5}; do \
RESPONSE_CODE=$(curl -s -o /dev/null \
-w "%{http_code}" "$SIMPLETEST_BASE_URL" || true); \
if [ "$RESPONSE_CODE" -gt "301" ] || [ "$RESPONSE_CODE" -lt "200" ]; \
then sleep 2; fi; done
# Chromium browser is required to run functional javascript tests.
# You can remove or uncomment this step if you don't have any functional
# js tests.
# - name: Start chromium-browser
# working-directory: ${{ env.DRUPAL_ROOT }}
# run: |
# chromium-browser --headless --disable-gpu \
# --no-sandbox \
# --remote-debugging-port=9222 &

# Run tests using core's run-tests.sh script. See the example below
# to run tests using phpunit.
# - name: Run tests
# working-directory: ${{ env.DRUPAL_ROOT }}
# run: |
# php ./core/scripts/run-tests.sh --dburl $SIMPLETEST_DB \
# --php /usr/local/bin/php --color --verbose \
# --sqlite /tmp/test.sqlite \
# --url $SIMPLETEST_BASE_URL $DRUPAL_MODULE_NAME

# Uncomment this step to run tests using phpunit. Your module is expected
# to ship with 'phpunit.xml' file. See the repository for an example
# phpunit.xml file.
- name: Run tests
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
vendor/bin/phpunit --bootstrap $DRUPAL_ROOT/core/tests/bootstrap.php \
-c $MODULE_FOLDER/phpunit.xml $MODULE_FOLDER
75 changes: 75 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- For how to customize PHPUnit configuration, see core/tests/README.md. -->
<!-- TODO set checkForUnintentionallyCoveredCode="true" once https://www.drupal.org/node/2626832 is resolved. -->
<!-- PHPUnit expects functional tests to be run with either a privileged user
or your current system user. See core/tests/README.md and
https://www.drupal.org/node/2116263 for details.
-->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" colors="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" beStrictAboutChangesToGlobalState="true" failOnWarning="true" printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter" cacheResult="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./includes</directory>
<directory>./lib</directory>
<directory>./modules</directory>
<directory>../modules</directory>
<directory>../sites</directory>
</include>
<exclude>
<directory>./modules/*/src/Tests</directory>
<directory>./modules/*/tests</directory>
<directory>../modules/*/src/Tests</directory>
<directory>../modules/*/tests</directory>
<directory>../modules/*/*/src/Tests</directory>
<directory>../modules/*/*/tests</directory>
</exclude>
</coverage>
<php>
<!-- Set error reporting to E_ALL. -->
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
<!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
<env name="SIMPLETEST_BASE_URL" value="http://localhost"/>
<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
<env name="SIMPLETEST_DB" value="mysql://drupal:drupal@localhost/drupal"/>
<!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/html/drupal/sites/default/browser_output"/>
<!-- By default, browser tests will output links that use the base URL set
in SIMPLETEST_BASE_URL. However, if your SIMPLETEST_BASE_URL is an internal
path (such as may be the case in a virtual or Docker-based environment),
you can set the base URL used in the browser test output links to something
reachable from your host machine here. This will allow you to follow them
directly and view the output. -->
<env name="BROWSERTEST_OUTPUT_BASE_URL" value=""/>
<!-- To disable deprecation testing completely uncomment the next line. -->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
<!-- Example for changing the driver class for mink tests MINK_DRIVER_CLASS value: 'Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver' -->
<env name="MINK_DRIVER_CLASS" value=""/>
<!-- Example for changing the driver args to mink tests MINK_DRIVER_ARGS value: '["http://127.0.0.1:8510"]' -->
<env name="MINK_DRIVER_ARGS" value=""/>
<!-- Example for changing the driver args to webdriver tests MINK_DRIVER_ARGS_WEBDRIVER value: '["chrome", { "chromeOptions": { "w3c": false } }, "http://localhost:4444/wd/hub"]' For using the Firefox browser, replace "chrome" with "firefox" -->
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value=""/>
</php>
<testsuites>
<testsuite name="unit">
<file>./tests/TestSuites/UnitTestSuite.php</file>
</testsuite>
<testsuite name="kernel">
<file>./tests/TestSuites/KernelTestSuite.php</file>
</testsuite>
<testsuite name="functional">
<file>./tests/TestSuites/FunctionalTestSuite.php</file>
</testsuite>
<testsuite name="functional-javascript">
<file>./tests/TestSuites/FunctionalJavascriptTestSuite.php</file>
</testsuite>
<testsuite name="build">
<file>./tests/TestSuites/BuildTestSuite.php</file>
</testsuite>
</testsuites>
<listeners>
<listener class="\Drupal\Tests\Listeners\DrupalListener">
</listener>
</listeners>
<!-- Filter for coverage reports. -->
</phpunit>

0 comments on commit d55fe51

Please sign in to comment.