-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEndpointContractTest.php
More file actions
160 lines (151 loc) · 7.3 KB
/
Copy pathEndpointContractTest.php
File metadata and controls
160 lines (151 loc) · 7.3 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
<?php
declare(strict_types=1);
namespace Fleetbase\Sdk\Test\Contract;
use Fleetbase\Sdk\Service;
use Fleetbase\Sdk\Test\TestCase;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use ReflectionClass;
final class EndpointContractTest extends TestCase
{
public function testEveryEndpointContract(): void
{
foreach (self::endpointCases() as $case) {
[$serviceClass, $method, $httpMethod, $urlTemplate, $parameters, $requestFixture] = $case;
$options = [];
$query = self::normalizeFixture($requestFixture['query'] ?? []);
if (is_array($query) && $query !== []) {
$options['query'] = $query;
}
$bodyType = $requestFixture['body_type'] ?? null;
$body = self::normalizeFixture($requestFixture['body'] ?? null);
$expectedBody = null;
$expectedMultipart = [];
if ($bodyType === 'json' && is_array($body)) {
$parameters['body'] = $body;
$expectedBody = $body;
} elseif ($bodyType === 'formdata' && is_array($body)) {
foreach ($body as $part) {
if (!is_array($part) || !is_string($part['key'] ?? null)) {
continue;
}
$normalizedValue = self::normalizeFixture($part['value'] ?? '');
$contents = ($part['type'] ?? null) === 'file'
? 'fixture-file-content'
: (is_scalar($normalizedValue) ? (string) $normalizedValue : '');
$options['multipart'][] = ['name' => $part['key'], 'contents' => $contents];
$expectedMultipart[$part['key']] = $contents;
}
}
$client = $this->mockHttpClient([new Response(200, ['Content-Type' => 'application/json'], '{}')]);
$reflection = new ReflectionClass($serviceClass);
$service = $reflection->newInstance($client);
$service->{$method}($parameters, $options);
$transaction = $this->history[0] ?? null;
self::assertIsArray($transaction);
$request = $transaction['request'] ?? null;
self::assertInstanceOf(RequestInterface::class, $request);
self::assertSame($httpMethod, $request->getMethod());
$expected = preg_replace('#^\{\{base_url\}\}/\{\{namespace\}\}/?#i', '', $urlTemplate);
self::assertIsString($expected);
foreach ($parameters as $name => $value) {
if ($name === 'body') {
continue;
}
if (!is_scalar($value)) {
throw new \RuntimeException('Endpoint path parameters must be scalar.');
}
$expected = str_replace('{{' . $name . '}}', rawurlencode((string) $value), $expected);
$expected = preg_replace('/:' . preg_quote($name, '/') . '(?![A-Za-z0-9_-])/', rawurlencode((string) $value), $expected);
self::assertIsString($expected);
}
if (is_array($query) && $query !== []) {
$expected .= (strpos($expected, '?') === false ? '?' : '&')
. http_build_query($query, '', '&', PHP_QUERY_RFC3986);
}
self::assertSame('/v1/' . ltrim($expected, '/'), $request->getUri()->getPath()
. ($request->getUri()->getQuery() !== '' ? '?' . $request->getUri()->getQuery() : ''));
if (is_array($expectedBody)) {
self::assertSame($expectedBody, json_decode((string) $request->getBody(), true, 512, JSON_THROW_ON_ERROR));
}
foreach ($expectedMultipart as $name => $contents) {
self::assertStringContainsString('name="' . $name . '"', (string) $request->getBody());
self::assertStringContainsString($contents, (string) $request->getBody());
}
}
}
/** @return iterable<string, array{class-string<Service>, string, string, string, array<string, scalar>, array<string, mixed>}> */
private static function endpointCases(): iterable
{
$contents = file_get_contents(dirname(__DIR__, 2) . '/contracts/postman-manifest.json');
if (!is_string($contents)) {
throw new \RuntimeException('Unable to read the endpoint contract manifest.');
}
$manifest = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
if (!is_array($manifest) || !is_array($manifest['requests'] ?? null)) {
throw new \RuntimeException('The endpoint contract manifest is invalid.');
}
foreach ($manifest['requests'] as $request) {
if (!is_array($request)) {
throw new \RuntimeException('The endpoint contract request is invalid.');
}
$id = $request['id'] ?? null;
$implementationName = $request['implementation'] ?? null;
$httpMethod = $request['method'] ?? null;
$url = $request['url'] ?? null;
$requestFixture = $request['request_fixture'] ?? null;
if (!is_string($id) || !is_string($implementationName) || !is_string($httpMethod) || !is_string($url) || !is_array($requestFixture)) {
throw new \RuntimeException('The endpoint contract request fields are invalid.');
}
$implementation = explode('::', $implementationName, 2);
$serviceClass = $implementation[0] ?? '';
$serviceMethod = $implementation[1] ?? '';
if (!class_exists($serviceClass) || !is_subclass_of($serviceClass, Service::class) || !method_exists($serviceClass, $serviceMethod)) {
throw new \RuntimeException('The endpoint contract implementation is invalid.');
}
$normalizedRequestFixture = [];
foreach ($requestFixture as $fixtureKey => $fixtureValue) {
if (is_string($fixtureKey)) {
$normalizedRequestFixture[$fixtureKey] = $fixtureValue;
}
}
$parameters = [];
preg_match_all('/(?:\{\{([^}]+)\}\}|:([A-Za-z][A-Za-z0-9_-]*))/', $url, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$bracedName = $match[1] ?? '';
$colonName = $match[2] ?? '';
$name = $bracedName !== '' ? $bracedName : $colonName;
if (!in_array($name, ['base_url', 'namespace'], true)) {
$parameters[$name] = $name . '-fixture';
}
}
yield $id => [
$serviceClass,
$serviceMethod,
$httpMethod,
$url,
$parameters,
$normalizedRequestFixture,
];
}
}
/**
* @param mixed $value
* @return mixed
*/
private static function normalizeFixture($value)
{
if (is_array($value)) {
foreach ($value as $key => $item) {
$value[$key] = self::normalizeFixture($item);
}
return $value;
}
if (!is_string($value)) {
return $value;
}
return preg_replace_callback('/\{\{([^}]+)\}\}/', static function (array $matches): string {
return trim($matches[1], '$') . '-fixture';
}, $value) ?? $value;
}
}