Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charjr committed Dec 16, 2024
1 parent a882c41 commit c234c9c
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 81 deletions.
77 changes: 77 additions & 0 deletions tests/ValueObject/Valid/V30/KeywordsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Membrane\OpenAPIReader\Tests\ValueObject\Valid\V30;

use Membrane\OpenAPIReader\Exception\InvalidOpenAPI;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesInvalidatedSchemas;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesReviewedSchemas;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesSimplifiedSchemas;
use Membrane\OpenAPIReader\ValueObject\Partial;
use Membrane\OpenAPIReader\ValueObject\Valid\Enum\Type;
use Membrane\OpenAPIReader\ValueObject\Valid\Identifier;
use Membrane\OpenAPIReader\ValueObject\Valid\V30\Keywords;
use Membrane\OpenAPIReader\ValueObject\Valid\V30\Schema;
use Membrane\OpenAPIReader\ValueObject\Valid\Validated;
use Membrane\OpenAPIReader\ValueObject\Valid\Warning;
use Membrane\OpenAPIReader\ValueObject\Valid\Warnings;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Schema::class)]
#[CoversClass(Partial\Schema::class)] // DTO
#[CoversClass(InvalidOpenAPI::class)]
#[UsesClass(Type::class)]
#[UsesClass(Identifier::class)]
#[UsesClass(Validated::class)]
#[UsesClass(Warning::class)]
#[UsesClass(Warnings::class)]
class KeywordsTest extends TestCase
{
#[Test]
#[TestDox('It reviews schema keywords for recoverable issues')]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'forV3X')]
public function itReviewsKeywords(Partial\Schema $schema, Warning $warning): void
{
$identifier = new Identifier('sut');
$sut = new Keywords($identifier, new Warnings($identifier), $schema);

self::assertContainsEquals($warning, $sut->getWarnings()->all());
}

#[Test]
#[TestDox('It simplifies schema keywords where possible')]
#[DataProviderExternal(ProvidesSimplifiedSchemas::class, 'forV3X')]
#[DataProviderExternal(ProvidesSimplifiedSchemas::class, 'forV30')]
public function itSimplifiesKeywords(
Partial\Schema $schema,
string $propertyName,
mixed $expected,
): void {
$identifier = new Identifier('sut');
$sut = new Keywords($identifier, new Warnings($identifier), $schema);

self::assertEquals($expected, $sut->{$propertyName});
}

#[Test]
#[TestDox('It invalidates schema keywords for non-recoverable issues')]
#[DataProviderExternal(ProvidesInvalidatedSchemas::class, 'forV3X')]
#[DataProviderExternal(ProvidesInvalidatedSchemas::class, 'forV30')]
public function itInvalidatesKeywords(
InvalidOpenAPI $expected,
Identifier $identifier,
Partial\Schema $schema,
): void {
$warnings = new Warnings($identifier);

self::expectExceptionObject($expected);

new Keywords($identifier, $warnings, $schema);
}
}
16 changes: 7 additions & 9 deletions tests/ValueObject/Valid/V30/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Membrane\OpenAPIReader\Exception\InvalidOpenAPI;
use Membrane\OpenAPIReader\OpenAPIVersion;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesReviewedSchemas;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesSimplifiedSchemas;
use Membrane\OpenAPIReader\ValueObject\Limit;
use Membrane\OpenAPIReader\ValueObject\Partial;
use Membrane\OpenAPIReader\ValueObject\Valid\Enum\Type;
Expand Down Expand Up @@ -36,25 +37,22 @@
class SchemaTest extends TestCase
{
#[Test]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV3xReviews')]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV30Reviews')]
public function itReviewsSchema(Partial\Schema $schema, array $warnings): void
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'forV3X')]
public function itReviewsSchema(Partial\Schema $schema, Warning $warning): void
{
$sut = new Schema(new Identifier('test'), $schema);
$sut = new Schema(new Identifier('sut'), $schema);

self::assertEqualsCanonicalizing($warnings, $sut->getWarnings()->all());
self::assertContainsEquals($warning, $sut->getWarnings()->all());
}

