MediaWiki master
SplitGroupExecutor.php
Go to the documentation of this file.
1<?php
2
3declare( strict_types = 1 );
4
6
7use Composer\Script\Event;
10use Shellbox\Command\UnboxedExecutor;
11use Shellbox\Command\UnboxedResult;
12
17
18 private UnboxedExecutor $executor;
19 private ?Event $event;
20 private ComposerSystemInterface $composerSystemInterface;
21
22 public function __construct(
23 UnboxedExecutor $shellExecutor,
24 ?Event $event,
25 ?ComposerSystemInterface $composerSystemInterface
26 ) {
27 $this->executor = $shellExecutor;
28 $this->event = $event;
29 $this->composerSystemInterface = $composerSystemInterface ?? new ComposerSystemInterface();
30 }
31
32 public function executeSplitGroup(
33 string $testSuite,
34 array $groups,
35 array $excludeGroups,
36 ?string $resultsCacheFile = null,
37 ?int $groupId = null
38 ): UnboxedResult {
39 $command = $this->executor->createCommand()
40 ->params(
41 'composer', 'run',
42 '--timeout=0',
43 'phpunit:entrypoint',
44 '--',
45 '--testsuite', $testSuite,
46 '--exclude-group', implode( ",", $excludeGroups )
47 );
48 if ( count( $groups ) ) {
49 $command->params( '--group', implode( ',', $groups ) );
50 }
51 if ( $resultsCacheFile ) {
52 $command->params(
53 "--cache-result-file=$resultsCacheFile"
54 );
55 }
56 $command->includeStderr( true );
57 if ( $groupId !== null ) {
58 $command->environment( [ 'MW_PHPUNIT_SPLIT_GROUP_ID' => $groupId ] );
59 }
60 $this->consoleLog( "Running command '" . $command->getCommandString() . "' ..." . PHP_EOL );
61 return $command->execute();
62 }
63
64 private function warning( string $warning ) {
65 if ( $this->event ) {
66 $this->event->getIO()->warning( $warning );
67 }
68 }
69
70 private function composerLog( string $text ) {
71 if ( $this->event ) {
72 $this->event->getIO()->write( $text );
73 }
74 }
75
76 private function consoleLog( string $outputText ) {
77 $this->composerSystemInterface->print( $outputText );
78 }
79
80 public function runLinearFallback( string $testSuite ) {
81 $this->warning( "Test suite splitting failed - falling back to linear run" );
82 $this->composerLog( "Running " . $testSuite . " phpunit suite databaseless tests..." );
83 $databaselessResult = $this->executeSplitGroup(
84 $testSuite,
87 );
88 $this->consoleLog( $databaselessResult->getStdout() );
89 if ( $databaselessResult->getExitCode() !== 0 ) {
90 return;
91 }
92 $this->composerLog( "Running " . $testSuite . " phpunit suite database tests..." );
93 $databaseResult = $this->executeSplitGroup(
94 $testSuite,
97 );
98 $this->consoleLog( $databaseResult->getStdout() );
99 }
100
101}
Launch PHPUnit test suites in parallel.
Wrapper around low-level system functions, so that we can inject mocks when testing the MediaWiki\Com...
__construct(UnboxedExecutor $shellExecutor, ?Event $event, ?ComposerSystemInterface $composerSystemInterface)
executeSplitGroup(string $testSuite, array $groups, array $excludeGroups, ?string $resultsCacheFile=null, ?int $groupId=null)