1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ByJG\Swagger; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use GuzzleHttp\Psr7\Request; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
// backward compatibility |
11
|
|
|
if (!class_exists('\PHPUnit\Framework\TestCase')) { |
12
|
|
|
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase'); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
abstract class SwaggerTestCase extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \ByJG\Swagger\SwaggerSchema |
19
|
|
|
*/ |
20
|
|
|
protected $swaggerSchema; |
21
|
|
|
|
22
|
|
|
protected $filePath; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @throws \Exception |
26
|
|
|
*/ |
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
if (empty($this->filePath)) { |
30
|
|
|
throw new \Exception('You have to define the property $filePath'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->swaggerSchema = new SwaggerSchema(file_get_contents($this->filePath)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc |
38
|
|
|
* @param string $path The REST path call |
39
|
|
|
* @param int $statusExpected |
40
|
|
|
* @param array|null $query |
41
|
|
|
* @param array|null $requestBody |
42
|
|
|
* @param array $requestHeader |
43
|
|
|
* @return mixed |
44
|
|
|
* @deprecated Use assertRequest instead |
45
|
|
|
* @throws \ByJG\Swagger\Exception\HttpMethodNotFoundException |
46
|
|
|
* @throws \ByJG\Swagger\Exception\InvalidDefinitionException |
47
|
|
|
* @throws \ByJG\Swagger\Exception\NotMatchedException |
48
|
|
|
* @throws \ByJG\Swagger\Exception\PathNotFoundException |
49
|
|
|
* @throws \ByJG\Swagger\Exception\RequiredArgumentNotFound |
50
|
|
|
* @throws \Exception |
51
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
52
|
|
|
*/ |
53
|
|
|
protected function makeRequest( |
54
|
|
|
$method, |
55
|
|
|
$path, |
56
|
|
|
$statusExpected = 200, |
57
|
|
|
$query = null, |
58
|
|
|
$requestBody = null, |
59
|
|
|
$requestHeader = [] |
60
|
|
|
) { |
61
|
|
|
$requester = new SwaggerRequester(); |
62
|
|
|
$body = $requester |
63
|
|
|
->withSwaggerSchema($this->swaggerSchema) |
64
|
|
|
->withMethod($method) |
65
|
|
|
->withPath($path) |
66
|
|
|
->withQuery($query) |
|
|
|
|
67
|
|
|
->withRequestBody($requestBody) |
|
|
|
|
68
|
|
|
->withRequestHeader($requestHeader) |
69
|
|
|
->assertResponseCode($statusExpected) |
70
|
|
|
->send(); |
71
|
|
|
|
72
|
|
|
// Note: |
73
|
|
|
// This code is only reached if the send is successful and |
74
|
|
|
// all matches are satisfied. Otherwise an error is throwed before |
75
|
|
|
// reach this; |
76
|
|
|
$this->assertTrue(true); |
77
|
|
|
|
78
|
|
|
return $body; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \ByJG\Swagger\SwaggerRequester $request |
83
|
|
|
* @return mixed |
84
|
|
|
* @throws \ByJG\Swagger\Exception\HttpMethodNotFoundException |
85
|
|
|
* @throws \ByJG\Swagger\Exception\InvalidDefinitionException |
86
|
|
|
* @throws \ByJG\Swagger\Exception\NotMatchedException |
87
|
|
|
* @throws \ByJG\Swagger\Exception\PathNotFoundException |
88
|
|
|
* @throws \ByJG\Swagger\Exception\RequiredArgumentNotFound |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
public function assertRequest(SwaggerRequester $request) |
92
|
|
|
{ |
93
|
|
|
// Request based on the Swagger Request definitios |
94
|
|
|
$body = $request |
95
|
|
|
->withSwaggerSchema($this->swaggerSchema) |
96
|
|
|
->send(); |
97
|
|
|
|
98
|
|
|
// Note: |
99
|
|
|
// This code is only reached if the send is successful and |
100
|
|
|
// all matches are satisfied. Otherwise an error is throwed before |
101
|
|
|
// reach this; |
102
|
|
|
$this->assertTrue(true); |
103
|
|
|
|
104
|
|
|
return $body; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.