Skip to content

Commit ce96ad3

Browse files
committed
tests: More coverage
1 parent 38f0354 commit ce96ad3

12 files changed

Lines changed: 301 additions & 0 deletions

tests/Unit/Concerns/BuildsArrayTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public function toArray(): array
3939
}
4040
}
4141

42+
final class BuildsArrayNoExtensionsFixture
43+
{
44+
use BuildsArray;
45+
46+
/**
47+
* @param array<string, mixed> $fields
48+
*
49+
* @return array<string, mixed>
50+
*/
51+
public function assemble(array $fields): array
52+
{
53+
return $this->buildArray($fields);
54+
}
55+
}
56+
4257
it('drops null fields', function (): void {
4358
$out = (new BuildsArrayFixture())->assemble([
4459
'title' => 'X',
@@ -147,6 +162,16 @@ public function toArray(): array
147162
]);
148163
});
149164

165+
it('does not call getExtensions() on classes without HasExtensionsInterface', function (): void {
166+
$fixture = new BuildsArrayNoExtensionsFixture();
167+
168+
// If the instanceof check were mutated to true, this would fatal-error
169+
// because BuildsArrayNoExtensionsFixture has no getExtensions() method.
170+
$out = $fixture->assemble(['title' => 'Test']);
171+
172+
expect($out)->toBe(['title' => 'Test']);
173+
});
174+
150175
it('unwraps a Cortex JsonSchema stripping $schema and title', function (): void {
151176
$stringSchema = Schema::string('IgnoredTitle')->minLength(2);
152177

tests/Unit/Objects/EncodingTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@
3838
'allowReserved' => true,
3939
]);
4040
});
41+
42+
it('explode() defaults to true', function (): void {
43+
expect(Encoding::create()->explode()->toArray())->toBe(['explode' => true]);
44+
});
45+
46+
it('allowReserved() defaults to true', function (): void {
47+
expect(Encoding::create()->allowReserved()->toArray())->toBe(['allowReserved' => true]);
48+
});

tests/Unit/Objects/HeaderTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,36 @@
3737
'example' => 'abc123',
3838
]);
3939
});
40+
41+
it('required() defaults to true', function (): void {
42+
expect(Header::create()->required()->toArray())->toBe(['required' => true]);
43+
});
44+
45+
it('deprecated() defaults to true', function (): void {
46+
expect(Header::create()->deprecated()->toArray())->toBe(['deprecated' => true]);
47+
});
48+
49+
it('emits all optional fields when set', function (): void {
50+
$header = Header::create()
51+
->allowEmptyValue(true)
52+
->style('simple')
53+
->explode(true)
54+
->allowReserved(true)
55+
->schema(Schema::string());
56+
57+
expect($header->toArray())->toBe([
58+
'allowEmptyValue' => true,
59+
'style' => 'simple',
60+
'explode' => true,
61+
'allowReserved' => true,
62+
'schema' => ['type' => 'string'],
63+
]);
64+
});
65+
66+
it('explode() defaults to true', function (): void {
67+
expect(Header::create()->explode()->toArray())->toBe(['explode' => true]);
68+
});
69+
70+
it('allowReserved() defaults to true', function (): void {
71+
expect(Header::create()->allowReserved()->toArray())->toBe(['allowReserved' => true]);
72+
});

tests/Unit/Objects/InfoTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,28 @@
5656
'version' => '1.0',
5757
]);
5858
});
59+
60+
it('contact emits all fields', function (): void {
61+
$contact = Contact::create()
62+
->name('API Team')
63+
->url('https://example.com/support')
64+
->email('support@example.com');
65+
66+
expect($contact->toArray())->toBe([
67+
'name' => 'API Team',
68+
'url' => 'https://example.com/support',
69+
'email' => 'support@example.com',
70+
]);
71+
});
72+
73+
it('license emits identifier and url', function (): void {
74+
$license = License::create('Apache 2.0')
75+
->identifier('Apache-2.0')
76+
->url('https://www.apache.org/licenses/LICENSE-2.0');
77+
78+
expect($license->toArray())->toBe([
79+
'name' => 'Apache 2.0',
80+
'identifier' => 'Apache-2.0',
81+
'url' => 'https://www.apache.org/licenses/LICENSE-2.0',
82+
]);
83+
});

