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 PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
trait AssertRequestAgainstSchema |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Schema |
19
|
|
|
*/ |
20
|
|
|
protected $schema; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* configure the schema to use for requests |
24
|
|
|
* |
25
|
|
|
* When set, all requests without an own schema use this one instead. |
26
|
|
|
* |
27
|
|
|
* @param Schema|null $schema |
28
|
|
|
*/ |
29
|
|
|
public function setSchema($schema) |
30
|
|
|
{ |
31
|
|
|
$this->schema = $schema; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc |
36
|
|
|
* @param string $path The REST path call |
37
|
|
|
* @param int $statusExpected |
38
|
|
|
* @param array|null $query |
39
|
|
|
* @param array|null $requestBody |
40
|
|
|
* @param array $requestHeader |
41
|
|
|
* @return mixed |
42
|
|
|
* @throws DefinitionNotFoundException |
43
|
|
|
* @throws GenericSwaggerException |
44
|
|
|
* @throws HttpMethodNotFoundException |
45
|
|
|
* @throws InvalidDefinitionException |
46
|
|
|
* @throws NotMatchedException |
47
|
|
|
* @throws PathNotFoundException |
48
|
|
|
* @throws StatusCodeNotMatchedException |
49
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
50
|
|
|
* @deprecated Use assertRequest instead |
51
|
|
|
*/ |
52
|
|
|
protected function makeRequest( |
53
|
|
|
$method, |
54
|
|
|
$path, |
55
|
|
|
$statusExpected = 200, |
56
|
|
|
$query = null, |
57
|
|
|
$requestBody = null, |
58
|
|
|
$requestHeader = [] |
59
|
|
|
) { |
60
|
|
|
assert($this instanceof TestCase); |
61
|
|
|
|
62
|
|
|
$this->checkSchema(); |
|
|
|
|
63
|
|
|
$requester = new ApiRequester(); |
64
|
|
|
$body = $requester |
65
|
|
|
->withSchema($this->schema) |
|
|
|
|
66
|
|
|
->withMethod($method) |
67
|
|
|
->withPath($path) |
68
|
|
|
->withQuery($query) |
|
|
|
|
69
|
|
|
->withRequestBody($requestBody) |
|
|
|
|
70
|
|
|
->withRequestHeader($requestHeader) |
71
|
|
|
->assertResponseCode($statusExpected) |
72
|
|
|
->send(); |
73
|
|
|
|
74
|
|
|
// Note: |
75
|
|
|
// This code is only reached if the send is successful and |
76
|
|
|
// all matches are satisfied. Otherwise an error is throwed before |
77
|
|
|
// reach this |
78
|
|
|
$this->assertTrue(true); |
79
|
|
|
|
80
|
|
|
return $body; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param AbstractRequester $request |
85
|
|
|
* @return mixed |
86
|
|
|
* @throws DefinitionNotFoundException |
87
|
|
|
* @throws GenericSwaggerException |
88
|
|
|
* @throws HttpMethodNotFoundException |
89
|
|
|
* @throws InvalidDefinitionException |
90
|
|
|
* @throws NotMatchedException |
91
|
|
|
* @throws PathNotFoundException |
92
|
|
|
* @throws StatusCodeNotMatchedException |
93
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
94
|
|
|
*/ |
95
|
|
|
public function assertRequest(AbstractRequester $request) |
96
|
|
|
{ |
97
|
|
|
assert($this instanceof TestCase); |
98
|
|
|
|
99
|
|
|
// Add own schema if nothing is passed. |
100
|
|
|
if (!$request->hasSchema()) { |
101
|
|
|
$this->checkSchema(); |
|
|
|
|
102
|
|
|
$request->withSchema($this->schema); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Request based on the Swagger Request definitios |
106
|
|
|
$body = $request->send(); |
107
|
|
|
|
108
|
|
|
// Note: |
109
|
|
|
// This code is only reached if the send is successful and |
110
|
|
|
// all matches are satisfied. Otherwise an error is throwed before |
111
|
|
|
// reach this |
112
|
|
|
$this->assertTrue(true); |
113
|
|
|
|
114
|
|
|
return $body; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @throws GenericSwaggerException |
119
|
|
|
*/ |
120
|
|
|
protected function checkSchema() |
121
|
|
|
{ |
122
|
|
|
if (!$this->schema) { |
123
|
|
|
throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase'); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: