Skip to content

Commit 39b128f

Browse files
committed
arrayToRequestString function for mocking requests
1 parent 97dbce8 commit 39b128f

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/VerifierServer/Server.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,21 @@ public function handleResource($client): null
295295
return null;
296296
}
297297

298+
/**
299+
* Converts an associative array into a request string format.
300+
* Useful for forging requests in tests or applications that require internal request generation.
301+
*
302+
* Each key-value pair in the array is transformed into a string
303+
* in the format "key: value" and concatenated with a newline character.
304+
*
305+
* @param array $formData The associative array to be converted.
306+
* @return string The resulting request string.
307+
*/
308+
public static function arrayToRequestString(array $formData): string
309+
{
310+
return implode(PHP_EOL, array_map(fn($key, $value) => $key . ': ' . $value, array_keys($formData), $formData));
311+
}
312+
298313
/**
299314
* Destructor method that is automatically called when the object is destroyed.
300315
* It ensures that the server is properly stopped by calling the stop() method.

tests/VerifiedEndpointTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use React\Http\Message\Response;
44
use PHPUnit\Framework\TestCase;
55
use VerifierServer\Endpoints\VerifiedEndpoint;
6+
use VerifierServer\Server;
67
use VerifierServer\PersistentState;
78

89
class VerifiedEndpointTest extends TestCase {
@@ -55,13 +56,11 @@ public function testPost() {
5556
* @param array $formData The associative array to be converted.
5657
* @return string The reconstructed string.
5758
*/
58-
$arrayToString = static fn(array $formData): string
59-
=> implode(PHP_EOL, array_map(fn($key, $value) => $key . ': ' . $value, array_keys($formData), $formData));
6059
$response = 0;
6160
$content_type = [];
6261
$body = "";
6362
$bypass_token = true;
64-
$this->endpoint->handle($method, $arrayToString($formData), $response, $content_type, $body, $bypass_token);
63+
$this->endpoint->handle($method, Server::arrayToRequestString($formData), $response, $content_type, $body, $bypass_token);
6564

6665
//$this->assertArrayHasKey($list, $this->state->getVerifyList(true));
6766
$this->assertStringContainsString((string) Response::STATUS_OK, (string) $response);

0 commit comments

Comments
 (0)