-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathPlaywrightNpmServer.php
More file actions
175 lines (146 loc) · 4.75 KB
/
PlaywrightNpmServer.php
File metadata and controls
175 lines (146 loc) · 4.75 KB
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
<?php
declare(strict_types=1);
namespace Pest\Browser\Playwright\Servers;
use Pest\Browser\Contracts\PlaywrightServer;
use Pest\Browser\Exceptions\PlaywrightNotInstalledException;
use Pest\Browser\Exceptions\PlaywrightOutdatedException;
use Pest\Browser\Playwright\Playwright;
use RuntimeException;
use Symfony\Component\Process\Process as SystemProcess;
/**
* @internal
*
* @codeCoverageIgnore This class is used at plugin level to manage processes.
*/
final class PlaywrightNpmServer implements PlaywrightServer
{
/**
* The playwright version required to run this server.
*/
private const string PLAYWRIGHT_VERSION = '1.54.1';
/**
* The underlying process instance, if any.
*/
private ?SystemProcess $systemProcess = null;
/**
* Creates a new playwright npm server instance.
*/
private function __construct(
public readonly string $baseDirectory,
public readonly string $command,
public readonly string $host,
public readonly int $port,
public readonly string $until,
) {
//
}
/**
* Creates a new playwright npm server instance with the given parameters.
*/
public static function create(string $baseDirectory, string $command, string $host, int $port, string $until): self
{
return new self(
$baseDirectory, $command, $host, $port, $until
);
}
/**
* Starts the process until the given "output" condition is met.
*/
public function start(): void
{
if ($this->isRunning()) {
return;
}
$this->systemProcess = SystemProcess::fromShellCommandline(sprintf(
$this->command,
$this->host,
$this->port,
), $this->baseDirectory, [
'APP_URL' => sprintf('http://%s:%d', $this->host, $this->port),
]);
$this->systemProcess->setTimeout(0);
$this->systemProcess->start();
$this->systemProcess->waitUntil(
fn (string $type, string $output): bool => str_contains($output, $this->until)
);
if ($this->isRunning() === false) {
self::ensurePlaywrightIsInstalledAndVersionIsSupported();
throw new PlaywrightNotInstalledException();
}
AlreadyStartedPlaywrightServer::persist(
$this->host,
$this->port,
);
}
/**
* Stops the process if it is running.
*/
public function stop(): void
{
if ($this->systemProcess instanceof SystemProcess && $this->isRunning()) {
$this->systemProcess->stop(timeout: 0.1);
}
$this->systemProcess = null;
AlreadyStartedPlaywrightServer::markAsStopped();
}
/**
* Flushes the process.
*/
public function flush(): void
{
//
}
/**
* Returns the URL of the process.
*
* @throws RuntimeException If the process has not been started yet or has stopped unexpectedly.
*/
public function url(): string
{
if (! $this->isRunning()) {
throw new RuntimeException(
sprintf('The process with arguments [%s] is not running or has stopped unexpectedly.', json_encode([
'baseDirectory' => $this->baseDirectory,
'command' => $this->command,
'host' => $this->host,
'port' => $this->port,
'until' => $this->until,
]),
));
}
return sprintf('%s:%d', $this->host, $this->port);
}
/**
* Ensures that Playwright is installed and the version is supported.
*
* @throws PlaywrightNotInstalledException
*/
private function ensurePlaywrightIsInstalledAndVersionIsSupported(): void
{
$process = SystemProcess::fromShellCommandline(
'.'.DIRECTORY_SEPARATOR.'node_modules'.DIRECTORY_SEPARATOR.'.bin'.DIRECTORY_SEPARATOR.'playwright run-server --version',
$this->baseDirectory,
);
$process->run();
if (! $process->isSuccessful()) {
throw new PlaywrightNotInstalledException();
}
$output = $process->getOutput();
// check if the output matches the required version
if (in_array(preg_match('/^Version\s+(\d+\.\d+\.\d+)/', $output, $matches), [0, false], true)) {
throw new PlaywrightNotInstalledException();
}
$version = $matches[1];
if (version_compare($version, self::PLAYWRIGHT_VERSION, '<')) {
throw new PlaywrightOutdatedException();
}
}
/**
* Checks
*/
private function isRunning(): bool
{
return $this->systemProcess instanceof SystemProcess
&& $this->systemProcess->isRunning();
}
}