From cd907c81d990304ee8affc9d91ff6ffffd03e42e Mon Sep 17 00:00:00 2001 From: IanM Date: Mon, 12 Oct 2026 11:51:43 +0100 Subject: [PATCH] feat(init): add test filter scripts to new extensions The backend testing module now adds test:unit:filter and test:integration:filter, so one test class can be run with composer test:integration:filter -- ClassName. --- boilerplate/skeleton/extension/composer.json | 4 ++++ src/steps/gen-ext-scaffolder.ts | 4 ++++ test/steps/init/init.test.ts | 21 ++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/boilerplate/skeleton/extension/composer.json b/boilerplate/skeleton/extension/composer.json index 3ae458da..40557248 100644 --- a/boilerplate/skeleton/extension/composer.json +++ b/boilerplate/skeleton/extension/composer.json @@ -49,7 +49,9 @@ "@test:integration" ], "test:unit": "phpunit -c tests/phpunit.unit.xml", + "test:unit:filter": "phpunit -c tests/phpunit.unit.xml --filter", "test:integration": "phpunit -c tests/phpunit.integration.xml", + "test:integration:filter": "phpunit -c tests/phpunit.integration.xml --filter", "test:setup": "@php tests/integration/setup.php", "analyse:phpstan": "phpstan analyse", "clear-cache:phpstan": "phpstan clear-result-cache" @@ -57,7 +59,9 @@ "scripts-descriptions": { "test": "Runs all tests.", "test:unit": "Runs all unit tests.", + "test:unit:filter": "Runs unit tests whose name matches: composer test:unit:filter -- ClassName", "test:integration": "Runs all integration tests.", + "test:integration:filter": "Runs integration tests whose name matches: composer test:integration:filter -- ClassName", "test:setup": "Sets up a database for use with integration tests. Execute this only once.", "analyse:phpstan": "Run static analysis" }, diff --git a/src/steps/gen-ext-scaffolder.ts b/src/steps/gen-ext-scaffolder.ts index 1ad6d663..cf0221f4 100644 --- a/src/steps/gen-ext-scaffolder.ts +++ b/src/steps/gen-ext-scaffolder.ts @@ -651,11 +651,15 @@ function moduleNameToDef(name: ExtensionModules): Module { 'autoload-dev.psr-4.${params.packageNamespace}\\Tests\\', 'scripts.test', 'scripts.test:unit', + 'scripts.test:unit:filter', 'scripts.test:integration', + 'scripts.test:integration:filter', 'scripts.test:setup', 'scripts-descriptions.test', 'scripts-descriptions.test:unit', + 'scripts-descriptions.test:unit:filter', 'scripts-descriptions.test:integration', + 'scripts-descriptions.test:integration:filter', 'scripts-descriptions.test:setup', 'require-dev.flarum/testing', ], diff --git a/test/steps/init/init.test.ts b/test/steps/init/init.test.ts index 54e96afc..7be375bd 100644 --- a/test/steps/init/init.test.ts +++ b/test/steps/init/init.test.ts @@ -182,4 +182,25 @@ describe('Test extension skeleton step', function () { expect(getExtFileContents(fs, '.github/workflows/frontend.yml').includes('my/prod/branch')).toBe(true); expect(await getEnabledModules(fs)).toStrictEqual(buildModules([])); }); + + test('Adds scripts to run only the tests whose name matches', async function () { + const { fs } = await runStep(initStep, {}, { usePrompts: false, initialParams: vars }); + + const composerJson = JSON.parse(getExtFileContents(fs, 'composer.json')); + + expect(composerJson.scripts['test:unit:filter']).toBe('phpunit -c tests/phpunit.unit.xml --filter'); + expect(composerJson.scripts['test:integration:filter']).toBe('phpunit -c tests/phpunit.integration.xml --filter'); + expect(composerJson['scripts-descriptions']['test:unit:filter']).toBeDefined(); + expect(composerJson['scripts-descriptions']['test:integration:filter']).toBeDefined(); + }); + + test('Leaves out every test script without backend testing', async function () { + const { fs } = await runStep(initStep, {}, { usePrompts: false, initialParams: { ...vars, 'modules.backendTesting': false } }); + + const composerJson = JSON.parse(getExtFileContents(fs, 'composer.json')); + const testScripts = (scripts: Record = {}) => Object.keys(scripts).filter((name) => name.startsWith('test')); + + expect(testScripts(composerJson.scripts)).toStrictEqual([]); + expect(testScripts(composerJson['scripts-descriptions'])).toStrictEqual([]); + }); });