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