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\HttpMethodNotFoundException; |
8
|
|
|
use ByJG\ApiTools\Exception\InvalidDefinitionException; |
9
|
|
|
use ByJG\ApiTools\Exception\NotMatchedException; |
10
|
|
|
use ByJG\ApiTools\Exception\PathNotFoundException; |
11
|
|
|
use ByJG\ApiTools\Exception\StatusCodeNotMatchedException; |
12
|
|
|
use ByJG\ApiTools\Exception\GenericSwaggerException; |
13
|
|
|
use GuzzleHttp\GuzzleException; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
|
16
|
|
|
abstract class ApiTestCase extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Schema |
20
|
|
|
*/ |
21
|
|
|
protected $swaggerSchema; |
22
|
|
|
|
23
|
|
|
protected $filePath; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws GenericSwaggerException |
27
|
|
|
*/ |
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
if (empty($this->filePath)) { |
31
|
|
|
throw new GenericSwaggerException('You have to define the property $filePath'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->swaggerSchema = Schema::getInstance(file_get_contents($this->filePath)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc |
39
|
|
|
* @param string $path The REST path call |
40
|
|
|
* @param int $statusExpected |
41
|
|
|
* @param array|null $query |
42
|
|
|
* @param array|null $requestBody |
43
|
|
|
* @param array $requestHeader |
44
|
|
|
* @return mixed |
45
|
|
|
* @throws DefinitionNotFoundException |
46
|
|
|
* @throws HttpMethodNotFoundException |
47
|
|
|
* @throws InvalidDefinitionException |
48
|
|
|
* @throws NotMatchedException |
49
|
|
|
* @throws PathNotFoundException |
50
|
|
|
* @throws StatusCodeNotMatchedException |
51
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
52
|
|
|
* @deprecated Use assertRequest instead |
53
|
|
|
*/ |
54
|
|
|
protected function makeRequest( |
55
|
|
|
$method, |
56
|
|
|
$path, |
57
|
|
|
$statusExpected = 200, |
58
|
|
|
$query = null, |
59
|
|
|
$requestBody = null, |
60
|
|
|
$requestHeader = [] |
61
|
|
|
) { |
62
|
|
|
$requester = new ApiRequester(); |
63
|
|
|
$body = $requester |
64
|
|
|
->withSwaggerSchema($this->swaggerSchema) |
65
|
|
|
->withMethod($method) |
66
|
|
|
->withPath($path) |
67
|
|
|
->withQuery($query) |
|
|
|
|
68
|
|
|
->withRequestBody($requestBody) |
|
|
|
|
69
|
|
|
->withRequestHeader($requestHeader) |
70
|
|
|
->assertResponseCode($statusExpected) |
71
|
|
|
->send(); |
72
|
|
|
|
73
|
|
|
// Note: |
74
|
|
|
// This code is only reached if the send is successful and |
75
|
|
|
// all matches are satisfied. Otherwise an error is throwed before |
76
|
|
|
// reach this |
77
|
|
|
$this->assertTrue(true); |
78
|
|
|
|
79
|
|
|
return $body; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ApiRequester $request |
84
|
|
|
* @return mixed |
85
|
|
|
* @throws DefinitionNotFoundException |
86
|
|
|
* @throws HttpMethodNotFoundException |
87
|
|
|
* @throws InvalidDefinitionException |
88
|
|
|
* @throws NotMatchedException |
89
|
|
|
* @throws PathNotFoundException |
90
|
|
|
* @throws StatusCodeNotMatchedException |
91
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
92
|
|
|
*/ |
93
|
|
|
public function assertRequest(ApiRequester $request) |
94
|
|
|
{ |
95
|
|
|
// Add own swagger if nothing is passed. |
96
|
|
|
if (!$request->hasSwaggerSchema()) { |
97
|
|
|
$request->withSwaggerSchema($this->swaggerSchema); |
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
|
|
|
|
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.