#[Test]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV3xReviews')]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV30Reviews')]
#[DataProviderExternal(ProvidesSimplifiedSchemas::class, 'forV3X')]
public function itSimplifiesSchema(
Partial\Schema $schema,
$_,
string $propertyName,
mixed $expected,
): void {
$sut = new Schema(new Identifier('test'), $schema);
$sut = new Schema(new Identifier('sut'), $schema);

self::assertEquals($expected, $sut->value->{$propertyName});
}
Expand Down
77 changes: 77 additions & 0 deletions tests/ValueObject/Valid/V31/KeywordsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Membrane\OpenAPIReader\Tests\ValueObject\Valid\V31;

use Membrane\OpenAPIReader\Exception\InvalidOpenAPI;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesInvalidatedSchemas;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesReviewedSchemas;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesSimplifiedSchemas;
use Membrane\OpenAPIReader\ValueObject\Partial;
use Membrane\OpenAPIReader\ValueObject\Valid\Enum\Type;
use Membrane\OpenAPIReader\ValueObject\Valid\Identifier;
use Membrane\OpenAPIReader\ValueObject\Valid\V31\Keywords;
use Membrane\OpenAPIReader\ValueObject\Valid\V31\Schema;
use Membrane\OpenAPIReader\ValueObject\Valid\Validated;
use Membrane\OpenAPIReader\ValueObject\Valid\Warning;
use Membrane\OpenAPIReader\ValueObject\Valid\Warnings;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(Schema::class)]
#[CoversClass(Partial\Schema::class)] // DTO
#[CoversClass(InvalidOpenAPI::class)]
#[UsesClass(Type::class)]
#[UsesClass(Identifier::class)]
#[UsesClass(Validated::class)]
#[UsesClass(Warning::class)]
#[UsesClass(Warnings::class)]
class KeywordsTest extends TestCase
{
#[Test]
#[TestDox('It reviews schema keywords for recoverable issues')]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'forV3X')]
public function itReviewsKeywords(Partial\Schema $schema, Warning $warning): void
{
$identifier = new Identifier('sut');
$sut = new Keywords($identifier, new Warnings($identifier), $schema);

self::assertContainsEquals($warning, $sut->getWarnings()->all());
}

#[Test]
#[TestDox('It simplifies schema keywords where possible')]
#[DataProviderExternal(ProvidesSimplifiedSchemas::class, 'forV3X')]
#[DataProviderExternal(ProvidesSimplifiedSchemas::class, 'forV31')]
public function itSimplifiesKeywords(
Partial\Schema $schema,
string $propertyName,
mixed $expected,
): void {
$identifier = new Identifier('sut');
$sut = new Keywords($identifier, new Warnings($identifier), $schema);

self::assertEquals($expected, $sut->{$propertyName});
}

#[Test]
#[TestDox('It invalidates schema keywords for non-recoverable issues')]
#[DataProviderExternal(ProvidesInvalidatedSchemas::class, 'forV3X')]
#[DataProviderExternal(ProvidesInvalidatedSchemas::class, 'forV31')]
public function itInvalidatesKeywords(
InvalidOpenAPI $expected,
Identifier $identifier,
Partial\Schema $schema,
): void {
$warnings = new Warnings($identifier);

self::expectExceptionObject($expected);

new Keywords($identifier, $warnings, $schema);
}
}
12 changes: 5 additions & 7 deletions tests/ValueObject/Valid/V31/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Membrane\OpenAPIReader\Exception\InvalidOpenAPI;
use Membrane\OpenAPIReader\OpenAPIVersion;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesReviewedSchemas;
use Membrane\OpenAPIReader\Tests\Fixtures\ProvidesSimplifiedSchemas;
use Membrane\OpenAPIReader\ValueObject\Limit;
use Membrane\OpenAPIReader\ValueObject\Partial;
use Membrane\OpenAPIReader\ValueObject\Valid\Enum\Type;
Expand Down Expand Up @@ -36,21 +37,18 @@
class SchemaTest extends TestCase
{
#[Test]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV3xReviews')]
//#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV31Reviews')]
public function itReviewsSchema(Partial\Schema $schema, array $warnings): void
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'forV3X')]
public function itReviewsSchema(Partial\Schema $schema, Warning $warning): void
{
$sut = new Schema(new Identifier('test'), $schema);

self::assertEqualsCanonicalizing($warnings, $sut->getWarnings()->all());
self::assertContainsEquals($warning, $sut->getWarnings()->all());
}

