Skip to content

Commit

Permalink
Merge pull request #60 from City-of-Helsinki/UHF-11137
Browse files Browse the repository at this point in the history
UHF-11137: Reusable test workflow
  • Loading branch information
tuutti authored Dec 19, 2024
2 parents 3bed308 + 8586950 commit 64f58ed
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 102 deletions.
100 changes: 6 additions & 94 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,13 @@ on:
branches:
- main
name: CI
env:
SYMFONY_DEPRECATIONS_HELPER: disabled
SIMPLETEST_BASE_URL: http://app:8888
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']
container:
image: ghcr.io/city-of-helsinki/drupal-php-docker:${{ matrix.php-versions }}-alpine
options: --hostname app

services:
chromium:
# @todo Update this to newer version once minkphp supports Selenium 4.
# @see https://github.com/minkphp/MinkSelenium2Driver/pull/372
image: selenium/standalone-chrome:106.0
db:
image: mysql:8
env:
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
MYSQL_DATABASE: drupal
MYSQL_ROOT_PASSWORD: drupal
ports:
- 3306:3306

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Parse $MODULE_NAME from composer.json
run: echo "MODULE_NAME=$(cat composer.json | jq -r .name | awk -F/ '{print $NF}')" >> $GITHUB_ENV

- name: Set Drupal root
run: echo "DRUPAL_ROOT=$HOME/drupal" >> $GITHUB_ENV

- name: Set module folder
run: |
echo "MODULE_FOLDER=$DRUPAL_ROOT/public/modules/contrib/$MODULE_NAME" >> $GITHUB_ENV
echo "BROWSERTEST_OUTPUT_DIRECTORY=$DRUPAL_ROOT/public/sites/simpletest" >> $GITHUB_ENV
- name: Clone platform
run: |
git clone --depth=1 https://github.com/City-of-Helsinki/drupal-helfi-platform.git $DRUPAL_ROOT
rm -rf $DRUPAL_ROOT/.git
- name: Install required composer dependencies
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
composer config repositories.5 path $GITHUB_WORKSPACE
composer require drupal/$MODULE_NAME -W
# We use COMPOSER_MIRROR_PATH_REPOS=1 to mirror local repository
# instead of symlinking it to prevent code coverage issues with
# phpunit. Copy .git folder manually so codecov can generate line by
# line coverage.
cp -r $GITHUB_WORKSPACE/.git $MODULE_FOLDER/
- 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 en $MODULE_NAME -y
- name: Run PHPCS
working-directory: ${{ env.DRUPAL_ROOT }}
run: vendor/bin/phpcs $MODULE_FOLDER --standard=Drupal,DrupalPractice --extensions=php,module,install

- name: Run phpstan
working-directory: ${{ env.DRUPAL_ROOT }}
run: vendor/bin/phpstan analyze -c $MODULE_FOLDER/phpstan.neon $MODULE_FOLDER

- name: Start services
working-directory: ${{ env.DRUPAL_ROOT }}
run: vendor/bin/drush runserver $SIMPLETEST_BASE_URL --dns &

- name: Run PHPUnit tests
working-directory: ${{ env.DRUPAL_ROOT }}
run: |
vendor/bin/phpunit \
--bootstrap $DRUPAL_ROOT/public/core/tests/bootstrap.php \
-c $MODULE_FOLDER/phpunit.xml \
--coverage-clover=$MODULE_FOLDER/coverage.xml \
$MODULE_FOLDER
- name: Run codecov
working-directory: ${{ env.MODULE_FOLDER }}
run: codecov

- name: Create an artifact from test report
uses: actions/upload-artifact@v4
if: always()
with:
name: results-${{ matrix.php-versions }}
path: ${{ env.BROWSERTEST_OUTPUT_DIRECTORY }}
retention-days: 1
php-versions: ['8.3']
uses: city-of-helsinki/drupal-gh-actions/.github/workflows/module-tests.yml@main
with:
php_version: ${{ matrix.php-versions }}
secrets:
codecov_token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ini name="error_reporting" value="32767"/>
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", {"browserName":"chrome","chromeOptions":{"w3c": false}}, "http://chromium:4444"]' />
<env name="MINK_DRIVER_ARGS_WEBDRIVER" value='["chrome", {"browserName":"chrome", "goog:chromeOptions":{"w3c": true, "args":["--no-sandbox", "--ignore-certificate-errors", "--allow-insecure-localhost", "--headless", "--dns-prefetch-disable"]}}, "http://chromium:4444"]' />
</php>
<testsuites>
<testsuite name="unit">
Expand Down
6 changes: 3 additions & 3 deletions src/Menu/MenuTreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function __construct(
public function build(
string $menuName,
string $langcode,
object $rootElement = NULL,
MenuTreeParameters $parameters = NULL,
?object $rootElement = NULL,
?MenuTreeParameters $parameters = NULL,
): array {
if (!$parameters) {
$parameters = new MenuTreeParameters();
Expand Down Expand Up @@ -120,7 +120,7 @@ public function build(
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
private function transform(array $menuItems, string $langcode, string $rootId = NULL): array {
private function transform(array $menuItems, string $langcode, ?string $rootId = NULL): array {
$items = [];

foreach ($menuItems as $element) {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Kernel/KernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function register(ContainerBuilder $container) {
* @param string $apiKey
* The api key.
*/
protected function populateConfiguration(string $siteName = NULL, string $apiKey = '123') : void {
protected function populateConfiguration(?string $siteName = NULL, string $apiKey = '123') : void {
$this->config('system.site')->set('name', $siteName)->save();
$this->config('helfi_navigation.api')->set('key', $apiKey)->save();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/src/Unit/ApiManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private function getTimeMock(int $expectedTime) : ObjectProphecy {
*/
private function getApiClientMock(
ClientInterface $httpClient,
TimeInterface $time = NULL,
EnvironmentResolverInterface $environmentResolver = NULL,
?TimeInterface $time = NULL,
?EnvironmentResolverInterface $environmentResolver = NULL,
): ApiClient {
if (!$time) {
$time = $this->getTimeMock(time())->reveal();
Expand Down Expand Up @@ -130,7 +130,7 @@ private function getApiClientMock(
*/
private function getSut(
ApiClient $client,
EnvironmentResolverInterface $environmentResolver = NULL,
?EnvironmentResolverInterface $environmentResolver = NULL,
?string $apiKey = '123',
) : ApiManager {

Expand Down

0 comments on commit 64f58ed

Please sign in to comment.