Skip to content

Commit e9d12a6

Browse files
authored
Merge pull request #75 from byjg/5.0.0
Major upgrade
2 parents 4ec04d0 + bfbc666 commit e9d12a6

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

.github/workflows/phpunit.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ on:
88
pull_request:
99
branches:
1010
- master
11-
schedule:
12-
- cron: "0 8 * * 1"
1311

1412
jobs:
1513
Build:
@@ -18,10 +16,10 @@ jobs:
1816
strategy:
1917
matrix:
2018
php-version:
19+
- "8.2"
2120
- "8.1"
2221
- "8.0"
2322
- "7.4"
24-
- "7.3"
2523

2624
steps:
2725
- uses: actions/checkout@v3

README.md

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
[![GitHub release](https://img.shields.io/github/release/byjg/php-swagger-test.svg)](https://github.com/byjg/php-swagger-test/releases/)
88
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/php-swagger-test/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/php-swagger-test/?branch=master)
99

10-
A set of tools for testing your REST calls based on the OpenApi specification using PHPUnit.
10+
A set of tools for testing your REST calls based on the OpenApi specification using PHPUnit.
1111
Currently, this library supports the OpenApi specifications `2.0` (formerly swagger) and `3.0`.
12-
Some features like callbacks, links and references to external documents/objects weren't implemented.
12+
Some features like callbacks, links and references to external documents/objects weren't implemented.
1313

1414
PHP Swagger Test can help you to test your REST Api. You can use this tool both for Unit Tests or Functional Tests.
1515

16-
This tool reads a previously Swagger JSON file (not YAML) and enables you to test the request and response.
17-
You can use the tool "https://github.com/zircote/swagger-php" for creating the JSON file when you are developing your
18-
rest API.
16+
This tool reads a previously Swagger JSON file (not YAML) and enables you to test the request and response.
17+
You can use the tool "<https://github.com/zircote/swagger-php>" for creating the JSON file when you are developing your
18+
rest API.
1919

2020
The ApiTestCase's assertion process is based on throwing exceptions if some validation or test failed.
2121

@@ -30,7 +30,7 @@ You can use the Swagger Test library as:
3030

3131
### Functional Test cases
3232

33-
Swagger Test provides the class `SwaggerTestCase` for you extend and create a PHPUnit test case. The code will try to
33+
Swagger Test provides the class `SwaggerTestCase` for you extend and create a PHPUnit test case. The code will try to
3434
make a request to your API Method and check if the request parameters, status and object returned are OK.
3535

3636
```php
@@ -45,7 +45,7 @@ class MyTestCase extends \ByJG\ApiTools\ApiTestCase
4545
$schema = \ByJG\ApiTools\Base\Schema::getInstance(file_get_contents('/path/to/json/definition'));
4646
$this->setSchema($schema);
4747
}
48-
48+
4949
/**
5050
* Test if the REST address /path/for/get/ID with the method GET returns what is
5151
* documented on the "swagger.json"
@@ -75,7 +75,7 @@ class MyTestCase extends \ByJG\ApiTools\ApiTestCase
7575
}
7676

7777
/**
78-
* Test if the REST address /path/for/post/ID with the method POST
78+
* Test if the REST address /path/for/post/ID with the method POST
7979
* and the request object ['name'=>'new name', 'field' => 'value'] will return an object
8080
* as is documented in the "swagger.json" file
8181
*/
@@ -91,7 +91,7 @@ class MyTestCase extends \ByJG\ApiTools\ApiTestCase
9191
}
9292

9393
/**
94-
* Test if the REST address /another/path/for/post/{id} with the method POST
94+
* Test if the REST address /another/path/for/post/{id} with the method POST
9595
* and the request object ['name'=>'new name', 'field' => 'value'] will return an object
9696
* as is documented in the "swagger.json" file
9797
*/
@@ -140,21 +140,21 @@ class MyAppRequester extends ByJG\ApiTools\AbstractRequester
140140
You now use an instance of this class in place of the `ApiRequester` class from the examples above. Of course, if you need to apply changes to the request, or the response in order
141141
to fit your framework, this is exactly the right place to do it.
142142

143-
@todo: Explain in the Docs sections the `RestServer` component
143+
@todo: Explain in the Docs sections the `RestServer` component
144144

145145
### Using it as Unit Test cases
146146

147-
If you want mock the request API and just test the expected parameters, you are sending and
147+
If you want mock the request API and just test the expected parameters, you are sending and
148148
receiving you have to:
149149

150-
**1. Create the Swagger or OpenAPI Test Schema**
150+
#### 1. Create the Swagger or OpenAPI Test Schema
151151

152152
```php
153153
<?php
154154
$schema = \ByJG\ApiTools\Base\Schema::getInstance($contentsOfSchemaJson);
155155
```
156156

157-
**2. Get the definitions for your path**
157+
#### 2. Get the definitions for your path
158158

159159
```php
160160
<?php
@@ -169,7 +169,7 @@ $bodyRequestDef = $schema->getRequestParameters($path, $method);
169169
$bodyResponseDef = $schema->getResponseParameters($path, $method, $statusExpected);
170170
```
171171

172-
**3. Match the result**
172+
#### 3. Match the result
173173

174174
```php
175175
<?php
@@ -180,12 +180,12 @@ $bodyResponseDef->match($responseBody);
180180
```
181181

182182
If the request or response body does not match with the definition an exception NotMatchException will
183-
be thrown.
183+
be thrown.
184184

185185
### Runtime parameters validator
186186

187187
This tool was not developed only for unit and functional tests. You can use to validate if the required body
188-
parameters is the expected.
188+
parameters is the expected.
189189

190190
So, before your API Code you can validate the request body using:
191191

@@ -201,7 +201,7 @@ $bodyRequestDef->match($requestBody);
201201
PHP Swagger has the class `MockRequester` with exact the same functionalities of `ApiRequester` class. The only
202202
difference is the `MockRequester` don't need to request to a real endpoint.
203203

204-
This is used to validate request and response against your OpenAPI spec without running any server code.
204+
This is used to validate request and response against your OpenAPI spec without running any server code.
205205

206206
```php
207207
<?php
@@ -217,7 +217,7 @@ class MyTest extends ApiTestCase
217217
])));
218218

