{"id":15657445,"url":"https://github.com/osteel/openapi-httpfoundation-testing","last_synced_at":"2025-04-08T04:12:13.439Z","repository":{"id":43659895,"uuid":"306618893","full_name":"osteel/openapi-httpfoundation-testing","owner":"osteel","description":"Validate your HttpFoundation requests and responses against OpenAPI (3+) definitions","archived":false,"fork":false,"pushed_at":"2024-10-07T15:35:47.000Z","size":78,"stargazers_count":110,"open_issues_count":1,"forks_count":16,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-01T03:32:06.717Z","etag":null,"topics":["api","http","httpfoundation","laravel","openapi","openapi3","php","psr-7","symfony","testing","validation"],"latest_commit_sha":null,"homepage":"https://tech.osteel.me/posts/openapi-backed-api-testing-in-php-projects-a-laravel-example","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/osteel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-23T11:42:16.000Z","updated_at":"2025-02-11T14:47:23.000Z","dependencies_parsed_at":"2023-11-29T17:27:49.578Z","dependency_job_id":"647629ce-1163-4ec4-ac7f-c95c45730db4","html_url":"https://github.com/osteel/openapi-httpfoundation-testing","commit_stats":{"total_commits":42,"total_committers":7,"mean_commits":6.0,"dds":"0.40476190476190477","last_synced_commit":"f310d2052eaf66ad665abd9971b3c13e8db1e2dd"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osteel%2Fopenapi-httpfoundation-testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osteel%2Fopenapi-httpfoundation-testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osteel%2Fopenapi-httpfoundation-testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osteel%2Fopenapi-httpfoundation-testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osteel","download_url":"https://codeload.github.com/osteel/openapi-httpfoundation-testing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247773719,"owners_count":20993639,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","http","httpfoundation","laravel","openapi","openapi3","php","psr-7","symfony","testing","validation"],"created_at":"2024-10-03T13:07:07.158Z","updated_at":"2025-04-08T04:12:13.414Z","avatar_url":"https://github.com/osteel.png","language":"PHP","readme":"# OpenAPI HttpFoundation Testing\n\n[![Build Status](https://github.com/osteel/openapi-httpfoundation-testing/workflows/CI/badge.svg)](https://github.com/osteel/openapi-httpfoundation-testing/actions)\n[![Latest Stable Version](https://img.shields.io/packagist/v/osteel/openapi-httpfoundation-testing)](https://packagist.org/packages/osteel/openapi-httpfoundation-testing)\n[![License](https://img.shields.io/packagist/l/osteel/openapi-httpfoundation-testing)](https://packagist.org/packages/osteel/openapi-httpfoundation-testing)\n[![Downloads](https://img.shields.io/packagist/dt/osteel/openapi-httpfoundation-testing)](https://packagist.org/packages/osteel/openapi-httpfoundation-testing)\n\n\nValidate HttpFoundation requests and responses against OpenAPI (3+) definitions.\n\nSee [this post](https://tech.osteel.me/posts/openapi-backed-api-testing-in-php-projects-a-laravel-example \"OpenAPI-backed API testing in PHP projects – a Laravel example\") for more details and [this repository](https://github.com/osteel/openapi-httpfoundation-testing-laravel-example) for an example use in a Laravel project.\n\n\u003e [!IMPORTANT]\n\u003e While you can safely use this package for your projects, as long as version `1.0` has not been released \"minor\" version patches can contain breaking changes. Make sure to check the [release section](../../releases) before you upgrade.\n\n## Why?\n\n[OpenAPI](https://swagger.io/specification/) is a specification intended to describe RESTful APIs in a way that can be understood by both humans and machines.\n\nBy validating an API's requests and responses against the OpenAPI definition that describes it, we guarantee that the API is used correctly and behaves in accordance with the documentation we provide, thus making the OpenAPI definition the single source of truth.\n\nThe [HttpFoundation component](https://symfony.com/doc/current/components/http_foundation.html) is developed and maintained as part of the [Symfony framework](https://symfony.com/). It is used to handle HTTP requests and responses in projects such as Symfony, Laravel, Drupal, and [many others](https://symfony.com/components/HttpFoundation).\n\n## How does it work?\n\nThis package is built on top of [OpenAPI PSR-7 Message Validator](https://github.com/thephpleague/openapi-psr7-validator), which validates [PSR-7 messages](https://www.php-fig.org/psr/psr-7/) against OpenAPI definitions.\n\nIt converts HttpFoundation request and response objects to PSR-7 messages using Symfony's [PSR-7 Bridge](https://symfony.com/doc/current/components/psr7.html) and [Tobias Nyholm](https://github.com/Nyholm)'s [PSR-7 implementation](https://github.com/Nyholm/psr7), before passing them on to OpenAPI PSR-7 Message Validator.\n\n## Installation\n\n\u003e [!NOTE]\n\u003e This package is mostly intended to be used as part of an API test suite.\n\nVia Composer:\n\n```bash\ncomposer require --dev osteel/openapi-httpfoundation-testing\n```\n\n## Usage\n\nImport the builder class:\n\n```php\nuse Osteel\\OpenApi\\Testing\\ValidatorBuilder;\n```\n\nUse the builder to create a [`\\Osteel\\OpenApi\\Testing\\Validator`](/src/Validator.php) object, using one of the available factory methods for YAML or JSON:\n\n```php\n// From a file:\n\n$validator = ValidatorBuilder::fromYamlFile($yamlFile)-\u003egetValidator();\n$validator = ValidatorBuilder::fromJsonFile($jsonFile)-\u003egetValidator();\n\n// From a string:\n\n$validator = ValidatorBuilder::fromYamlString($yamlString)-\u003egetValidator();\n$validator = ValidatorBuilder::fromJsonString($jsonString)-\u003egetValidator();\n\n// Automatic detection (slower):\n\n$validator = ValidatorBuilder::fromYaml($yamlFileOrString)-\u003egetValidator();\n$validator = ValidatorBuilder::fromJson($jsonFileOrString)-\u003egetValidator();\n```\n\n\u003e [!TIP]\n\u003e You can also use a dependency injection container to bind the `ValidatorBuilder` class to the [`ValidatorBuilderInterface`](/src/ValidatorBuilderInterface.php) interface it implements and inject the interface instead, which would also be useful for testing and mocking.\n\nYou can now validate `\\Symfony\\Component\\HttpFoundation\\Request` and `\\Symfony\\Component\\HttpFoundation\\Response` objects for a given [path](https://swagger.io/specification/#paths-object) and method:\n\n```php\n$validator-\u003evalidate($response, '/users', 'post');\n```\n\n\u003e [!TIP]\n\u003e For convenience, objects implementing `\\Psr\\Http\\Message\\ServerRequestInterface` or `\\Psr\\Http\\Message\\ResponseInterface` are also accepted.\n\nIn the example above, we check that the response matches the OpenAPI definition for a `POST` request on the `/users` path.\n\nEach of OpenAPI's [supported HTTP methods](https://swagger.io/docs/specification/paths-and-operations/ \"Paths and Operations\") (`DELETE`, `GET`, `HEAD`, `OPTIONS`, `PATCH`, `POST`, `PUT` and `TRACE`) also has a shortcut method that calls `validate` under the hood, meaning the line above could also be written this way:\n\n```php\n$validator-\u003epost($response, '/users');\n```\n\nValidating a request object works exactly the same way:\n\n```php\n$validator-\u003epost($request, '/users');\n```\n\nIn the example above, we check that the request matches the OpenAPI definition for a `POST` request on the `/users` path.\n\nThe `validate` method returns `true` in case of success, and throw a [`\\Osteel\\OpenApi\\Testing\\Exceptions\\ValidationException`](/src/Exceptions/ValidationException.php) exception in case of error.\n\n## Caching\n\nThis package supports caching to speed up the parsing of OpenAPI definitions. Simply pass your [PSR-6](https://www.php-fig.org/psr/psr-6/) or [PSR-16](https://www.php-fig.org/psr/psr-16/) cache object to the `setCache` method of the [`ValidatorBuilder`](/src/ValidatorBuilder.php) class.\n\nHere is an example using Symfony's [Array Cache Adapter](https://symfony.com/doc/current/components/cache/adapters/array_cache_adapter.html \"Array Cache Adapter\"):\n\n```php\nuse Osteel\\OpenApi\\Testing\\ValidatorBuilder;\nuse Symfony\\Component\\Cache\\Adapter\\ArrayAdapter;\n\n$cache = new ArrayAdapter();\n$validator = ValidatorBuilder::fromYamlFile($yamlFile)-\u003esetCache($cache)-\u003egetValidator();\n```\n\n## Extending the package\n\nThere are two main extension points – message adapters and cache adapters.\n\n### Message adapters\n\nThe [`ValidatorBuilder`](/src/ValidatorBuilder.php) class uses the [`HttpFoundationAdapter`](/src/Adapters/HttpFoundationAdapter.php) class as its default HTTP message adapter. This class converts HttpFoundation request and response objects to their PSR-7 counterparts.\n\nIf you need to change the adapter's logic, or if you need a new adapter altogether, create a class implementing the [`MessageAdapterInterface`](/src/Adapters/MessageAdapterInterface.php) interface and pass it to the `setMessageAdapter` method of the [`ValidatorBuilder`](/src/ValidatorBuilder.php) class:\n\n```php\n$validator = ValidatorBuilder::fromYamlFile($yamlFile)\n    -\u003esetMessageAdapter($yourAdapter)\n    -\u003egetValidator();\n```\n\n### Cache adapters\n\nThe [`ValidatorBuilder`](/src/ValidatorBuilder.php) class uses the [`Psr16Adapter`](/src/Cache/Psr16Adapter.php) class as its default cache adapter. This class converts PSR-16 cache objects to their PSR-6 counterparts.\n\nIf you need to change the adapter's logic, or if you need a new adapter altogether, create a class implementing the [`CacheAdapterInterface`](/src/Cache/CacheAdapterInterface.php) interface and pass it to the `setCacheAdapter` method of the [`ValidatorBuilder`](/src/ValidatorBuilder.php) class:\n\n```php\n$validator = ValidatorBuilder::fromYamlFile($yamlFile)\n    -\u003esetCacheAdapter($yourAdapter)\n    -\u003egetValidator();\n```\n\n### Other interfaces\n\nThe [`ValidatorBuilder`](/src/ValidatorBuilder.php) and [`Validator`](/src/Validator.php) classes are `final` but they implement the [`ValidatorBuilderInterface`](/src/ValidatorBuilderInterface.php) and [`ValidatorInterface`](/src/ValidatorInterface.php) interfaces respectively for which you can provide your own implementations if you need to.\n\n## Change log\n\nPlease see the [Releases section](../../releases) for details.\n\n## Contributing\n\nPlease see [CONTRIBUTING](/.github/CONTRIBUTING.md) for details.\n\n## Credits\n\n**People**\n\n- [Yannick Chenot](https://github.com/osteel)\n- [Patrick Rodacker](https://github.com/lordrhodos)\n- [Johnathan Michael Dell](https://github.com/johnathanmdell)\n- [Paul Mitchum](https://github.com/paul-m)\n- [All Contributors](../../contributors)\n\nSpecial thanks to [Pavel Batanov](https://github.com/scaytrase) for his advice on structuring the package.\n\n**Packages**\n\n- [OpenAPI PSR-7 Message Validator](https://github.com/thephpleague/openapi-psr7-validator)\n- [The PSR-7 Bridge](https://symfony.com/doc/current/components/psr7.html)\n- [PSR-7 implementation](https://github.com/Nyholm/psr7)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosteel%2Fopenapi-httpfoundation-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosteel%2Fopenapi-httpfoundation-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosteel%2Fopenapi-httpfoundation-testing/lists"}