Completed
Push — master ( e7c5cf...05695e )
by Alejandro
17s
created

ApiTestCase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getJsonResponsePayload() 0 3 1
A callApiWithKey() 0 7 1
A callApi() 0 3 1
A setApiClient() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Common\ApiTest;
5
6
use Fig\Http\Message\RequestMethodInterface;
7
use Fig\Http\Message\StatusCodeInterface;
8
use GuzzleHttp\ClientInterface;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\ResponseInterface;
11
use Shlinkio\Shlink\Rest\Authentication\Plugin\ApiKeyHeaderPlugin;
12
use function Shlinkio\Shlink\Common\json_decode;
13
use function sprintf;
14
15
abstract class ApiTestCase extends TestCase implements StatusCodeInterface, RequestMethodInterface
16
{
17
    private const PATH_PREFX = '/rest/v1';
18
19
    /** @var ClientInterface */
20
    private static $client;
21
22
    public static function setApiClient(ClientInterface $client): void
23
    {
24
        self::$client = $client;
25
    }
26
27
    /**
28
     * @throws \GuzzleHttp\Exception\GuzzleException
29
     */
30
    protected function callApi(string $method, string $uri, array $options = []): ResponseInterface
31
    {
32
        return self::$client->request($method, sprintf('%s%s', self::PATH_PREFX, $uri), $options);
33
    }
34
35
    /**
36
     * @throws \GuzzleHttp\Exception\GuzzleException
37
     */
38
    protected function callApiWithKey(string $method, string $uri, array $options = []): ResponseInterface
39
    {
40
        $headers = $options['headers'] ?? [];
41
        $headers[ApiKeyHeaderPlugin::HEADER_NAME] = 'valid_api_key';
42
        $options['headers'] = $headers;
43
44
        return $this->callApi($method, $uri, $options);
45
    }
46
47
    protected function getJsonResponsePayload(ResponseInterface $resp): array
48
    {
49
        return json_decode((string) $resp->getBody());
50
    }
51
}
52