219219
// The MockRequester does not send the request to a real endpoint
220-
// Just returning the expected Response object sent in the constructor
220+
// Just returning the expected Response object sent in the constructor
221221
$request = new \ByJG\ApiTools\MockRequester($expectedResponse);
222222
$request
223223
->withMethod('GET')
@@ -226,13 +226,13 @@ class MyTest extends ApiTestCase
226226
$this->assertRequest($request); // That should be "True" based on the specification
227227
}
228228
}
229-
```
229+
```
230230

231231
## Integration with PSR7
232232

233233
You can populate the `ApiRequester`/`MockRequester` with the information provided by the `RequestInterface` PSR7 interface.
234234

235-
e.g.
235+
e.g.
236236

237237
```php
238238
<?php
@@ -244,14 +244,22 @@ $psr7Request = \ByJG\Util\Psr7\Request::getInstance(new Uri("/method_to_be_teste
244244
$request = new \ByJG\ApiTools\ApiRequester();
245245
$request->withPsr7Request($psr7Request);
246246

247-
// Return a ResponseInterface PSR7 component
247+
// Return a ResponseInterface PSR7 component
248248
$response = $request->send();
249249
```
250250

251251
## Install
252252

253253
```bash
254-
composer require "byjg/swagger-test=3.1.*"
254+
composer require "byjg/swagger-test=4.9.*"
255+
```
256+
257+
## Tests
258+
259+
```bash
260+
SPEC=swagger php -S 127.0.0.1:8080 tests/rest/app.php &
261+
SPEC=openapi php -S 127.0.0.1:8081 tests/rest/app.php &
262+
vendor/bin/phpunit
255263
```
256264

257265
## Questions?
@@ -260,9 +268,15 @@ Please raise your issue on [Github issue](https://github.com/byjg/php-swagger-te
260268

261269
## References
262270

263-
This project uses the [byjg/webrequest](https://github.com/byjg/webrequest) component.
264-
It implements the PSR-7 specification, and a HttpClient / MockClient to do the requests.
265-
Check it out to get more information.
271+
This project uses the [byjg/webrequest](https://github.com/byjg/webrequest) component.
272+
It implements the PSR-7 specification, and a HttpClient / MockClient to do the requests.
273+
Check it out to get more information.
266274

275+
## Dependencies
276+
277+
```mermaid
278+
flowchart TD
279+
byjg/swagger-test --> byjg/webrequest
280+
```
267281
----
268282
[Open source ByJG](http://opensource.byjg.com)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"license": "MIT",
77
"require": {
88
"byjg/webrequest": "4.9.*",
9-
"php": ">=5.6",
9+
"php": ">=7.4",
1010
"ext-json": "*"
1111
},
1212
"require-dev": {

0 commit comments

Comments
 (0)