Skip to content

Commit 823c52f

Browse files
test: Validate Command tests
1 parent 1d8808b commit 823c52f

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
beforeEach(function () {
4+
$this->fixturesPath = dirname(__DIR__, ).'/Fixtures';
5+
});
6+
7+
it('validates correct specification', function () {
8+
$this->artisan('validate', [
9+
'spec' => "{$this->fixturesPath}/valid-petstore.yaml"
10+
])
11+
->expectsOutput('🔍 Validating your magical scroll...')
12+
->expectsOutput('✨ Your magical scroll OpenAPI specification is valid!')
13+
->assertExitCode(0);
14+
});
15+
16+
it('fails for non existent file', function () {
17+
$nonExistentPath = "{$this->fixturesPath}/non-existent.yaml'";
18+
19+
$this->artisan('validate', ['spec' => $nonExistentPath])
20+
->expectsOutput('🔍 Validating your magical scroll...')
21+
->expectsOutput('🌋 The magical scroll does not exist: ' . $nonExistentPath)
22+
->assertExitCode(1);
23+
});
24+
25+
it('fails for invalid yaml', function () {
26+
$this->artisan('validate', [
27+
'spec' => "{$this->fixturesPath}/invalid-yaml.yaml"
28+
])
29+
->expectsOutput('🔍 Validating your magical scroll...')
30+
->expectsOutput('🌋 Your magical scroll has some imperfections:')
31+
->assertExitCode(1);
32+
});
33+
34+
it('shows summary for valid specification', function () {
35+
$this->artisan('validate', [
36+
'spec' => "{$this->fixturesPath}/valid-petstore.yaml"
37+
])
38+
->expectsOutput('📖 Specification Summary:')
39+
->expectsOutput('🛣️ Available Paths:')
40+
->expectsOutput(' /pet [PUT, POST]')
41+
->assertExitCode(0);
42+
});
43+
44+
it('loads custom config when provided', function () {
45+
$configPath = "{$this->fixturesPath}/config.php";
46+
file_put_contents($configPath, '<?php return ["openapi" => ["version" => "3.0"]];');
47+
48+
$this->artisan('validate', [
49+
'spec' => "{$this->fixturesPath}/valid-petstore.yaml",
50+
'--config' => $configPath
51+
])->assertExitCode(0);
52+
53+
unlink($configPath);
54+
});

tests/Fixtures/invalid-yaml.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
openapi: 3.0.2
2+
info:
3+
title: Invalid YAML
4+
version: 1.0.0
5+
paths:
6+
/pet:
7+
post
8+
summary: Invalid YAML structure
9+
responses:
10+
'200':
11+
description: OK

tests/Fixtures/valid-petstore.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "Test Pet Store",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/pet": {
9+
"post": {
10+
"summary": "Add a pet",
11+
"responses": {
12+
"200": {
13+
"description": "OK"
14+
}
15+
}
16+
},
17+
"put": {
18+
"summary": "Update a pet",
19+
"responses": {
20+
"200": {
21+
"description": "OK"
22+
}
23+
}
24+
}
25+
}
26+
},
27+
"components": {
28+
"schemas": {
29+
"Pet": {
30+
"type": "object",
31+
"properties": {
32+
"id": {
33+
"type": "integer"
34+
},
35+
"name": {
36+
"type": "string"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}

tests/Fixtures/valid-petstore.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: 3.0.2
2+
info:
3+
title: Test Pet Store
4+
version: 1.0.0
5+
paths:
6+
/pet:
7+
post:
8+
summary: Add a pet
9+
responses:
10+
'200':
11+
description: OK
12+
put:
13+
summary: Update a pet
14+
responses:
15+
'200':
16+
description: OK
17+
components:
18+
schemas:
19+
Pet:
20+
type: object
21+
properties:
22+
id:
23+
type: integer
24+
name:
25+
type: string
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use App\Config\ApiSwookeryConfig;
4+
use App\OpenApi\SpecificationReader;
5+
use cebe\openapi\spec\OpenApi;
6+
7+
beforeEach(function () {
8+
$this->reader = new SpecificationReader(ApiSwookeryConfig::defaults());
9+
$this->fixturesPath = __DIR__ .'/../Fixtures';
10+
});
11+
12+
it('can read valid yaml specification', function () {
13+
$spec = $this->reader->read("{$this->fixturesPath}/valid-petstore.yaml");
14+
15+
expect($spec)
16+
->toBeInstanceOf(OpenApi::class)
17+
->and($spec->openapi)
18+
->toBe('3.0.2');
19+
});
20+
21+
it('can read valid json specification', function () {
22+
$spec = $this->reader->read("{$this->fixturesPath}/valid-petstore.json");
23+
24+
expect($spec)
25+
->toBeInstanceOf(OpenApi::class)
26+
->and($spec->openapi)
27+
->toBe('3.0.2');
28+
});
29+
30+
it('throws exception for invalid yaml', function () {
31+
$this->reader->read("{$this->fixturesPath}/invalid-yaml.yaml");
32+
})->throws(RuntimeException::class);
33+
34+
it('validates openapi version', function () {
35+
$spec = $this->reader->read("{$this->fixturesPath}/valid-petstore.yaml");
36+
37+
expect($this->reader->validate($spec))->toBeTrue();
38+
});
39+
40+
it('throws exception for unsupported version', function () {
41+
$config = ApiSwookeryConfig::fromArray(['openapi' => ['version' => '3.1.0']]);
42+
$reader = new SpecificationReader($config);
43+
44+
$spec = $reader->read("{$this->fixturesPath}/valid-petstore.yaml");
45+
$reader->validate($spec);
46+
})->throws(RuntimeException::class, 'OpenAPI version 3.0.2 is not supported');

0 commit comments

Comments
 (0)