Skip to content

Commit

Permalink
Add ResponseStatusCodesWithWildcards Test for Specfinder
Browse files Browse the repository at this point in the history
Test Different Response Adresses and check if the correct
Response Schema is Found

thephpleague#158
  • Loading branch information
wolffc committed Mar 1, 2022
1 parent 23cdb51 commit fd9bede
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/PSR7/SpecFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use League\OpenAPIValidation\PSR7\CallbackAddress;
use League\OpenAPIValidation\PSR7\OperationAddress;
use League\OpenAPIValidation\PSR7\ResponseAddress;
use League\OpenAPIValidation\PSR7\SpecFinder;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -122,4 +123,61 @@ public function testHandleParameters(): void
$pathItem = $specFinder->findPathSpec(new OperationAddress('/api/1.0/order/123', 'get'));
self::assertSame('The order object', $pathItem->get->responses[200]->description);
}

public function testResponseStatusCodesWithWildcards(): void
{
$yaml = <<<YAML
openapi: 3.0.0
info:
title: Product import API
version: '1.0'
servers:
- url: 'http://localhost:8000/api/v1'
paths:
/products.find:
get:
responses:
'404':
description: Not Found
content:
application/json:
schema:
properties:
message:
type: string
'4XX':
description: Client Error
content:
application/json:
schema:
properties:
message:
type: string
'default':
description: Unexpected Error
content:
application/json:
schema:
properties:
message:
type: string
YAML;

$schema = (new ValidatorBuilder())->fromYaml($yaml)->getServerRequestValidator()->getSchema();
$specFinder = new SpecFinder($schema);
$responseSpec = $specFinder->findResponseSpec(
new ResponseAddress('/products.find', 'get', 404)
);
self::assertSame('Not Found', $responseSpec->description);

$responseSpec = $specFinder->findResponseSpec(
new ResponseAddress('/products.find', 'get', 400)
);
self::assertSame('Client Error', $responseSpec->description);

$responseSpec = $specFinder->findResponseSpec(
new ResponseAddress('/products.find', 'get', 500)
);
self::assertSame('Unexpected Error', $responseSpec->description);
}
}

0 comments on commit fd9bede

Please sign in to comment.