Skip to content

Commit 0274a7a

Browse files
committed
docs(readme): add #[OpenApiSpec] attribute usage documentation
1 parent 19f2001 commit 0274a7a

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,51 @@ class GetPetsTest extends TestCase
106106
}
107107
```
108108

109-
To use a different spec for a specific test class, override `openApiSpec()`:
109+
To use a different spec for a specific test class, add the `#[OpenApiSpec]` attribute:
110110

111111
```php
112+
use Studio\OpenApiContractTesting\Laravel\OpenApiSpec;
113+
use Studio\OpenApiContractTesting\Laravel\ValidatesOpenApiSchema;
114+
115+
#[OpenApiSpec('admin')]
112116
class AdminGetUsersTest extends TestCase
113117
{
114118
use ValidatesOpenApiSchema;
115119

116-
protected function openApiSpec(): string
120+
// All tests in this class use the 'admin' spec
121+
}
122+
```
123+
124+
You can also specify the spec per test method. Method-level attributes take priority over class-level:
125+
126+
```php
127+
#[OpenApiSpec('front')]
128+
class MixedApiTest extends TestCase
129+
{
130+
use ValidatesOpenApiSchema;
131+
132+
public function test_front_endpoint(): void
117133
{
118-
return 'admin';
134+
// Uses 'front' from class-level attribute
119135
}
120136

121-
// ...
137+
#[OpenApiSpec('admin')]
138+
public function test_admin_endpoint(): void
139+
{
140+
// Uses 'admin' from method-level attribute (overrides class)
141+
}
122142
}
123143
```
124144

145+
Resolution priority (highest to lowest):
146+
147+
1. Method-level `#[OpenApiSpec]` attribute
148+
2. Class-level `#[OpenApiSpec]` attribute
149+
3. `openApiSpec()` method override
150+
4. `config('openapi-contract-testing.default_spec')`
151+
152+
> **Note:** You can still override `openApiSpec()` as before — it remains fully backward-compatible.
153+
125154
#### Framework-agnostic
126155

127156
```php

0 commit comments

Comments
 (0)