Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions tests/ExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OpenApi\Annotations as OA;
use OpenApi\Generator;
use OpenApi\Serializer;
use OpenApi\TypeResolverInterface;

/**
* @requires PHP 8.1
Expand All @@ -35,22 +36,25 @@ public function exampleSpecs(): iterable
$implementations = ['annotations', 'attributes', 'mixed'];
$versions = [OA\OpenApi::VERSION_3_0_0, OA\OpenApi::VERSION_3_1_0];

foreach ($examples as $example) {
foreach ($implementations as $implementation) {
if (!file_exists($this->examplePath($example) . '/' . $implementation)) {
continue;
}

foreach ($versions as $version) {
if (!file_exists($this->getSpecFilename($example, $implementation, $version))) {
foreach (static::getTypeResolvers() as $resolverName => $typeResolver) {
foreach ($examples as $example) {
foreach ($implementations as $implementation) {
if (!file_exists($this->examplePath($example) . '/' . $implementation)) {
continue;
}

yield "$example:$implementation;$version" => [
$example,
$implementation,
$version,
];
foreach ($versions as $version) {
if (!file_exists($this->getSpecFilename($example, $implementation, $version))) {
continue;
}

yield "$example:$resolverName-$implementation-$version" => [
$typeResolver,
$example,
$implementation,
$version,
];
}
}
}
}
Expand All @@ -61,7 +65,7 @@ public function exampleSpecs(): iterable
*
* @dataProvider exampleSpecs
*/
public function testExample(string $name, string $implementation, string $version): void
public function testExample(TypeResolverInterface $typeResolver, string $name, string $implementation, string $version): void
{
$this->registerExampleClassloader($name, $implementation);

Expand All @@ -70,7 +74,7 @@ public function testExample(string $name, string $implementation, string $versio

$openapi = (new Generator($this->getTrackingLogger()))
->setVersion($version)
->setTypeResolver($this->getTypeResolver())
->setTypeResolver($typeResolver)
->generate([$path]);
// file_put_contents($specFilename, $openapi->toYaml());
$this->assertSpecEquals(
Expand All @@ -83,7 +87,7 @@ public function testExample(string $name, string $implementation, string $versio
/**
* @dataProvider exampleSpecs
*/
public function testSerializer(string $name, string $implementation, string $version): void
public function testSerializer(TypeResolverInterface $typeResolver, string $name, string $implementation, string $version): void
{
$specFilename = $this->getSpecFilename($name, $implementation, $version);

Expand Down
35 changes: 0 additions & 35 deletions tests/Fixtures/Scratch/Examples3.0.0.yaml

This file was deleted.

51 changes: 51 additions & 0 deletions tests/Fixtures/Scratch/RequestBody3.0.0-legacy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
openapi: 3.0.0
info:
title: RequestBody
version: '1.0'
paths:
/endpoint/schema-ref-json:
post:
operationId: 8d1f9c9f0437712695d337f1c11badc7
requestBody:
description: 'Information about a new pet in the system'
content:
application/json:
schema:
$ref: '#/components/schemas/RequestBodySchema'
responses:
'200':
description: 'All good'
/endpoint/schema-ref:
post:
operationId: 7fa3f3456b14a4a8eb3a0357283ecd15
requestBody:
description: 'Information about a new pet in the system'
content:
application/json:
schema:
$ref: '#/components/schemas/RequestBodySchema'
responses:
'200':
description: 'All good'
/endpoint/ref:
post:
operationId: 4250dd87e4e3872a8f2e481532cbc245
requestBody:
$ref: '#/components/requestBodies/RequestBodyRef'
responses:
'200':
description: 'All good'
/endpoint/ref-foo:
post:
operationId: 344406e28927343e4e9e4f39bd6c385b
requestBody:
required: true
responses:
'200':
description: 'All good'
components:
schemas:
RequestBodySchema: { }
requestBodies:
RequestBodyRef: { }
foo: { }
51 changes: 51 additions & 0 deletions tests/Fixtures/Scratch/RequestBody3.1.0-legacy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
openapi: 3.1.0
info:
title: RequestBody
version: '1.0'
paths:
/endpoint/schema-ref-json:
post:
operationId: 8d1f9c9f0437712695d337f1c11badc7
requestBody:
description: 'Information about a new pet in the system'
content:
application/json:
schema:
$ref: '#/components/schemas/RequestBodySchema'
responses:
'200':
description: 'All good'
/endpoint/schema-ref:
post:
operationId: 7fa3f3456b14a4a8eb3a0357283ecd15
requestBody:
description: 'Information about a new pet in the system'
content:
application/json:
schema:
$ref: '#/components/schemas/RequestBodySchema'
responses:
'200':
description: 'All good'
/endpoint/ref:
post:
operationId: 4250dd87e4e3872a8f2e481532cbc245
requestBody:
$ref: '#/components/requestBodies/RequestBodyRef'
responses:
'200':
description: 'All good'
/endpoint/ref-foo:
post:
operationId: 344406e28927343e4e9e4f39bd6c385b
requestBody:
required: true
responses:
'200':
description: 'All good'
components:
schemas:
RequestBodySchema: { }
requestBodies:
RequestBodyRef: { }
foo: { }
12 changes: 12 additions & 0 deletions tests/OpenApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ public function getTypeResolver(): TypeResolverInterface
: new LegacyTypeResolver();
}

public static function getTypeResolvers(): array
{
$typeResolvers = [
'legacy' => new LegacyTypeResolver(),
];
if (class_exists(StringTypeResolver::class)) {
$typeResolvers['type-info'] = new TypeInfoTypeResolver();
}

return $typeResolvers;
}

public function initializeProcessors(array $processors): array
{
$generator = (new Generator())
Expand Down
54 changes: 30 additions & 24 deletions tests/ScratchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use OpenApi\Annotations as OA;
use OpenApi\Generator;
use OpenApi\TypeResolverInterface;

/**
* @requires PHP 8.1
Expand All @@ -16,32 +17,37 @@ class ScratchTest extends OpenApiTestCase
{
public static function scratchTestProvider(): iterable
{
foreach (glob(static::fixture('Scratch/*.php')) as $fixture) {
$name = pathinfo($fixture, PATHINFO_FILENAME);
foreach (static::getTypeResolvers() as $resolverName => $typeResolver) {
foreach (glob(static::fixture('Scratch/*.php')) as $fixture) {
$name = pathinfo($fixture, PATHINFO_FILENAME);

if (0 === strpos($name, 'Abstract')) {
continue;
}
if (0 === strpos($name, 'Abstract')) {
continue;
}

$scratch = static::fixture("Scratch/$name.php");
$specs = [
static::fixture("Scratch/{$name}3.1.0.yaml") => OA\OpenApi::VERSION_3_1_0,
static::fixture("Scratch/{$name}3.0.0.yaml") => OA\OpenApi::VERSION_3_0_0,
];
$scratch = static::fixture("Scratch/$name.php");
$specs = [
static::fixture("Scratch/{$name}3.1.0.yaml") => OA\OpenApi::VERSION_3_1_0,
static::fixture("Scratch/{$name}3.1.0-{$resolverName}.yaml") => OA\OpenApi::VERSION_3_1_0,
static::fixture("Scratch/{$name}3.0.0.yaml") => OA\OpenApi::VERSION_3_0_0,
static::fixture("Scratch/{$name}3.0.0-{$resolverName}.yaml") => OA\OpenApi::VERSION_3_0_0,
];

$expectedLogs = [
'Examples-3.0.0' => ['@OA\Schema() is only allowed for 3.1.x'],
];
$expectedLogs = [
'Examples-3.0.0' => ['@OA\Schema() is only allowed for 3.1.x'],
];

foreach ($specs as $spec => $version) {
if (file_exists($spec)) {
$dataSet = "$name-$version";
yield $dataSet => [
$scratch,
$spec,
$version,
array_key_exists($dataSet, $expectedLogs) ? $expectedLogs[$dataSet] : [],
];
foreach ($specs as $spec => $version) {
if (file_exists($spec)) {
$dataSet = "$resolverName-$name-$version";
yield $dataSet => [
$typeResolver,
$scratch,
$spec,
$version,
array_key_exists($dataSet, $expectedLogs) ? $expectedLogs[$dataSet] : [],
];
}
}
}
}
Expand All @@ -54,7 +60,7 @@ public static function scratchTestProvider(): iterable
*
* @requires PHP 8.2
*/
public function testScratch(string $scratch, string $spec, string $version, array $expectedLogs): void
public function testScratch(TypeResolverInterface $typeResolver, string $scratch, string $spec, string $version, array $expectedLogs): void
{
foreach ($expectedLogs as $logLine) {
$this->assertOpenApiLogEntryContains($logLine);
Expand All @@ -63,7 +69,7 @@ public function testScratch(string $scratch, string $spec, string $version, arra
require_once $scratch;

$openapi = (new Generator($this->getTrackingLogger()))
->setTypeResolver($this->getTypeResolver())
->setTypeResolver($typeResolver)
->setVersion($version)
->generate([$scratch]);

Expand Down