Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/osteel/openapi-httpfoundation-testing-laravel-example
OpenAPI HttpFoundation Testing: a Laravel Example
https://github.com/osteel/openapi-httpfoundation-testing-laravel-example
api http httpfoundation laravel openapi testing validation
Last synced: about 1 month ago
JSON representation
OpenAPI HttpFoundation Testing: a Laravel Example
- Host: GitHub
- URL: https://github.com/osteel/openapi-httpfoundation-testing-laravel-example
- Owner: osteel
- Created: 2020-11-05T16:24:20.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-06-19T15:34:27.000Z (over 1 year ago)
- Last Synced: 2024-10-30T00:35:41.367Z (3 months ago)
- Topics: api, http, httpfoundation, laravel, openapi, testing, validation
- Language: PHP
- Homepage: https://tech.osteel.me/posts/openapi-backed-api-testing-in-php-projects-a-laravel-example
- Size: 260 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# OpenAPI HttpFoundation Testing – a Laravel Example
This repository demonstrates how to use the [OpenAPI HttpFoundation Testing](https://github.com/osteel/openapi-httpfoundation-testing) package in a Laravel project.
It uses [L5 Swagger](https://github.com/DarkaOnLine/L5-Swagger) to expose an OpenAPI-based documentation through [Swagger UI](https://swagger.io/tools/swagger-ui/).
Please refer to [this blog 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 a detailed explanation.
## Installation
Clone the repository:
```bash
$ git clone [email protected]:osteel/openapi-httpfoundation-testing-laravel-example.git
```Instal the project's dependencies from the root folder:
```bash
$ composer install
```## Usage
Start the PHP development server using Laravel's Artisan command:
```bash
$ php artisan serve
```Swagger UI should now be available at [localhost:8000/api/documentation](http://localhost:8000/api/documentation) (you might need to replace `api-docs.json` with `api-docs.yaml` in the "explore" bar at the top, so it loads the [YAML definition](https://github.com/osteel/openapi-httpfoundation-testing-laravel-example/blob/main/storage/api-docs/api-docs.yaml) instead of the JSON one, that we haven't provided).
There is an [example test](https://github.com/osteel/openapi-httpfoundation-testing-laravel-example/blob/main/tests/ExampleTest.php) which you can run:
```bash
$ ./vendor/bin/phpunit tests/Feature
```This test queries the `/api/test` endpoint and validates the response against the provided YAML OpenAPI definition.
Now alter that endpoint's behaviour by changing its route in `routes/api.php` for this one:
```php
Route::get('/test', function (Request $request) {
return response()->json(['baz' => 'bar']);
});
```Run the test again – it should now fail because the response contains a `baz` key where `foo` is expected, as per the OpenAPI definition.