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
|
|
|
|
14
|
|
|
trait AssertRequestAgainstSchema |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Schema |
18
|
|
|
*/ |
19
|
|
|
protected $schema; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* configure the schema to use for requests |
23
|
|
|
* |
24
|
|
|
* When set, all requests without an own schema use this one instead. |
25
|
|
|
* |
26
|
|
|
* @param Schema|null $schema |
27
|
|
|
*/ |
28
|
|
|
public function setSchema($schema) |
29
|
|
|
{ |
30
|
|
|
$this->schema = $schema; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc |
35
|
|
|
* @param string $path The REST path call |
36
|
|
|
* @param int $statusExpected |
37
|
|
|
* @param array|null $query |
38
|
|
|
* @param array|null $requestBody |
39
|
|
|
* @param array $requestHeader |
40
|
|
|
* @return mixed |
41
|
|
|
* @throws DefinitionNotFoundException |
42
|
|
|
* @throws GenericSwaggerException |
43
|
|
|
* @throws HttpMethodNotFoundException |
44
|
|
|
* @throws InvalidDefinitionException |
45
|
|
|
* @throws NotMatchedException |
46
|
|
|
* @throws PathNotFoundException |
47
|
|
|
* @throws StatusCodeNotMatchedException |
48
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
49
|
|
|
* @deprecated Use assertRequest instead |
50
|
|
|
*/ |
51
|
|
|
protected function makeRequest( |
52
|
|
|
$method, |
53
|
|
|
$path, |
54
|
|
|
$statusExpected = 200, |
55
|
|
|
$query = null, |
56
|
|
|
$requestBody = null, |
57
|
|
|
$requestHeader = [] |
58
|
|
|
) { |
59
|
|
|
$this->checkSchema(); |
60
|
|
|
$requester = new ApiRequester(); |
61
|
|
|
$body = $requester |
62
|
|
|
->withSchema($this->schema) |
63
|
|
|
->withMethod($method) |
64
|
|
|
->withPath($path) |
65
|
|
|
->withQuery($query) |
|
|
|
|
66
|
|
|
->withRequestBody($requestBody) |
|
|
|
|
67
|
|
|
->withRequestHeader($requestHeader) |
68
|
|
|
->assertResponseCode($statusExpected) |
69
|
|
|
->send(); |
70
|
|
|
|
71
|
|
|
// Note: |
72
|
|
|
// This code is only reached if the send is successful and |
73
|
|
|
// all matches are satisfied. Otherwise an error is throwed before |
74
|
|
|
// reach this |
75
|
|
|
$this->assertTrue(true); |
|
|
|
|
76
|
|
|
|
77
|
|
|
return $body; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param AbstractRequester $request |
82
|
|
|
* @return mixed |
83
|
|
|
* @throws DefinitionNotFoundException |
84
|
|
|
* @throws GenericSwaggerException |
85
|
|
|
* @throws HttpMethodNotFoundException |
86
|
|
|
* @throws InvalidDefinitionException |
87
|
|
|
* @throws NotMatchedException |
88
|
|
|
* @throws PathNotFoundException |
89
|
|
|
* @throws StatusCodeNotMatchedException |
90
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
91
|
|
|
*/ |
92
|
|
|
public function assertRequest(AbstractRequester $request) |
93
|
|
|
{ |
94
|
|
|
// Add own schema if nothing is passed. |
95
|
|
|
if (!$request->hasSchema()) { |
96
|
|
|
$this->checkSchema(); |
97
|
|
|
$request->withSchema($this->schema); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Request based on the Swagger Request definitios |
101
|
|
|
$body = $request->send(); |
102
|
|
|
|
103
|
|
|
// Note: |
104
|
|
|
// This code is only reached if the send is successful and |
105
|
|
|
// all matches are satisfied. Otherwise an error is throwed before |
106
|
|
|
// reach this |
107
|
|
|
$this->assertTrue(true); |
|
|
|
|
108
|
|
|
|
109
|
|
|
return $body; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @throws GenericSwaggerException |
114
|
|
|
*/ |
115
|
|
|
protected function checkSchema() |
116
|
|
|
{ |
117
|
|
|
if (!$this->schema) { |
118
|
|
|
throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.