tests/Unit/Objects/LinkTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,54 @@
5252
'$ref' => '#/components/links/Foo',
5353
]);
5454
});
55+
56+
it('inserts requestBody after parameters when parameters are present', function (): void {
57+
$link = Link::create()
58+
->operationId('users.create')
59+
->parameters(['id' => '$response.body#/id'])
60+
->requestBody(['key' => 'value']);
61+
62+
$array = $link->toArray();
63+
64+
expect($array)->toHaveKey('requestBody');
65+
expect($array['requestBody'])->toBe(['key' => 'value']);
66+
$keys = array_keys($array);
67+
expect(array_search('requestBody', $keys))->toBeGreaterThan(array_search('parameters', $keys));
68+
});
69+
70+
it('inserts requestBody before description when no parameters present', function (): void {
71+
$link = Link::create()
72+
->operationId('users.show')
73+
->requestBody(null)
74+
->description('Fetch user');
75+
76+
$array = $link->toArray();
77+
78+
expect($array)->toHaveKey('requestBody');
79+
$keys = array_keys($array);
80+
expect(array_search('requestBody', $keys))->toBeLessThan(array_search('description', $keys));
81+
});
82+
83+
it('inserts requestBody before server when no parameters or description', function (): void {
84+
$link = Link::create()
85+
->operationId('users.show')
86+
->requestBody(['body' => 'data'])
87+
->server(Server::create('https://api.example.com'));
88+
89+
$array = $link->toArray();
90+
91+
expect($array)->toHaveKey('requestBody');
92+
$keys = array_keys($array);
93+
expect(array_search('requestBody', $keys))->toBeLessThan(array_search('server', $keys));
94+
});
95+
96+
it('appends requestBody at end when no parameters, description, or server', function (): void {
97+
$link = Link::create()
98+
->operationId('users.show')
99+
->requestBody(['body' => 'data']);
100+
101+
$array = $link->toArray();
102+
103+
expect($array)->toHaveKey('requestBody');
104+
expect(array_key_last($array))->toBe('requestBody');
105+
});

tests/Unit/Objects/MediaTypeTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use Cortex\JsonSchema\Schema;
6+
use Cortex\OpenApi\Objects\Encoding;
67
use Cortex\OpenApi\Objects\Example;
78
use Cortex\OpenApi\Objects\MediaType;
89
use Cortex\OpenApi\Objects\Reference;
@@ -70,3 +71,28 @@
7071
],
7172
]);
7273
});
74+
75+
it('example appears before encoding in output', function (): void {
76+
$mediaType = MediaType::json(Schema::string())
77+
->example('foo')
78+
->encoding(['photo' => Encoding::create()->contentType('image/png')]);
79+
80+
$keys = array_keys($mediaType->toArray());
81+
expect(array_search('example', $keys))->toBeLessThan(array_search('encoding', $keys));
82+
});
83+
84+
it('example appears at end when only schema present', function (): void {
85+
$mediaType = MediaType::json(Schema::string())->example('foo');
86+
87+
expect($mediaType->toArray())->toBe([
88+
'schema' => ['type' => 'string'],
89+
'example' => 'foo',
90+
]);
91+
});
92+
93+
it('emits encoding', function (): void {
94+
$mediaType = MediaType::json(Schema::object())
95+
->encoding(['photo' => Encoding::create()->contentType('image/png')]);
96+
97+
expect($mediaType->toArray())->toHaveKey('encoding');
98+
});

tests/Unit/Objects/OperationTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
use Cortex\JsonSchema\Schema;
66
use Cortex\OpenApi\Enums\HttpMethod;
7+
use Cortex\OpenApi\Objects\Callback;
8+
use Cortex\OpenApi\Objects\PathItem;
9+
use Cortex\OpenApi\Objects\Reference;
710
use Cortex\OpenApi\Objects\Response;
811
use Cortex\OpenApi\Objects\MediaType;
912
use Cortex\OpenApi\Objects\Operation;
1013
use Cortex\OpenApi\Objects\Parameter;
1114
use Cortex\OpenApi\Objects\RequestBody;
1215
use Cortex\OpenApi\Objects\ExternalDocs;
1316
use Cortex\OpenApi\Objects\SecurityRequirement;
17+
use Cortex\OpenApi\Objects\Server;
1418

