Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 __mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ const window = {
};
}),
showErrorMessage: vi.fn(),
showInformationMessage: vi.fn(),
showQuickPick: vi.fn(),
};

const commands = (function () {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
"command": "phpunit.run-test-at-cursor",
"title": "PHPUnit: Run the test at the current cursor position"
},
{
"command": "phpunit.run-by-group",
"title": "PHPUnit: Run tests by group"
},
{
"command": "phpunit.rerun",
"title": "PHPUnit: Repeat the last test run"
Expand Down
38 changes: 38 additions & 0 deletions src/Commands/TestCommandRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,44 @@ export class TestCommandRegistry {
});
}

runByGroup() {
return commands.registerCommand('phpunit.run-by-group', async () => {
let groups = this.testCollection.findGroups();
if (groups.length === 0) {
await this.testFileDiscovery.reloadAll();
groups = this.testCollection.findGroups();
}
if (groups.length === 0) {
window.showInformationMessage(
'No PHPUnit groups found. Add @group annotations or #[Group] attributes to your tests.',
);
return;
}

const selectedGroup = await window.showQuickPick(groups, {
placeHolder: 'Select a PHPUnit group to run',
title: 'Run Tests by Group',
});
if (!selectedGroup) {
return;
}

const tests = this.testCollection.findTestsByGroup(selectedGroup);
if (tests.length === 0) {
window.showInformationMessage(`No tests found for group "${selectedGroup}".`);
return;
}

const cancellation = new CancellationTokenSource().token;
await this.handler.startGroupTestRun(
selectedGroup,
tests,
cancellation,
this.testRunProfile,
);
});
}

rerun() {
return commands.registerCommand('phpunit.rerun', () => {
return this.run(
Expand Down
24 changes: 24 additions & 0 deletions src/TestCollection/TestCollection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ describe('Extension TestCollection', () => {
]);
});

it('find groups and tests by group', async () => {
const collection = givenTestCollection(`
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>`);

await collection.add(URI.file(phpUnitProject('tests/AssertionsTest.php')));
await collection.add(URI.file(phpUnitProject('tests/AttributeTest.php')));

expect(collection.findGroups()).toEqual(['integration']);
expect(collection.findTestsByGroup('integration')).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: 'Assertions (Tests\\Assertions)::Passed',
}),
expect.objectContaining({
id: 'Attribute (Tests\\Attribute)::Hi',
}),
]),
);
});

