/
RunsInParallel.php
188 lines (159 loc) · 4.98 KB
/
RunsInParallel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Illuminate\Testing\Concerns;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\ParallelTesting;
use Illuminate\Testing\ParallelConsoleOutput;
use PHPUnit\TextUI\Configuration\PhpHandler;
use RuntimeException;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
trait RunsInParallel
{
/**
* The application resolver callback.
*
* @var \Closure|null
*/
protected static $applicationResolver;
/**
* The runner resolver callback.
*
* @var \Closure|null
*/
protected static $runnerResolver;
/**
* The original test runner options.
*
* @var \ParaTest\Runners\PHPUnit\Options|\ParaTest\Options
*/
protected $options;
/**
* The output instance.
*
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;
/**
* The original test runner.
*
* @var \ParaTest\Runners\PHPUnit\RunnerInterface|\ParaTest\RunnerInterface
*/
protected $runner;
/**
* Creates a new test runner instance.
*
* @param \ParaTest\Runners\PHPUnit\Options|\ParaTest\Options $options
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
public function __construct($options, OutputInterface $output)
{
$this->options = $options;
if ($output instanceof ConsoleOutput) {
$output = new ParallelConsoleOutput($output);
}
$runnerResolver = static::$runnerResolver ?: function ($options, OutputInterface $output) {
$wrapperRunnerClass = class_exists(\ParaTest\WrapperRunner\WrapperRunner::class)
? \ParaTest\WrapperRunner\WrapperRunner::class
: \ParaTest\Runners\PHPUnit\WrapperRunner::class;
return new $wrapperRunnerClass($options, $output);
};
$this->runner = $runnerResolver($options, $output);
}
/**
* Set the application resolver callback.
*
* @param \Closure|null $resolver
* @return void
*/
public static function resolveApplicationUsing($resolver)
{
static::$applicationResolver = $resolver;
}
/**
* Set the runner resolver callback.
*
* @param \Closure|null $resolver
* @return void
*/
public static function resolveRunnerUsing($resolver)
{
static::$runnerResolver = $resolver;
}
/**
* Runs the test suite.
*
* @return int
*/
public function execute(): int
{
$configuration = $this->options instanceof \ParaTest\Options
? $this->options->configuration
: $this->options->configuration();
(new PhpHandler())->handle($configuration->php());
$this->forEachProcess(function () {
ParallelTesting::callSetUpProcessCallbacks();
});
try {
$potentialExitCode = $this->runner->run();
} finally {
$this->forEachProcess(function () {
ParallelTesting::callTearDownProcessCallbacks();
});
}
return $potentialExitCode ?? $this->getExitCode();
}
/**
* Returns the highest exit code encountered throughout the course of test execution.
*
* @return int
*/
public function getExitCode(): int
{
return $this->runner->getExitCode();
}
/**
* Apply the given callback for each process.
*
* @param callable $callback
* @return void
*/
protected function forEachProcess($callback)
{
$processes = $this->options instanceof \ParaTest\Options
? $this->options->processes
: $this->options->processes();
collect(range(1, $processes))->each(function ($token) use ($callback) {
tap($this->createApplication(), function ($app) use ($callback, $token) {
ParallelTesting::resolveTokenUsing(fn () => $token);
$callback($app);
})->flush();
});
}
/**
* Creates the application.
*
* @return \Illuminate\Contracts\Foundation\Application
*
* @throws \RuntimeException
*/
protected function createApplication()
{
$applicationResolver = static::$applicationResolver ?: function () {
if (trait_exists(\Tests\CreatesApplication::class)) {
$applicationCreator = new class
{
use \Tests\CreatesApplication;
};
return $applicationCreator->createApplication();
} elseif (file_exists($path = (Application::inferBasePath().'/bootstrap/app.php'))) {
$app = require $path;
$app->make(Kernel::class)->bootstrap();
return $app;
}
throw new RuntimeException('Parallel Runner unable to resolve application.');
};
return $applicationResolver();
}
}