Home

Awesome

OpenAPI HttpFoundation Testing – a Laravel Example

This repository demonstrates how to use the OpenAPI HttpFoundation Testing package in a Laravel project.

It uses L5 Swagger to expose an OpenAPI-based documentation through Swagger UI.

Please refer to this blog post for a detailed explanation.

Installation

Clone the repository:

$ git clone git@github.com:osteel/openapi-httpfoundation-testing-laravel-example.git

Instal the project's dependencies from the root folder:

$ composer install

Usage

Start the PHP development server using Laravel's Artisan command:

$ php artisan serve

Swagger UI should now be available at 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 instead of the JSON one, that we haven't provided).

There is an example test which you can run:

$ ./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:

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.