it('with testsuites', async () => {
const collection = givenTestCollection(`
<testsuites>
Expand Down
31 changes: 31 additions & 0 deletions src/TestCollection/TestCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ export class TestCollection extends BaseTestCollection {
return tests.length > 0 ? tests : undefined;
}

findGroups(): string[] {
const groups = new Set<string>();
for (const [, testData] of this.getTestData()) {
testData.forEach((_, testItem: TestItem) => {
(testItem.tags ?? [])
.filter((tag) => tag.id.startsWith('group:'))
.forEach((tag) => groups.add(tag.id.replace(/^group:/, '')));
});
}

return [...groups].sort();
}

findTestsByGroup(group: string): TestItem[] {
const groupTagId = `group:${group}`;
const tests: TestItem[] = [];
for (const [, testData] of this.getTestData()) {
for (const [testItem, testCase] of testData) {
if (testCase.type !== TestType.method) {
continue;
}

if ((testItem.tags ?? []).some((tag) => tag.id === groupTagId)) {
tests.push(testItem);
}
}
}

return tests;
}

reset() {
for (const [, testData] of this.getTestData()) {
for (const [testItem] of testData) {
Expand Down
33 changes: 29 additions & 4 deletions src/TestExecution/TestRunHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
type CancellationToken,
debug,
type TestController,
type TestItem,
type TestRunProfile,
type TestRun,
type TestRunRequest,
TestRunRequest,
workspace,
} from 'vscode';
import { Configuration } from '../Configuration';
Expand Down Expand Up @@ -52,6 +54,23 @@ export class TestRunHandler {
this.previousRequest = request;
}

async startGroupTestRun(
group: string,
include: readonly TestItem[],
cancellation?: CancellationToken,
profile?: TestRunProfile,
) {
const request = new TestRunRequest(include, undefined, profile);
const builder = await this.createProcessBuilder(request);
const xdebug = builder.getXdebug()!;
builder.setArguments(`--group=${group}`);

await this.manageDebugSession(xdebug, async () => {
const testRun = this.ctrl.createTestRun(request);
await this.runTestQueue(builder, testRun, request, cancellation, true);
});
}

private async createProcessBuilder(request: TestRunRequest): Promise<ProcessBuilder> {
const builder = new ProcessBuilder(this.configuration, { cwd: this.phpUnitXML.root() });
const xdebug = new Xdebug(this.configuration);
Expand Down Expand Up @@ -80,6 +99,7 @@ export class TestRunHandler {
testRun: TestRun,
request: TestRunRequest,
cancellation?: CancellationToken,
forceSingleProcess = false,
) {
const queue = await this.testQueueBuilder.build(
request.include ?? this.testQueueBuilder.collectItems(this.ctrl.items),
Expand All @@ -90,7 +110,7 @@ export class TestRunHandler {
const runner = this.testRunnerBuilder.build(queue, testRun, request);
runner.emit(TestRunnerEvent.start, undefined);

const processes = this.createProcesses(runner, builder, request);
const processes = this.createProcesses(runner, builder, request, forceSingleProcess);
cancellation?.onCancellationRequested(() =>
processes.forEach((process) => process.abort()),
);
Expand All @@ -101,8 +121,13 @@ export class TestRunHandler {
runner.emit(TestRunnerEvent.done, undefined);
}

private createProcesses(runner: TestRunner, builder: ProcessBuilder, request: TestRunRequest) {
if (!request.include) {
private createProcesses(
runner: TestRunner,
builder: ProcessBuilder,
request: TestRunRequest,
forceSingleProcess = false,
) {
if (!request.include || forceSingleProcess) {
return [runner.run(builder)];
}

Expand Down
51 changes: 50 additions & 1 deletion src/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type TestController,
type TestItem,
type TestItemCollection,
TestRunRequest,
TestRunProfileKind,
type TextDocument,
tests,
Expand Down Expand Up @@ -246,11 +247,12 @@ describe('Extension Test', () => {
'phpunit.run-all',
'phpunit.run-file',
'phpunit.run-test-at-cursor',
'phpunit.run-by-group',
'phpunit.rerun',
]) {
expect(commands.registerCommand).toHaveBeenCalledWith(cmd, expect.any(Function));
}
expect(context.subscriptions.push).toHaveBeenCalledTimes(7);
expect(context.subscriptions.push).toHaveBeenCalledTimes(8);
});

it('should only update configuration when phpunit settings change', async () => {
Expand Down Expand Up @@ -368,6 +370,53 @@ describe('Extension Test', () => {
await configuration.update('args', []);
});

it('should run tests by selected group', async () => {
await activate(context);
(window.showQuickPick as Mock).mockResolvedValue('integration');

await commands.executeCommand('phpunit.run-by-group');
const testRunRequestCalls = (TestRunRequest as unknown as Mock).mock.calls;
const [, , profile] = testRunRequestCalls[testRunRequestCalls.length - 1];

expect(window.showQuickPick).toHaveBeenCalledWith(
expect.arrayContaining(['integration']),
expect.objectContaining({
placeHolder: 'Select a PHPUnit group to run',
title: 'Run Tests by Group',
}),
);
expectSpawnCalled([
binary,
'--group=integration',
'--colors=never',
'--teamcity',
]);
expect(profile).toBe(getTestRunProfile(getTestController()));
});

it('should discover tests before running selected group when tests are not preloaded', async () => {
setTextDocuments([]);
await activate(context);
(window.showQuickPick as Mock).mockResolvedValue('integration');

await commands.executeCommand('phpunit.run-by-group');

expect(workspace.findFiles).toHaveBeenCalled();
expect(window.showQuickPick).toHaveBeenCalledWith(
expect.arrayContaining(['integration']),
expect.objectContaining({
placeHolder: 'Select a PHPUnit group to run',
title: 'Run Tests by Group',
}),
);
expectSpawnCalled([
binary,
'--group=integration',
'--colors=never',
'--teamcity',
]);
});

it('should run class with group', async () => {
await activateAndRun({ include: 'Attribute (Tests\\Attribute)' });

Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function registerCommands(
context.subscriptions.push(testCommandRegistry.runAll());
context.subscriptions.push(testCommandRegistry.runFile());
context.subscriptions.push(testCommandRegistry.runTestAtCursor());
context.subscriptions.push(testCommandRegistry.runByGroup());
context.subscriptions.push(testCommandRegistry.rerun());
}

Expand Down