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