Skip to content

Commit b0f9724

Browse files
Initial commit
0 parents  commit b0f9724

19 files changed

+5367
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
testsuite:
7+
name: Unittests
8+
runs-on: ubuntu-24.04
9+
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
php-version: ['8.3', '8.4']
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 1
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php-version }}
24+
extensions: json, fileinfo
25+
tools: pecl
26+
coverage: pcov
27+
28+
- name: Composer install
29+
run: |
30+
if [[ ${{ matrix.prefer-lowest == 'prefer-lowest' }} ]]; then
31+
composer update --prefer-stable
32+
else
33+
composer install
34+
fi
35+
36+
- name: Run PHPUnit
37+
run: |
38+
if [[ ${{ matrix.php-version }} == '8.3' ]]; then
39+
bin/phpunit --coverage-clover=coverage.xml
40+
else
41+
bin/phpunit
42+
fi
43+
- name: Code Coverage Report
44+
if: success() && matrix.php-version == '8.2'
45+
uses: codecov/codecov-action@v4
46+
47+
code-analysis:
48+
name: Coding Standard & Static Analysis
49+
runs-on: ubuntu-24.04
50+
51+
steps:
52+
- uses: actions/checkout@v4
53+
with:
54+
fetch-depth: 1
55+
56+
- name: Setup PHP
57+
uses: shivammathur/setup-php@v2
58+
with:
59+
php-version: ${{ matrix.php-version }}
60+
extensions: json, fileinfo
61+
coverage: pcov
62+
tools: pecl
63+
64+
- name: Composer install
65+
run: composer update --prefer-lowest --prefer-stable
66+
67+
- name: Run phpcs
68+
run: bin/phpcs --version && bin/phpcs --report=source --standard=phpcs.xml
69+
70+
- name: Run phpstan
71+
run: bin/phpstan -V && bin/phpstan --error-format=github
72+
73+
- name: Run phpmd
74+
run: bin/phpmd --version && composer phpmd

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.idea/
2+
/vendor/
3+
/bin/
4+
/.phpunit.cache/
5+
/tmp/
6+
/tools/

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Open API Validation for PHPUnit Tests
2+
3+
This small library will make it very easy for you to validate your request and response objects against a given Open API Schemas.
4+
5+
Under the hood `league/openapi-psr7-validator` is used but abstracted in a way that it can be replaced with any other implementation.
6+
7+
## Testing the OpenAPI Schema
8+
9+
* Your test case must use the [OpenAPIValidatorTrait](src/OpenAPIValidatorTrait.php) or extend [App\Tests\ApiTestCase](../tests/ApiTestCase.php)
10+
* Your test case must instantiate and set the [App\Tests\OpenAPISchemaValidator](../tests/OpenAPISchemaValidator.php) with the right schema. If you don't do this, the next steps will fail with an exception.
11+
* Call `assertRequestMatchesOpenAPISchema($request)` to validate the request against the OpenAPI schema.
12+
* Call `assertResponseMatchesOpenAPISchema($response)` to validate the response against the OpenAPI schema.
13+
14+
### Example:
15+
16+
```php
17+
use Phauthentic\PHPUnit\OpenAPIValidator\OpenAPIValidatorTrait;
18+
19+
class MyTestCase extends TestCase
20+
{
21+
use OpenAPIValidatorTrait;
22+
23+
public function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
// Load your OpenAPI schema
28+
self::setOpenAPISchemaValidator = new OpenAPISchemaValidator(
29+
'path/to/openapi.yaml',
30+
);
31+
}
32+
33+
public function testSomeAPIIntegration(): void
34+
{
35+
// Create a client and make a request or whatever your framework
36+
// provides you to make such calls.
37+
$client = $this->createClient();
38+
$client->request('POST', '/api/v1/products', [
39+
'productName' => 'PHP',
40+
]);
41+
42+
// Assert the request and response against the OpenAPI schema
43+
self::assertRequestMatchesOpenAPISchema($client->getRequest());
44+
self::assertResponseMatchesOpenAPISchema($client->getResponse());
45+
path: '/api/v1/follows',
46+
method: 'post'
47+
);
48+
}
49+
}
50+
```
51+
52+
## Symfony Support
53+
54+
Symfony does not support the PSR-7 interface for requests and responses. This means that you cannot use the OpenAPISchemaValidator directly in your Symfony tests. This package provides a workaround for this limitation by using the Symfony Bridge for PHPUnit. This bridge provides a way to use the PSR-7 interface in your Symfony tests.
55+
56+
You need to add those dependencies to your project via Composer:
57+
58+
* nyholm/psr7
59+
* symfony/phpunit-bridge
60+
61+
Instead of using the OpenAPISchemaValidator use the OpenAPISymfonySchemaValidator in your test case.
62+
63+
```php
64+
self::setOpenAPISchemaValidator = new OpenAPISymfonySchemaValidator(
65+
'path/to/openapi.yaml',
66+
);
67+
```
68+
69+
## License
70+
71+
This bundle is under the [MIT license](LICENSE).
72+
73+
Copyright Florian Krämer

composer.json

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"name": "phauthentic/phpunit-openapi-assertions",
3+
"type": "library",
4+
"require": {
5+
"php": "^8.1",
6+
"league/openapi-psr7-validator": "^0.22.0"
7+
},
8+
"require-dev": {
9+
"nyholm/psr7": "^1.0",
10+
"phpmd/phpmd": "^2.15",
11+
"phpstan/phpstan": "^2.1",
12+
"phpunit/phpunit": "^10.0||^11.0||^12.0",
13+
"squizlabs/php_codesniffer": "^3.13",
14+
"symfony/http-foundation": "^6.0||^7.0",
15+
"symfony/phpunit-bridge": "^7.2",
16+
"symfony/psr-http-message-bridge": "^6.0||^7.0",
17+
"symfony/test-pack": "^1.0"
18+
},
19+
"suggest": {
20+
"nyholm/psr7": "f you want to use Symfony's HTTP Foundation Response and Request Objects",
21+
"symfony/psr-http-message-bridge": "f you want to use Symfony's HTTP Foundation Response and Request Objects",
22+
"symfony/http-foundation": "If you want to use Symfony's HTTP Foundation Response and Request Objects"
23+
},
24+
"license": "MIT",
25+
"authors": [
26+
{
27+
"name": "Florian Krämer"
28+
}
29+
],
30+
"autoload": {
31+
"psr-4": {
32+
"Phauthentic\\PHPUnit\\OpenAPIValidator\\": "src/"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Phauthentic\\PHPUnit\\OpenAPIValidator\\Tests\\": "tests/"
38+
}
39+
},
40+
"minimum-stability": "stable",
41+
"prefer-stable": true,
42+
"config": {
43+
"sort-packages": true,
44+
"optimize-autoloader": true,
45+
"bin-dir": "bin"
46+
},
47+
"scripts": {
48+
"test": [
49+
"phpunit"
50+
],
51+
"test-coverage": [
52+
"phpunit --coverage-text"
53+
],
54+
"test-coverage-html": [
55+
"phpunit --coverage-html tmp/coverage/"
56+
],
57+
"cscheck": [
58+
"phpcs src/ tests/ -s --standard=phpcs.xml"
59+
],
60+
"csfix": [
61+
"phpcbf src/ tests/"
62+
],
63+
"analyze": [
64+
"phpstan analyse src/"
65+
],
66+
"analyse": [
67+
"phpstan analyse src/"
68+
],
69+
"phpmd": [
70+
"bin/phpmd ./src/ text phpmd.xml"
71+
]
72+
}
73+
}

0 commit comments

Comments
 (0)