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.
1111Currently, 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
1414PHP 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
2020The 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
3434make 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
140140You 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
141141to 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
148148receiving 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
182182If 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
187187This 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
190190So, before your API Code you can validate the request body using:
191191
@@ -201,7 +201,7 @@ $bodyRequestDef->match($requestBody);
201201PHP Swagger has the class ` MockRequester ` with exact the same functionalities of ` ApiRequester ` class. The only
202202difference 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
233233You 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 )
0 commit comments