Skip to content

Commit b272943

Browse files
committed
Refactored to maintain REST semantics in the fake server.
1 parent c2b3161 commit b272943

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

src/Bypass.php

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public static function serve(...$routes): self
4848

4949
public function stop(): self
5050
{
51-
$url = $this->getBaseUrl("___api_faker_clear_router");
51+
$url = $this->getBaseUrl("___api_faker_router");
5252

5353
\file_get_contents(filename: $url, context: \stream_context_create([
5454
'http' => [
55-
'method' => 'PUT',
55+
'method' => 'DELETE',
5656
'header' => 'Content-Type: application/json',
5757
'content' => '{}'
5858
],
@@ -68,23 +68,13 @@ public function down(): self
6868
}
6969
$status = proc_get_status($this->process);
7070
$pid = $status['pid'] ?? null;
71-
7271
if ($pid) {
73-
if (stripos(PHP_OS_FAMILY, 'Windows') !== false) {
74-
exec("taskkill /F /T /PID {$pid}");
75-
} else {
76-
exec("kill -9 {$pid}");
77-
}
72+
$this->killPid($pid);
7873
}
7974

80-
proc_terminate($this->process);
81-
proc_close($this->process);
82-
$this->process = null;
83-
8475
return $this;
8576
}
86-
87-
77+
8878
public function getPort(): int
8979
{
9080
return $this->port;
@@ -122,7 +112,7 @@ public function handle(?int $port = null): self
122112
$pattern = '/Development Server \(http:\/\/localhost:(?<port>\d+)\) started/';
123113
$start = time();
124114
$timeout = 5;
125-
115+
126116
while (true) {
127117
$chunk = fread($pipes[2], 1024);
128118
if ($chunk !== false && strlen($chunk)) {
@@ -132,48 +122,48 @@ public function handle(?int $port = null): self
132122
break;
133123
}
134124
}
135-
125+
136126
if ((time() - $start) > $timeout) {
137127
proc_terminate($this->process);
138128
proc_close($this->process);
139129
throw new RuntimeException("Server did not start within {$timeout} seconds.");
140130
}
141-
131+
142132
usleep(50);
143133
}
144-
134+
145135
// kill process
146136
pcntl_async_signals(true);
147137
pcntl_signal(SIGINT, fn() => $this->down());
148138
pcntl_signal(SIGTERM, fn() => $this->down());
149-
139+
150140
static $process_registry = [];
151141
$process_registry[] = $this->process;
152-
142+
153143
$status = proc_get_status($this->process);
154144
$wrapper_pid = $status['pid'] ?? null;
155-
145+
156146
register_shutdown_function(function () use ($wrapper_pid) {
157147
if ($wrapper_pid) {
158148
if (stripos(PHP_OS_FAMILY, 'Windows') !== false) {
159149
exec("taskkill /F /T /PID {$wrapper_pid} >nul 2>&1");
160150
} else {
161151
exec("ps -p {$wrapper_pid}", $output, $code);
162-
152+
163153
if ($code === 0) {
164154
exec("pgrep -P {$wrapper_pid}", $child_pids);
165155
foreach ($child_pids as $pid) {
166156
if ($pid) {
167157
exec("kill -9 {$pid} > /dev/null 2>&1");
168158
}
169159
}
170-
160+
171161
exec("kill -9 {$wrapper_pid} > /dev/null 2>&1");
172162
}
173163
}
174164
}
175165
});
176-
166+
177167
return $this;
178168
}
179169

@@ -190,10 +180,10 @@ public function addRoute(
190180
): self {
191181
$body = is_array($body) ? json_encode($body) : $body;
192182

193-
$this->addRouteParams(new Route(
183+
$this->registerRoute(new Route(
194184
method: strtoupper($method),
195185
uri: $uri,
196-
body: $body,
186+
body: $body,
197187
status: $status,
198188
times: $times,
199189
headers: $headers,
@@ -213,7 +203,7 @@ public function addFileRoute(
213203
int $times = 1,
214204
array $headers = []
215205
): self {
216-
$this->addRouteParams(new RouteFile(
206+
$this->registerRoute(new RouteFile(
217207
method: strtoupper($method),
218208
uri: $uri,
219209
file: $file,
@@ -234,15 +224,14 @@ public function getRoutes(): array
234224

235225
public function assertRoutes(): void
236226
{
237-
$url = $this->getBaseUrl("___api_faker_router_index");
227+
$url = $this->getBaseUrl("___api_faker_router");
238228
$routes = [];
239229
foreach ($this->routes as $route) {
240230
$uri = $route->uri;
241231
$method = $route->method;
242232
$path = "{$url}?route={$uri}&method={$method}";
243233
$response = \file_get_contents($path);
244234
$routes[$route->uri] = $response;
245-
246235
$currentTimes = \json_decode($response, true);
247236
$expectedTimes = $route->times;
248237
if ($currentTimes === $expectedTimes) {
@@ -264,27 +253,24 @@ public function expect(
264253
return $this->addRoute($method, $uri, $status, $body, $times, $headers);
265254
}
266255

267-
protected function addRouteParams(Route|RouteFile $route): array
256+
protected function registerRoute(Route|RouteFile $route): array
268257
{
269258
if (!$this->port || !$this->process) {
270259
$this->handle();
271260
}
272-
$url = $this->getBaseUrl("___api_faker_add_router");
273-
261+
$url = $this->getBaseUrl("___api_faker_router");
274262
$this->routes[] = $route;
275263
$params = $route->toArray();
276264
if ($route instanceof RouteFile) {
277265
$params['file'] = base64_encode($params['file']);
278266
}
279-
280267
$response = \file_get_contents(filename: $url, context: \stream_context_create([
281268
'http' => [
282-
'method' => 'PUT',
269+
'method' => 'POST',
283270
'header' => 'Content-Type: application/json',
284271
'content' => json_encode($params),
285272
],
286273
]));
287-
288274
\preg_match('/^HTTP\/.* (\d{3})/', $http_response_header[0] ?? '', $matches);
289275
$status = $matches[1] ?? '';
290276

@@ -298,4 +284,13 @@ public function __toString()
298284
{
299285
return $this->getBaseUrl();
300286
}
287+
288+
private function killPid(int $pid)
289+
{
290+
if (stripos(PHP_OS_FAMILY, 'Windows') !== false) {
291+
exec("taskkill /F /T /PID {$pid}");
292+
} else {
293+
exec("kill -9 {$pid}");
294+
}
295+
}
301296
}

src/server.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
include_once("lib/functions.php");
44

5-
if ($_SERVER['REQUEST_METHOD'] === "GET" && $_SERVER['PHP_SELF'] === '/___api_faker_router_index') {
5+
if ($_SERVER['REQUEST_METHOD'] === "GET" && $_SERVER['PHP_SELF'] === '/___api_faker_router') {
66
$route = getRoute($_GET['route'], $_GET['method']);
77
$route = json_decode($route, true);
88

@@ -11,7 +11,7 @@
1111
exit;
1212
}
1313

14-
if ($_SERVER['REQUEST_METHOD'] === "PUT" && $_SERVER['REQUEST_URI'] === '/___api_faker_clear_router') {
14+
if ($_SERVER['REQUEST_METHOD'] === "DELETE" && $_SERVER['REQUEST_URI'] === '/___api_faker_router') {
1515
$sessionName = getSessionName();
1616

1717
foreach (glob($sessionName . "_*.tmp") as $file) {
@@ -21,7 +21,7 @@
2121
exit;
2222
}
2323

24-
if ($_SERVER['REQUEST_METHOD'] === "PUT" && $_SERVER['REQUEST_URI'] === '/___api_faker_add_router') {
24+
if ($_SERVER['REQUEST_METHOD'] === "POST" && $_SERVER['REQUEST_URI'] === '/___api_faker_router') {
2525
$inputs = file_get_contents("php://input");
2626
$router = json_decode($inputs, true);
2727

0 commit comments

Comments
 (0)