1519
covers(Operation::class);
1620

@@ -106,3 +110,36 @@
106110
]],
107111
]);
108112
});
113+
114+
it('deprecated() defaults to true', function (): void {
115+
expect(Operation::get()->deprecated()->toArray())->toMatchArray(['deprecated' => true]);
116+
});
117+
118+
it('emits servers when set', function (): void {
119+
$operation = Operation::get()->servers(Server::create('https://api.example.com'));
120+
121+
expect($operation->toArray())->toBe([
122+
'servers' => [['url' => 'https://api.example.com']],
123+
]);
124+
});
125+
126+
it('skips Reference objects in responses()', function (): void {
127+
$operation = Operation::get()->responses(
128+
Response::ok(),
129+
Reference::to('#/components/responses/Error'),
130+
);
131+
132+
expect($operation->toArray())->toBe([
133+
'responses' => [
134+
'200' => ['description' => 'OK'],
135+
],
136+
]);
137+
});
138+
139+
it('emits callbacks when set', function (): void {
140+
$operation = Operation::post()->callbacks([
141+
'onData' => Callback::create()->expression('{$url}', PathItem::create('/hook')),
142+
]);
143+
144+
expect($operation->toArray())->toHaveKey('callbacks');
145+
});

tests/Unit/Objects/ParameterTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,23 @@
8686
'$ref' => '#/components/parameters/PageSize',
8787
]);
8888
});
89+
90+
it('required() defaults to true', function (): void {
91+
expect(Parameter::query('test', Schema::string())->required()->toArray())->toMatchArray(['required' => true]);
92+
});
93+
94+
it('deprecated() defaults to true', function (): void {
95+
expect(Parameter::query('test', Schema::string())->deprecated()->toArray())->toMatchArray(['deprecated' => true]);
96+
});
97+
98+
it('explode() defaults to true', function (): void {
99+
expect(Parameter::query('test', Schema::string())->explode()->toArray())->toMatchArray(['explode' => true]);
100+
});
101+
102+
it('allowReserved() defaults to true', function (): void {
103+
expect(Parameter::query('test', Schema::string())->allowReserved()->toArray())->toMatchArray(['allowReserved' => true]);
104+
});
105+
106+
it('emits allowEmptyValue when set', function (): void {
107+
expect(Parameter::query('test', Schema::string())->allowEmptyValue(true)->toArray())->toMatchArray(['allowEmptyValue' => true]);
108+
});

tests/Unit/Objects/PathItemTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,39 @@
7171
'$ref' => '#/components/pathItems/UserById',
7272
]);
7373
});
74+
75+
it('emits vendor extensions', function (): void {
76+
$pathItem = PathItem::create('/users')
77+
->x('internal', true);
78+
79+
expect($pathItem->toArray())->toBe([
80+
'x-internal' => true,
81+
]);
82+
});
83+
84+
it('servers() preserves values as a list', function (): void {
85+
$pathItem = PathItem::create('/users')
86+
->servers(
87+
Server::create('https://api.example.com'),
88+
Server::create('https://staging.example.com'),
89+
);
90+
91+
$result = $pathItem->toArray();
92+
expect($result['servers'])->toBe([
93+
['url' => 'https://api.example.com'],
94+
['url' => 'https://staging.example.com'],
95+
]);
96+
expect(array_is_list($result['servers']))->toBeTrue();
97+
});
98+
99+
it('parameters() preserves values as a list', function (): void {
100+
$pathItem = PathItem::create('/users/{id}')
101+
->parameters(
102+
Parameter::path('id', Schema::string()),
103+
Parameter::query('include', Schema::string()),
104+
);
105+
106+
$result = $pathItem->toArray();
107+
expect(array_is_list($result['parameters']))->toBeTrue();
108+
expect($result['parameters'])->toHaveCount(2);
109+
});

tests/Unit/Objects/RequestBodyTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,11 @@
6060
'$ref' => '#/components/requestBodies/Create',
6161
]);
6262
});
63+
64+
it('required() defaults to true', function (): void {
65+
$requestBody = RequestBody::create()
66+
->required()
67+
->content(MediaType::json(Schema::string()));
68+
69+
expect($requestBody->toArray())->toMatchArray(['required' => true]);
70+
});

0 commit comments

Comments
 (0)