You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -85,6 +85,11 @@ return [
85
85
// Maximum number of validation errors to report per response.
86
86
// 0 = unlimited (reports all errors).
87
87
'max_errors' => 20,
88
+
89
+
// Automatically validate every TestResponse produced by Laravel HTTP
90
+
// helpers (get(), post(), etc.) against the OpenAPI spec. Defaults to
91
+
// false for backward compatibility.
92
+
'auto_assert' => false,
88
93
];
89
94
```
90
95
@@ -223,6 +228,42 @@ $validator = new OpenApiResponseValidator(maxErrors: 1);
223
228
224
229
For Laravel, set the `max_errors` key in `config/openapi-contract-testing.php`.
225
230
231
+
#### Auto-assert every response
232
+
233
+
Forgetting `$this->assertResponseMatchesOpenApiSchema($response)` in a test means the contract is silently unchecked. Enable `auto_assert` to validate every response produced by Laravel's HTTP helpers automatically — just include the trait:
234
+
235
+
```php
236
+
// config/openapi-contract-testing.php
237
+
return [
238
+
'default_spec' => 'front',
239
+
'auto_assert' => true,
240
+
];
241
+
```
242
+
243
+
```php
244
+
use Studio\OpenApiContractTesting\Laravel\ValidatesOpenApiSchema;
245
+
246
+
class GetPetsTest extends TestCase
247
+
{
248
+
use ValidatesOpenApiSchema;
249
+
250
+
public function test_list_pets(): void
251
+
{
252
+
// Contract is checked automatically — no explicit assert call needed.
253
+
$this->get('/api/v1/pets')->assertOk();
254
+
}
255
+
}
256
+
```
257
+
258
+
Notes:
259
+
260
+
- Defaults to `false` so existing test suites keep their explicit-assert behavior.
261
+
- Auto-assert hooks into `MakesHttpRequests::createTestResponse()`. Responses you construct manually (outside `$this->get()`, `$this->post()`, etc.) are not touched.
262
+
- Idempotency is keyed on the `(spec, method, path)` tuple. Calling `assertResponseMatchesOpenApiSchema($response)` after auto-assert with the matching signature is a no-op. Calling it with a different `method`/`path` — or a different `#[OpenApiSpec]` — runs validation again.
263
+
- When auto-assert fails, the exception is thrown from inside `$this->get(...)`, so any chained assertion on the same line (`$this->get(...)->assertOk()`) will not run. This is usually what you want — the schema failure takes precedence over status-code checks.
264
+
-`auto_assert` accepts boolean-compatible values (`true`/`false`/`"1"`/`"0"`/`"true"`/`"false"`) so `'auto_assert' => env('OPENAPI_AUTO_ASSERT')` works. Unrecognized values fail the test loudly with a clear message, not silently.
265
+
- Streamed responses (`StreamedResponse`, binary downloads) cause `getContent()` to return `false`, which fails auto-assert with a clear message. If you use `auto_assert=true` on tests that exercise streams, scope the config change per-test or fall back to explicit manual asserts.
266
+
226
267
## Coverage Report
227
268
228
269
After running tests, the PHPUnit extension prints a coverage report. The output format is controlled by the `console_output` parameter (or `OPENAPI_CONSOLE_OUTPUT` environment variable).
0 commit comments