Repository navigation
Expand file tree
/
Copy pathTestHelper.php
More file actions
68 lines (58 loc) · 1.93 KB
/
Copy pathTestHelper.php
File metadata and controls
68 lines (58 loc) · 1.93 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
<?php
declare(strict_types=1);
// @oagen-ignore-file
namespace WorkOS;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
trait TestHelper
{
private ?MockHandler $mockHandler = null;
private array $requestHistory = [];
protected function loadFixture(string $name): array
{
$path = __DIR__ . '/Fixtures/' . $name . '.json';
if (!file_exists($path)) {
$this->markTestSkipped("Fixture not found: {$name}.json");
}
return json_decode(file_get_contents($path), true);
}
protected function createMockClient(
array $responses,
string $apiKey = 'test_api_key',
?string $clientId = 'test_client_id',
string $baseUrl = 'https://api.workos.com',
int $maxRetries = 3,
?string $userAgent = null,
): WorkOS {
$mockResponses = array_map(
fn (array $response) => new Response(
$response['status'] ?? 200,
$response['headers'] ?? [],
json_encode($response['body'] ?? [])
),
$responses,
);
$this->mockHandler = new MockHandler($mockResponses);
$handler = HandlerStack::create($this->mockHandler);
$this->requestHistory = [];
$handler->push(Middleware::history($this->requestHistory));
return new WorkOS(
apiKey: $apiKey,
clientId: $clientId,
baseUrl: $baseUrl,
maxRetries: $maxRetries,
handler: $handler,
userAgent: $userAgent,
);
}
protected function getLastRequest(): \Psr\Http\Message\RequestInterface
{
return $this->requestHistory[array_key_last($this->requestHistory)]['request'];
}
protected function getLastRequestOptions(): array
{
return $this->requestHistory[array_key_last($this->requestHistory)]['options'];
}
}