Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ActionMethodApiCallExpectingStatusCodes
95.24% covered (success)
95.24%
20 / 21
66.67% covered (warning)
66.67%
2 / 3
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRequest
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 verifyValidResponse
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Apie\IntegrationTests\Requests;
4
5use Apie\Core\BoundedContext\BoundedContextId;
6use Apie\IntegrationTests\Requests\JsonFields\JsonGetFieldInterface;
7use Apie\IntegrationTests\Requests\JsonFields\JsonSetFieldInterface;
8use Nyholm\Psr7\ServerRequest;
9use PHPUnit\Framework\TestCase;
10use Psr\Http\Message\ResponseInterface;
11use Psr\Http\Message\ServerRequestInterface;
12
13/**
14 * Variant of ActionMethodApiCall used to verify that a request results in one of a set
15 * of expected (error) status codes instead of the hardcoded 200 the parent class expects.
16 *
17 * Supports a configurable HTTP method so it can also be used to verify a GET request on a
18 * non existing resource results in a 404 instead of only verifying action (POST) calls.
19 */
20class ActionMethodApiCallExpectingStatusCodes extends ActionMethodApiCall
21{
22    /**
23     * @param array<int, int> $expectedStatusCodes
24     */
25    public function __construct(
26        BoundedContextId $boundedContextId,
27        string $url,
28        JsonGetFieldInterface|JsonSetFieldInterface $inputOutput,
29        private readonly array $expectedStatusCodes,
30        private readonly string $method = 'POST',
31    ) {
32        parent::__construct($boundedContextId, $url, $inputOutput);
33    }
34
35    public function getRequest(): ServerRequestInterface
36    {
37        if ($this->method === 'POST') {
38            return parent::getRequest();
39        }
40        return new ServerRequest(
41            $this->method,
42            'http://localhost/api/' . $this->boundedContextId . '/' . $this->url,
43            [
44                'accept' => 'application/json',
45            ]
46        );
47    }
48
49    public function verifyValidResponse(ResponseInterface $response): void
50    {
51        $body = (string) $response->getBody();
52        $statusCode = $response->getStatusCode();
53        // the faker datalayer always returns a fake result, so authorization/not-found checks can not be guaranteed
54        $expectedStatusCodes = $this->isFakeDatalayer()
55            ? [...$this->expectedStatusCodes, 200]
56            : $this->expectedStatusCodes;
57        TestCase::assertContains(
58            $statusCode,
59            $expectedStatusCodes,
60            'Expected one of status codes [' . implode(', ', $expectedStatusCodes) . '], got ' . $statusCode . ': ' . $body
61        );
62        TestCase::assertEquals('application/json', $response->getHeaderLine('content-type'));
63    }
64}