Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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