Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
de80ce2
GH-119: Add module-scoped phpunit GrumPHP task
hkirsman Mar 16, 2026
92e3d14
GH-119: Add phpunit_drupal_modules GrumPHP task
hkirsman Mar 16, 2026
9f9638e
119: Optimize Drupal module phpunit task
hkirsman Mar 16, 2026
81f1c24
#119: Add configurable phpunit config file for Drupal modules task
hkirsman Mar 16, 2026
3866dde
#119: Add testsuite option to Drupal modules phpunit task
hkirsman Mar 16, 2026
e993a86
#119: Print affected modules before running phpunit
hkirsman Mar 16, 2026
2a5f9eb
#119: Clarify and tighten Drupal modules phpunit task output
hkirsman Mar 16, 2026
dfa3a7e
119: Report modules without tests in phpunit task
hkirsman Mar 16, 2026
cca6935
#119: Highlight modules without tests in phpunit task
hkirsman Mar 16, 2026
51ed9d2
#119: Make phpunit_drupal_modules module roots configurable
hkirsman Mar 17, 2026
f2b754a
#119: Add tests for PhpUnitDrupalModulesTask arguments
hkirsman Mar 17, 2026
3238a92
119: Avoid callback functions in PhpUnitDrupalModulesTask
hkirsman Mar 17, 2026
a6a243d
119: Remove trailing blank lines from files
hkirsman Mar 17, 2026
6d5c8df
119: Remove empty line from PhpUnitDrupalModulesExtensionLoader
hkirsman Mar 17, 2026
bc89f31
#119: Refactor PhpUnitDrupalModulesTask for improved readability
hkirsman Mar 17, 2026
478d369
#119: Run phpunit per affected Drupal module
hkirsman Mar 17, 2026
ba7a752
#119: Log per-module progress in Drupal phpunit task
hkirsman Mar 17, 2026
18cf0f5
#119: Add timeout option for Drupal modules phpunit task
hkirsman Mar 17, 2026
e3c5837
#119: Add integration test for Drupal modules phpunit task
hkirsman Mar 17, 2026
42dd850
#119: Improve visibility and configuration of Drupal modules phpunit …
hkirsman Mar 17, 2026
a1a54e5
#119 Fix tests and coding standards.
hkirsman Mar 17, 2026
8b175c8
#119 Fix tests.
hkirsman Mar 17, 2026
7a2403f
#119: Assert skipped result code
hkirsman Mar 17, 2026
1688231
#119 We phpunit_drupal_modules task name as it's used on screen.
hkirsman Mar 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ grumphp:
yaml_lint: ~
json_lint: ~
psalm: ~
phpunit_drupal_modules: ~
extensions:
- Wunderio\GrumPHP\Task\PhpCompatibility\PhpCompatibilityExtensionLoader
- Wunderio\GrumPHP\Task\PhpCheckSyntax\PhpCheckSyntaxExtensionLoader
Expand All @@ -23,3 +24,4 @@ grumphp:
- Wunderio\GrumPHP\Task\YamlLint\YamlLintExtensionLoader
- Wunderio\GrumPHP\Task\JsonLint\JsonLintExtensionLoader
- Wunderio\GrumPHP\Task\Psalm\PsalmExtensionLoader
- Wunderio\GrumPHP\Task\PhpUnitDrupalModules\PhpUnitDrupalModulesExtensionLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Wunderio\GrumPHP\Task\PhpUnitDrupalModules;

use Wunderio\GrumPHP\Task\AbstractExternalExtensionLoader;

/**
* Registers the PhpUnitDrupalModules task in GrumPHP.
*/
class PhpUnitDrupalModulesExtensionLoader extends AbstractExternalExtensionLoader {}
229 changes: 229 additions & 0 deletions src/Task/PhpUnitDrupalModules/PhpUnitDrupalModulesTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php

declare(strict_types=1);

namespace Wunderio\GrumPHP\Task\PhpUnitDrupalModules;

use GrumPHP\Collection\ProcessArgumentsCollection;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Context\ContextInterface;
use Wunderio\GrumPHP\Task\AbstractMultiPathProcessingTask;

