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\NotMatchedException; |
||
11 | use ByJG\ApiTools\Exception\PathNotFoundException; |
||
12 | use ByJG\ApiTools\Exception\StatusCodeNotMatchedException; |
||
13 | use ByJG\Util\Psr7\MessageException; |
||
0 ignored issues
–
show
|
|||
14 | use ByJG\Util\Psr7\Response; |
||
15 | use PHPUnit\Framework\TestCase; |
||
16 | |||
17 | abstract class ApiTestCase extends TestCase |
||
18 | { |
||
19 | /** |
||
20 | * @var Schema |
||
21 | */ |
||
22 | protected $schema; |
||
23 | |||
24 | /** |
||
25 | * @var AbstractRequester |
||
26 | */ |
||
27 | protected $requester = null; |
||
28 | |||
29 | /** |
||
30 | * configure the schema to use for requests |
||
31 | * |
||
32 | * When set, all requests without an own schema use this one instead. |
||
33 | * |
||
34 | * @param Schema|null $schema |
||
35 | */ |
||
36 | public function setSchema($schema) |
||
37 | { |
||
38 | $this->schema = $schema; |
||
39 | } |
||
40 | |||
41 | public function setRequester(AbstractRequester $requester) |
||
42 | { |
||
43 | $this->requester = $requester; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return AbstractRequester |
||
48 | */ |
||
49 | protected function getRequester() |
||
50 | { |
||
51 | if (is_null($this->requester)) { |
||
52 | $this->requester = new ApiRequester(); |
||
53 | } |
||
54 | return $this->requester; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc |
||
59 | * @param string $path The REST path call |
||
60 | * @param int $statusExpected |
||
61 | * @param array|null $query |
||
62 | * @param array|null $requestBody |
||
63 | * @param array $requestHeader |
||
64 | * @return mixed |
||
65 | * @throws DefinitionNotFoundException |
||
66 | * @throws GenericSwaggerException |
||
67 | * @throws HttpMethodNotFoundException |
||
68 | * @throws InvalidDefinitionException |
||
69 | * @throws NotMatchedException |
||
70 | * @throws PathNotFoundException |
||
71 | * @throws StatusCodeNotMatchedException |
||
72 | * @throws MessageException |
||
73 | * @deprecated Use assertRequest instead |
||
74 | */ |
||
75 | protected function makeRequest( |
||
76 | $method, |
||
77 | $path, |
||
78 | $statusExpected = 200, |
||
79 | $query = null, |
||
80 | $requestBody = null, |
||
81 | $requestHeader = [] |
||
82 | ) { |
||
83 | $this->checkSchema(); |
||
84 | $body = $this->requester |
||
85 | ->withSchema($this->schema) |
||
86 | ->withMethod($method) |
||
87 | ->withPath($path) |
||
88 | ->withQuery($query) |
||
89 | ->withRequestBody($requestBody) |
||
90 | ->withRequestHeader($requestHeader) |
||
91 | ->assertResponseCode($statusExpected) |
||
92 | ->send(); |
||
93 | |||
94 | // Note: |
||
95 | // This code is only reached if the send is successful and |
||
96 | // all matches are satisfied. Otherwise an error is throwed before |
||
97 | // reach this |
||
98 | $this->assertTrue(true); |
||
99 | |||
100 | return $body; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param AbstractRequester $request |
||
105 | * @return Response |
||
106 | * @throws DefinitionNotFoundException |
||
107 | * @throws GenericSwaggerException |
||
108 | * @throws HttpMethodNotFoundException |
||
109 | * @throws InvalidDefinitionException |
||
110 | * @throws NotMatchedException |
||
111 | * @throws PathNotFoundException |
||
112 | * @throws StatusCodeNotMatchedException |
||
113 | * @throws MessageException |
||
114 | */ |
||
115 | public function assertRequest(AbstractRequester $request) |
||
116 | { |
||
117 | // Add own schema if nothing is passed. |
||
118 | if (!$request->hasSchema()) { |
||
119 | $this->checkSchema(); |
||
120 | $request = $request->withSchema($this->schema); |
||
121 | } |
||
122 | |||
123 | // Request based on the Swagger Request definitios |
||
124 | $body = $request->send(); |
||
125 | |||
126 | // Note: |
||
127 | // This code is only reached if the send is successful and |
||
128 | // all matches are satisfied. Otherwise an error is throwed before |
||
129 | // reach this |
||
130 | $this->assertTrue(true); |
||
131 | |||
132 | return $body; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @throws GenericSwaggerException |
||
137 | */ |
||
138 | protected function checkSchema() |
||
139 | { |
||
140 | if (!$this->schema) { |
||
141 | throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase'); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths