Skip to content

Commit

Permalink
Merge pull request #74 from byjg/bump
Browse files Browse the repository at this point in the history
Bump Major Components Changes
  • Loading branch information
byjg authored May 21, 2023
2 parents fcc7134 + cb73a7e commit 8025692
Show file tree
Hide file tree
Showing 23 changed files with 312 additions and 119 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: PHPUnit
on:
push:
branches:
- master
tags:
- "*.*.*"
pull_request:
branches:
- master
schedule:
- cron: "0 8 * * 1"

jobs:
Build:
runs-on: 'ubuntu-latest'
container: 'byjg/php:${{ matrix.php-version }}-cli'
strategy:
matrix:
php-version:
- "8.1"
- "8.0"
- "7.4"
- "7.3"

steps:
- uses: actions/checkout@v3
- name: Setup test
run: |
composer install
SPEC=swagger php -S 127.0.0.1:8080 tests/rest/app.php &
SPEC=openapi php -S 127.0.0.1:8081 tests/rest/app.php &
- run: ./vendor/bin/phpunit

Documentation:
runs-on: 'ubuntu-latest'
needs: Build
if: github.ref == 'refs/heads/master'
env:
DOC_GITHUB_TOKEN: '${{ secrets.DOC_TOKEN }}'
steps:
- uses: actions/checkout@v3
- run: curl https://opensource.byjg.com/add-doc.sh | bash /dev/stdin php php-swagger-test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ composer.lock
/sonar-scanner-3.2.0.1227-linux/
/phpunit.coverage.xml
/phpunit.report.xml
.phpunit.result.cache
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: php
php:
- nightly
- "8.0"
- "7.4"
- "7.3"
- "7.2"
- "7.1"
- "7.0"
- "5.6"

install:
- composer install
Expand Down
74 changes: 74 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Swagger Test Server",
"type": "php",
"request": "launch",
"env": {
"SPEC": "swagger"
},
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:8080",
"tests/rest/app.php"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003
},
{
"name": "Launch OpenAPI Test Server",
"type": "php",
"request": "launch",
"env": {
"SPEC": "openapi"
},
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:8081",
"tests/rest/app.php"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9004
},
{
"name": "Debug current Script in Console",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "PHPUnit Debug",
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/vendor/bin/phpunit",
"cwd": "${workspaceFolder}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
}
]
}
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# PHP Swagger Test

[![Build Status](https://github.com/byjg/php-swagger-test/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-swagger-test/actions/workflows/phpunit.yml)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-swagger-test/)
[![GitHub license](https://img.shields.io/github/license/byjg/php-swagger-test.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/php-swagger-test.svg)](https://github.com/byjg/php-swagger-test/releases/)
[![Build Status](https://travis-ci.com/byjg/php-swagger-test.svg?branch=master)](https://travis-ci.com/byjg/php-swagger-test)
[![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)


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

Expand All @@ -29,7 +28,6 @@ You can use the Swagger Test library as:
- Runtime parameters validator
- Validate your specification


### Functional Test cases

Swagger Test provides the class `SwaggerTestCase` for you extend and create a PHPUnit test case. The code will try to
Expand All @@ -42,7 +40,7 @@ make a request to your API Method and check if the request parameters, status an
*/
class MyTestCase extends \ByJG\ApiTools\ApiTestCase
{
public function setUp()
public function setUp(): void
{
$schema = \ByJG\ApiTools\Base\Schema::getInstance(file_get_contents('/path/to/json/definition'));
$this->setSchema($schema);
Expand Down Expand Up @@ -138,6 +136,7 @@ class MyAppRequester extends ByJG\ApiTools\AbstractRequester
}
}
```

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
to fit your framework, this is exactly the right place to do it.

Expand Down Expand Up @@ -211,7 +210,7 @@ class MyTest extends ApiTestCase
public function testExpectOK()
{
$expectedResponse = \ByJG\Util\Psr7\Response::getInstance(200)
->withBody(new MemoryStream(json_encode([
->withBody(new \ByJG\Util\Psr7\MemoryStream(json_encode([
"id" => 1,
"name" => "Spike",
"photoUrls" => []
Expand Down Expand Up @@ -249,8 +248,6 @@ $request->withPsr7Request($psr7Request);
$response = $request->send();
```



## Install

```bash
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"prefer-stable": true,
"license": "MIT",
"require": {
"byjg/webrequest": "^2.0.2",
"byjg/webrequest": "4.9.*",
"php": ">=5.6",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^5.7|^7.4",
"byjg/restserver": "4.0.*"
"phpunit/phpunit": "5.7.*|7.4.*|^9.5",
"byjg/restserver": "4.9.*"
},
"autoload": {
"psr-4": {
Expand Down
10 changes: 9 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ and open the template in the editor.

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
colors="false"
colors="true"
testdox="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
stopOnFailure="false">

<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>

<filter>
<whitelist>
<directory>./src</directory>
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* The baseclass provides processing and verification of request and response.
* It only delegates the actual message exchange to the derived class. For the
* messages, it uses the PSR-7 implementation from Guzzle.
* messages, it uses the PHP PSR-7 implementation.
*
* This is an implementation of the Template Method Patttern
* (https://en.wikipedia.org/wiki/Template_method_pattern).
Expand Down
4 changes: 4 additions & 0 deletions src/Base/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ protected function matchString($name, $schemaArray, $body, $type)
$this->checkPattern($name, $body, $schemaArray['pattern']);
}

if (!is_string($body)) {
throw new NotMatchedException("Value '" . var_export($body, true) . "' in '$name' is not string. ", $this->structure);
}

return true;
}

Expand Down
30 changes: 18 additions & 12 deletions tests/AbstractRequesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ public function testExpectOK()
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
* @throws \ByJG\ApiTools\Exception\StatusCodeNotMatchedException
* @throws \ByJG\Util\Psr7\MessageException
* @expectedException \ByJG\ApiTools\Exception\NotMatchedException
* @expectedExceptionMessage Required property 'name'
*/
public function testExpectError()
{
$this->expectException(\ByJG\ApiTools\Exception\NotMatchedException::class);
$this->expectExceptionMessage("Required property 'name'");

$expectedResponse = Response::getInstance(200)
->withBody(new MemoryStream(json_encode([
"id" => 1,
Expand Down Expand Up @@ -126,11 +127,12 @@ public function testValidateAssertResponse404()
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
* @throws \ByJG\ApiTools\Exception\StatusCodeNotMatchedException
* @throws \ByJG\Util\Psr7\MessageException
* @expectedException \ByJG\ApiTools\Exception\NotMatchedException
* @expectedExceptionMessage Expected empty body for GET 404 /v2/pet/1
*/
public function testValidateAssertResponse404WithContent()
{
$this->expectException(\ByJG\ApiTools\Exception\NotMatchedException::class);
$this->expectExceptionMessage("Expected empty body for GET 404 /v2/pet/1");

$expectedResponse = Response::getInstance(404)
->withBody(new MemoryStream('{"error":"not found"}'));

Expand All @@ -152,11 +154,12 @@ public function testValidateAssertResponse404WithContent()
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
* @throws \ByJG\ApiTools\Exception\StatusCodeNotMatchedException
* @throws \ByJG\Util\Psr7\MessageException
* @expectedException \ByJG\ApiTools\Exception\StatusCodeNotMatchedException
* @expectedExceptionMessage Status code not matched: Expected 404, got 522
*/
public function testValidateAssertResponseNotExpected()
{
$this->expectException(\ByJG\ApiTools\Exception\StatusCodeNotMatchedException::class);
$this->expectExceptionMessage("Status code not matched: Expected 404, got 522");

$expectedResponse = Response::getInstance(522);

$request = new MockRequester($expectedResponse);
Expand Down Expand Up @@ -207,11 +210,12 @@ public function testValidateAssertHeaderContains()
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
* @throws \ByJG\ApiTools\Exception\StatusCodeNotMatchedException
* @throws \ByJG\Util\Psr7\MessageException
* @expectedException \ByJG\ApiTools\Exception\NotMatchedException
* @expectedExceptionMessage Does not exists header 'X-Test' with value 'Different'
*/
public function testValidateAssertHeaderContainsWrongValue()
{
$this->expectException(\ByJG\ApiTools\Exception\NotMatchedException::class);
$this->expectExceptionMessage("Does not exists header 'X-Test' with value 'Different'");

$expectedResponse = Response::getInstance(200)
->withBody(new MemoryStream(json_encode([
"id" => 1,
Expand Down Expand Up @@ -239,11 +243,12 @@ public function testValidateAssertHeaderContainsWrongValue()
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
* @throws \ByJG\ApiTools\Exception\StatusCodeNotMatchedException
* @throws \ByJG\Util\Psr7\MessageException
* @expectedException \ByJG\ApiTools\Exception\NotMatchedException
* @expectedExceptionMessage Does not exists header 'X-Test' with value 'Different'
*/
public function testValidateAssertHeaderContainsNonExistent()
{
$this->expectException(\ByJG\ApiTools\Exception\NotMatchedException::class);
$this->expectExceptionMessage("Does not exists header 'X-Test' with value 'Different'");

$expectedResponse = Response::getInstance(200)
->withBody(new MemoryStream(json_encode([
"id" => 1,
Expand Down Expand Up @@ -299,11 +304,12 @@ public function testValidateAssertBodyContains()
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
* @throws \ByJG\ApiTools\Exception\StatusCodeNotMatchedException
* @throws \ByJG\Util\Psr7\MessageException
* @expectedException \ByJG\ApiTools\Exception\NotMatchedException
* @expectedExceptionMessage Body does not contain 'Doris'
*/
public function testValidateAssertBodyNotContains()
{
$this->expectException(\ByJG\ApiTools\Exception\NotMatchedException::class);
$this->expectExceptionMessage("Body does not contain 'Doris'");

$expectedResponse = Response::getInstance(200)
->withBody(new MemoryStream(json_encode([
"id" => 1,
Expand Down
Loading

0 comments on commit 8025692

Please sign in to comment.