|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByJG\ApiTools; |
|
4
|
|
|
|
|
5
|
|
|
use ByJG\ApiTools\Exception\DefinitionNotFoundException; |
|
6
|
|
|
use ByJG\ApiTools\Exception\GenericApiException; |
|
7
|
|
|
use ByJG\ApiTools\Exception\HttpMethodNotFoundException; |
|
8
|
|
|
use ByJG\ApiTools\Exception\InvalidDefinitionException; |
|
9
|
|
|
use ByJG\ApiTools\Exception\InvalidRequestException; |
|
10
|
|
|
use ByJG\ApiTools\Exception\NotMatchedException; |
|
11
|
|
|
use ByJG\ApiTools\Exception\PathNotFoundException; |
|
12
|
|
|
use ByJG\ApiTools\Exception\RequiredArgumentNotFound; |
|
13
|
|
|
use ByJG\ApiTools\Exception\StatusCodeNotMatchedException; |
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Base test case for OpenAPI/Swagger validation. |
|
19
|
|
|
* |
|
20
|
|
|
* This class extends PHPUnit's TestCase and uses the OpenApiValidation trait |
|
21
|
|
|
* to provide OpenAPI/Swagger validation capabilities. |
|
22
|
|
|
* |
|
23
|
|
|
* If you need to extend a different base class, you can use the OpenApiValidation |
|
24
|
|
|
* trait directly instead of extending this class. |
|
25
|
|
|
* |
|
26
|
|
|
* @see OpenApiValidation |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class ApiTestCase extends TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
use OpenApiValidation; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Legacy method for backward compatibility. |
|
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 string|array|null $query |
|
39
|
|
|
* @param array|string|null $requestBody |
|
40
|
|
|
* @param array $requestHeader |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
* @throws DefinitionNotFoundException |
|
43
|
|
|
* @throws GenericApiException |
|
44
|
|
|
* @throws HttpMethodNotFoundException |
|
45
|
|
|
* @throws InvalidDefinitionException |
|
46
|
|
|
* @throws InvalidRequestException |
|
47
|
|
|
* @throws NotMatchedException |
|
48
|
|
|
* @throws PathNotFoundException |
|
49
|
|
|
* @throws RequiredArgumentNotFound |
|
50
|
|
|
* @throws StatusCodeNotMatchedException |
|
51
|
|
|
* @deprecated Since version 6.0, use sendRequest() with ApiRequester fluent interface instead. Will be removed in version 7.0 |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function makeRequest( |
|
54
|
|
|
string $method, |
|
55
|
|
|
string $path, |
|
56
|
|
|
int $statusExpected = 200, |
|
57
|
|
|
string|array|null $query = null, |
|
58
|
|
|
array|string|null $requestBody = null, |
|
59
|
|
|
array $requestHeader = [] |
|
60
|
|
|
): ResponseInterface { |
|
61
|
|
|
$this->checkSchema(); |
|
62
|
|
|
return $this->getRequester() |
|
63
|
|
|
->withSchema($this->schema) |
|
|
|
|
|
|
64
|
|
|
->withMethod($method) |
|
65
|
|
|
->withPath($path) |
|
66
|
|
|
->withQuery($query) |
|
67
|
|
|
->withRequestBody($requestBody) |
|
|
|
|
|
|
68
|
|
|
->withRequestHeader($requestHeader) |
|
69
|
|
|
->expectStatus($statusExpected) |
|
70
|
|
|
->send(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|