/**
* Class PhpUnitDrupalModulesTask.
*
* Runs phpunit only for affected Drupal custom modules.
*/
class PhpUnitDrupalModulesTask extends AbstractMultiPathProcessingTask {

/**
* {@inheritdoc}
*/
public function run(ContextInterface $context): TaskResultInterface {
$config = $this->getConfig()->getOptions();
$paths = $this->getPathsOrResult($context, $config, $this);

if ($paths instanceof TaskResultInterface) {
return $paths;
}

$moduleRoots = $this->getModuleRoots($config);
$modules = $this->collectModulesFromPaths($paths, $moduleRoots);

[$modulesWithTests, $modulesWithoutTests] = $this->splitModulesByTests($modules);

$this->printModulesWithoutTests($modulesWithoutTests);

if (!$modulesWithTests) {
return TaskResult::createSkipped($this, $context);
}

// Run phpunit once per affected module so that each module's directory
// is honoured even when a testsuite is used in the configuration file.
$this->printModulesWithTests($modulesWithTests);

$moduleCount = count($modulesWithTests);
$timeout = $config['timeout'] ?? NULL;

foreach ($modulesWithTests as $index => $modulePath) {
$process = $this->processBuilder->buildProcess($this->buildArguments([$modulePath]));

if ($timeout !== NULL) {
$process->setTimeout($timeout);
}

$process->run();

$result = $this->getTaskResult($process, $context);

fwrite(
STDOUT,
sprintf(
"%s: finished module %d/%d: %s [%s]\n\n",
$this->getName(),
$index + 1,
$moduleCount,
$modulePath,
$result->isPassed() ? 'OK' : 'FAILED'
)
);

if (!$result->isPassed()) {
// Stop on first failure/error to keep feedback fast and clear.
return $result;
}
}

return TaskResult::createPassed($this, $context);
}

/**
* Determine which directory roots should be treated as Drupal module roots.
*
* Defaults to web/modules/custom for backward compatibility, but can be
* configured via the run_on option in tasks.yml.
*
* @param array $config
* Task configuration options.
*
* @return string[]
* Normalised module root paths.
*/
private function getModuleRoots(array $config): array {
$moduleRoots = $config['run_on'] ?? ['web/modules/custom'];
$normalisedRoots = [];
foreach ($moduleRoots as $root) {
$normalisedRoots[] = rtrim((string) $root, '/');
}
return array_values($normalisedRoots);
}

/**
* Collect all affected modules from the changed file paths.
*
* @param iterable $paths
* Changed paths.
* @param string[] $moduleRoots
* Module root directories.
*
* @return string[]
* Module paths keyed by path for uniqueness.
*/
protected function collectModulesFromPaths(iterable $paths, array $moduleRoots): array {
$modules = [];
foreach ($paths as $file) {
$path = (string) $file;
foreach ($moduleRoots as $root) {
$rootWithSlash = $root . '/';

if (!str_starts_with($path, $rootWithSlash)) {
continue;
}

if (preg_match('#^(' . preg_quote($root, '#') . '/[^/]+)#', $path, $matches)) {
$modules[$matches[1]] = $matches[1];
}
}
}

return $modules;
}

/**
* Split modules into ones with and without tests directories.
*
* @param string[] $modules
* All affected modules.
*
* @return array{0: string[], 1: string[]}
* First array contains modules with tests, second without.
*/
protected function splitModulesByTests(array $modules): array {
$modulesWithTests = [];
foreach ($modules as $modulePath) {
if (is_dir($modulePath . '/tests')) {
$modulesWithTests[] = $modulePath;
}
}

$modulesWithoutTests = array_values(array_diff($modules, $modulesWithTests));

return [$modulesWithTests, $modulesWithoutTests];
}

/**
* Print a note about affected modules that do not have tests.
*
* @param string[] $modulesWithoutTests
* Affected modules without tests.
*/
private function printModulesWithoutTests(array $modulesWithoutTests): void {
if (!$modulesWithoutTests) {
return;
}

$lines = [];
foreach ($modulesWithoutTests as $modulePath) {
$lines[] = ' - ' . $modulePath;
}

fwrite(
STDOUT,
sprintf(
"\n%s: NOTE: affected modules without tests:\n%s\n\n",
$this->getName(),
implode("\n", $lines)
)
);
}

/**
* Print a list of modules for which tests will be executed.
*
* @param string[] $modulesWithTests
* Affected modules with tests.
*/
private function printModulesWithTests(array $modulesWithTests): void {
$lines = [];
foreach ($modulesWithTests as $modulePath) {
$lines[] = ' - ' . $modulePath;
}

fwrite(
STDOUT,
sprintf(
"%s: running tests for modules:\n%s\n\n",
$this->getName(),
implode("\n", $lines)
)
);
}

/**
* {@inheritdoc}
*/
public function buildArguments(iterable $modules): ProcessArgumentsCollection {
$config = $this->getConfig()->getOptions();

$arguments = $this->processBuilder->createArgumentsForCommand('phpunit');

if (!empty($config['config_file'])) {
// Mirror GrumPHP's core phpunit task: allow passing a custom config file.
$arguments->add('-c');
$arguments->add($config['config_file']);
}

if (!empty($config['testsuite'])) {
$arguments->add('--testsuite');
$arguments->add($config['testsuite']);
}

foreach ($modules as $modulePath) {
$arguments->add($modulePath);
}

return $arguments;
}

}
9 changes: 9 additions & 0 deletions src/Task/PhpUnitDrupalModules/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
Wunderio\GrumPHP\Task\PhpUnitDrupalModules\PhpUnitDrupalModulesTask:
class: Wunderio\GrumPHP\Task\PhpUnitDrupalModules\PhpUnitDrupalModulesTask
arguments:
- '@process_builder'
- '@formatter.raw_process'
tags:
- { name: grumphp.task, task: phpunit_drupal_modules }

28 changes: 28 additions & 0 deletions src/Task/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,31 @@ Wunderio\GrumPHP\Task\Psalm\PsalmTask:
show_info:
defaults: false
allowed_types: ['bool']

Wunderio\GrumPHP\Task\PhpUnitDrupalModules\PhpUnitDrupalModulesTask:
name: phpunit_drupal_modules
is_file_specific: true
options:
ignore_patterns:
defaults:
- '/vendor/'
- '/node_modules/'
- '/core/'
- '/libraries/'
- '/contrib/'
allowed_types: ['array']
extensions:
defaults: ['php', 'inc', 'module', 'install', 'theme']
allowed_types: ['array']
run_on:
defaults: ['web/modules/custom']
allowed_types: ['array']
config_file:
defaults: ~
allowed_types: ['string', 'null']
testsuite:
defaults: ~
allowed_types: ['string', 'null']
timeout:
defaults: ~
allowed_types: ['int', 'null']
Loading