-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApiTestCase.php
More file actions
90 lines (74 loc) · 2.87 KB
/
ApiTestCase.php
File metadata and controls
90 lines (74 loc) · 2.87 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
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\TestUtils\ApiTest;
use Fig\Http\Message\RequestMethodInterface;
use Fig\Http\Message\StatusCodeInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use Shlinkio\Shlink\TestUtils\Exception\MissingDependencyException;
use Shlinkio\Shlink\TestUtils\Helper\CoverageHelper;
use Shlinkio\Shlink\TestUtils\Helper\SeededTestCase;
use function Shlinkio\Shlink\Json\json_decode;
use function sprintf;
use function str_starts_with;
abstract class ApiTestCase extends SeededTestCase implements StatusCodeInterface, RequestMethodInterface
{
private const REST_PATH_PREFIX = '/rest/v2';
private static ?ClientInterface $client = null;
public static function setApiClient(ClientInterface $client): void
{
self::$client = $client;
}
final protected function callApi(string $method, string $uri, array $options = []): ResponseInterface
{
$uri = str_starts_with($uri, '/rest') ? $uri : sprintf('%s%s', self::REST_PATH_PREFIX, $uri);
return $this->requestWithCoverageId($method, $uri, $options);
}
final protected function callApiWithKey(
string $method,
string $uri,
array $options = [],
string $apiKey = 'valid_api_key',
): ResponseInterface {
$options = $this->optionsWithHeader($options, 'X-Api-Key', $apiKey);
return $this->callApi($method, $uri, $options);
}
final protected function getJsonResponsePayload(ResponseInterface $resp): array
{
$body = (string) $resp->getBody();
return json_decode($body);
}
final protected function callShortUrl(string $shortCode, ?string $userAgent = null): ResponseInterface
{
return $this->requestWithCoverageId(self::METHOD_GET, sprintf('/%s', $shortCode), [
RequestOptions::ALLOW_REDIRECTS => false,
RequestOptions::HEADERS => [
'User-Agent' => $userAgent,
],
]);
}
private function requestWithCoverageId(string $method, string $uri, array $options): ResponseInterface
{
$coverageId = CoverageHelper::resolveCoverageId(static::class, $this->dataName());
return self::getClient()->request(
$method,
$uri,
$this->optionsWithHeader($options, CoverageMiddleware::COVERAGE_ID_HEADER, $coverageId),
);
}
private static function getClient(): ClientInterface
{
if (self::$client === null) {
throw MissingDependencyException::forApiClient();
}
return self::$client;
}
private function optionsWithHeader(array $options, string $header, string $value): array
{
$headers = $options[RequestOptions::HEADERS] ?? [];
$headers[$header] = $value;
$options[RequestOptions::HEADERS] = $headers;
return $options;
}
}