Skip to content

Commit f067f62

Browse files
committed
feat(tests): add mock web server behat context
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 8d424a9 commit f067f62

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
use Behat\Hook\AfterScenario;
11+
use donatj\MockWebServer\MockWebServer;
12+
use donatj\MockWebServer\RequestInfo;
13+
use Libresign\NextcloudBehat\NextcloudApiContext;
14+
15+
class MockWebServerContext extends NextcloudApiContext {
16+
/** @var array<string, MockWebServer> */
17+
private array $mockServers = [];
18+
19+
/**
20+
* @Given /^the mock web server "([^"]*)" is started$/
21+
*/
22+
public function theMockWebServerIsStarted(string $serverName): void {
23+
if (isset($this->mockServers[$serverName]) && $this->mockServers[$serverName]->isRunning()) {
24+
return;
25+
}
26+
27+
$server = new MockWebServer();
28+
$server->start();
29+
$this->mockServers[$serverName] = $server;
30+
}
31+
32+
/**
33+
* @Given /^save the mock web server "([^"]*)" root URL as "([^"]*)"$/
34+
*/
35+
public function saveTheMockWebServerRootUrlAs(string $serverName, string $fieldName): void {
36+
$this->fields[$fieldName] = $this->getMockServer($serverName)->getServerRoot();
37+
}
38+
39+
/**
40+
* @When /^read the last request from mock web server "([^"]*)"$/
41+
*/
42+
public function readTheLastRequestFromMockWebServer(string $serverName): void {
43+
$request = $this->getLastRequest($serverName);
44+
self::$commandOutput = json_encode($request, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
45+
}
46+
47+
/**
48+
* @When /^read the last request body from mock web server "([^"]*)"$/
49+
*/
50+
public function readTheLastRequestBodyFromMockWebServer(string $serverName): void {
51+
$input = $this->getLastRequest($serverName)->getInput();
52+
53+
try {
54+
$decoded = json_decode($input, true, 512, JSON_THROW_ON_ERROR);
55+
self::$commandOutput = json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
56+
} catch (JsonException) {
57+
self::$commandOutput = $input;
58+
}
59+
}
60+
61+
#[AfterScenario()]
62+
public function stopMockWebServers(): void {
63+
foreach ($this->mockServers as $server) {
64+
if ($server->isRunning()) {
65+
$server->stop();
66+
}
67+
}
68+
69+
$this->mockServers = [];
70+
}
71+
72+
protected function beforeRequest(string $fullUrl, array $options): array {
73+
[$fullUrl, $options] = parent::beforeRequest($fullUrl, $options);
74+
75+
if (isset($options['body']) && is_string($options['body'])) {
76+
$options['body'] = $this->parseText($options['body']);
77+
}
78+
79+
return [$fullUrl, $options];
80+
}
81+
82+
private function getMockServer(string $serverName): MockWebServer {
83+
if (!isset($this->mockServers[$serverName])) {
84+
throw new RuntimeException('Mock web server "' . $serverName . '" is not started');
85+
}
86+
87+
return $this->mockServers[$serverName];
88+
}
89+
90+
private function getLastRequest(string $serverName): RequestInfo {
91+
$request = $this->getMockServer($serverName)->getLastRequest();
92+
if (!$request instanceof RequestInfo) {
93+
throw new RuntimeException('Mock web server "' . $serverName . '" has not received any request yet');
94+
}
95+
96+
return $request;
97+
}
98+
}

0 commit comments

Comments
 (0)