#[Test]
#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV3xReviews')]
//#[DataProviderExternal(ProvidesReviewedSchemas::class, 'provideV31Reviews')]
#[DataProviderExternal(ProvidesSimplifiedSchemas::class, 'forV3X')]
public function itSimplifiesSchema(
Partial\Schema $schema,
$_,
string $propertyName,
mixed $expected,
): void {
Expand Down
119 changes: 119 additions & 0 deletions tests/fixtures/ProvidesInvalidatedSchemas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);



namespace Membrane\OpenAPIReader\Tests\Fixtures;

use Generator;
use Membrane\OpenAPIReader\Exception\InvalidOpenAPI;
use Membrane\OpenAPIReader\ValueObject\Partial;
use Membrane\OpenAPIReader\ValueObject\Valid\Identifier;
use Membrane\OpenAPIReader\ValueObject\Valid\Warning;
use Membrane\OpenAPIReader\ValueObject\Value;

final class ProvidesInvalidatedSchemas
{
/**
* @return Generator<array{
* 0:InvalidOpenAPI,
* 1:Identifier,
* 2:Partial\Schema,
* }>
*/
public static function forV3X(): Generator
{
$identifier = new Identifier('sut');

yield 'invalid type' => [
InvalidOpenAPI::invalidType($identifier, 'invalid'),
$identifier,
new Partial\Schema(type: 'invalid'),
];

yield 'properties without string keys' => [
InvalidOpenAPI::mustHaveStringKeys($identifier, 'properties'),
$identifier,
new Partial\Schema(properties: [new Partial\Schema()]),
];

yield 'negative maxLength' => [
InvalidOpenAPI::keywordMustBeNonNegativeInteger($identifier, 'maxLength'),
$identifier,
new Partial\Schema(maxLength: -1),
];

yield 'negative maxItems' => [
InvalidOpenAPI::keywordMustBeNonNegativeInteger($identifier, 'maxItems'),
$identifier,
new Partial\Schema(maxItems: -1),
];

yield 'negative maxProperties' => [
InvalidOpenAPI::keywordMustBeNonNegativeInteger($identifier, 'maxProperties'),
$identifier,
new Partial\Schema(maxProperties: -1),
];

yield 'zero multipleOf' => [
InvalidOpenAPI::keywordCannotBeZero($identifier, 'multipleOf'),
$identifier,
new Partial\Schema(multipleOf: 0),
];

yield 'default does not conform to type' => [
InvalidOpenAPI::defaultMustConformToType($identifier),
$identifier,
new Partial\Schema(type: 'string', default: new Value(1)),
];
}

/**
* @return Generator<array{
* 0:InvalidOpenAPI,
* 1:Identifier,
* 2:Partial\Schema,
* }>
*/
public static function forV30(): Generator
{
$identifier = new Identifier('sut');

yield 'numeric exclusiveMaximum' => [
InvalidOpenAPI::numericExclusiveMinMaxIn30($identifier, 'exclusiveMaximum'),
$identifier,
new Partial\Schema(exclusiveMaximum: 5),
];

yield 'numeric exclusiveMinimum' => [
InvalidOpenAPI::numericExclusiveMinMaxIn30($identifier, 'exclusiveMinimum'),
$identifier,
new Partial\Schema(exclusiveMinimum: 5),
];
}

/**
* @return Generator<array{
* 0:InvalidOpenAPI,
* 1:Identifier,
* 2:Partial\Schema,
* }>
*/
public static function forV31(): Generator
{
$identifier = new Identifier('sut');

yield 'numeric exclusiveMaximum' => [
InvalidOpenAPI::boolExclusiveMinMaxIn31($identifier, 'exclusiveMaximum'),
$identifier,
new Partial\Schema(exclusiveMaximum: true),
];

yield 'numeric exclusiveMinimum' => [
InvalidOpenAPI::boolExclusiveMinMaxIn31($identifier, 'exclusiveMinimum'),
$identifier,
new Partial\Schema(exclusiveMinimum: true),
];
}
}
Loading

0 comments on commit c234c9c

Please sign in to comment.