ApiTestCase::setRequester()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ByJG\ApiTools;
4
5
use ByJG\ApiTools\Base\Schema;
6
use ByJG\ApiTools\Exception\DefinitionNotFoundException;
7
use ByJG\ApiTools\Exception\GenericSwaggerException;
8
use ByJG\ApiTools\Exception\HttpMethodNotFoundException;
9
use ByJG\ApiTools\Exception\InvalidDefinitionException;
10
use ByJG\ApiTools\Exception\InvalidRequestException;
11
use ByJG\ApiTools\Exception\NotMatchedException;
12
use ByJG\ApiTools\Exception\PathNotFoundException;
13
use ByJG\ApiTools\Exception\RequiredArgumentNotFound;
14
use ByJG\ApiTools\Exception\StatusCodeNotMatchedException;
15
use ByJG\WebRequest\Psr7\Response;
16
use PHPUnit\Framework\TestCase;
17
use Psr\Http\Message\ResponseInterface;
18
19
abstract class ApiTestCase extends TestCase
20
{
21
    /**
22
     * @var Schema|null
23
     */
24
    protected ?Schema $schema = null;
25
26
    /**
27
     * @var AbstractRequester|null
28
     */
29
    protected ?AbstractRequester $requester = null;
30
31
    /**
32
     * configure the schema to use for requests
33
     *
34
     * When set, all requests without an own schema use this one instead.
35
     *
36
     * @param Schema|null $schema
37
     */
38
    public function setSchema(?Schema $schema): void
39
    {
40
        $this->schema = $schema;
41
    }
42
43
    public function setRequester(AbstractRequester $requester): void
44
    {
45
        $this->requester = $requester;
46
    }
47
48
    /**
49
     * @return AbstractRequester|null
50
     */
51
    protected function getRequester(): AbstractRequester|null
52
    {
53
        if (is_null($this->requester)) {
54
            $this->requester = new ApiRequester();
55
        }
56
        return $this->requester;
57
    }
58
59
    /**
60
     * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
61
     * @param string $path The REST path call
62
     * @param int $statusExpected
63
     * @param string|array|null $query
64
     * @param array|string|null $requestBody
65
     * @param array $requestHeader
66
     * @return mixed
67
     * @throws DefinitionNotFoundException
68
     * @throws GenericSwaggerException
69
     * @throws HttpMethodNotFoundException
70
     * @throws InvalidDefinitionException
71
     * @throws InvalidRequestException
72
     * @throws NotMatchedException
73
     * @throws PathNotFoundException
74
     * @throws RequiredArgumentNotFound
75
     * @throws StatusCodeNotMatchedException
76
     * @deprecated Use assertRequest instead
77
     */
78
    protected function makeRequest(
79
        string $method,
80
        string $path,
81
        int $statusExpected = 200,
82
        string|array|null $query = null,
83
        array|string $requestBody = null,
84
        array $requestHeader = []
85
    ): ResponseInterface {
86
        $this->checkSchema();
87
        $body = $this->requester
88
            ->withSchema($this->schema)
0 ignored issues
show
Bug introduced by
The method withSchema() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            ->/** @scrutinizer ignore-call */ withSchema($this->schema)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
It seems like $this->schema can also be of type null; however, parameter $schema of ByJG\ApiTools\AbstractRequester::withSchema() does only seem to accept ByJG\ApiTools\Base\Schema, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
            ->withSchema(/** @scrutinizer ignore-type */ $this->schema)
Loading history...
89
            ->withMethod($method)
90
            ->withPath($path)
91
            ->withQuery($query)
92
            ->withRequestBody($requestBody)
0 ignored issues
show
Bug introduced by
It seems like $requestBody can also be of type null; however, parameter $requestBody of ByJG\ApiTools\AbstractRequester::withRequestBody() does only seem to accept array|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            ->withRequestBody(/** @scrutinizer ignore-type */ $requestBody)
Loading history...
93
            ->withRequestHeader($requestHeader)
94
            ->assertResponseCode($statusExpected)
95
            ->send();
96
97
        // Note:
98
        // This code is only reached if to send is successful and
99
        // all matches are satisfied. Otherwise, an error is throwed before
100
        // reach this
101
        $this->assertTrue(true);
102
103
        return $body;
104
    }
105
106
    /**
107
     * @param AbstractRequester $request
108
     * @return Response
109
     * @throws DefinitionNotFoundException
110
     * @throws GenericSwaggerException
111
     * @throws HttpMethodNotFoundException
112
     * @throws InvalidDefinitionException
113
     * @throws InvalidRequestException
114
     * @throws NotMatchedException
115
     * @throws PathNotFoundException
116
     * @throws RequiredArgumentNotFound
117
     * @throws StatusCodeNotMatchedException
118
     */
119
    public function assertRequest(AbstractRequester $request): ResponseInterface
120
    {
121
        // Add own schema if nothing is passed.
122
        if (!$request->hasSchema()) {
123
            $this->checkSchema();
124
            $request = $request->withSchema($this->schema);
0 ignored issues
show
Bug introduced by
It seems like $this->schema can also be of type null; however, parameter $schema of ByJG\ApiTools\AbstractRequester::withSchema() does only seem to accept ByJG\ApiTools\Base\Schema, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
            $request = $request->withSchema(/** @scrutinizer ignore-type */ $this->schema);
Loading history...
125
        }
126
127
        // Request based on the Swagger Request definitios
128
        $body = $request->send();
129
130
        // Note:
131
        // This code is only reached if to send is successful and
132
        // all matches are satisfied. Otherwise, an error is throwed before
133
        // reach this
134
        $this->assertTrue(true);
135
136
        return $body;
137
    }
138
139
    /**
140
     * @throws GenericSwaggerException
141
     */
142
    protected function checkSchema(): void
143
    {
144
        if (!$this->schema) {
145
            throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase');
146
        }
147
    }
148
}
149