Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.
Open
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
5 changes: 5 additions & 0 deletions Enum/ParameterExtensionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ class ParameterExtensionEnum
public const X_PARAMETER_LOCATION = 'parameter_location';
public const X_OPTION_RESOLVE = 'option_resolve';
public const X_CLASS = 'class';

public static function getAll(): array
{
return [self::X_PARAMETER_LOCATION, self::X_OPTION_RESOLVE, self::X_CLASS];
}
}
50 changes: 50 additions & 0 deletions Tests/AnnotationXTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) MarfaTech <https://marfa-tech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Linkin\Bundle\SwaggerResolverBundle\Tests;

use OpenApi\Annotations\Property;
use OpenApi\Annotations\Schema;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class AnnotationXTest extends KernelTestCase
{
public function testSchemaBuilder(): void
{
$key = 'option_resolve';
$value = 'kek';

$schema = $this->createSchemaDefinition($key, $value);
$x = $schema->properties[0]->x;

self::assertEquals($x, [$key => 'kek']);
}

private function createSchemaDefinition(string $key, string $value): Schema
{
return new Schema(
[
'type' => 'object',
'required' => [],
'properties' => [
new Property(
[
'property' => 'new',
'x' => [$key => $value],
]
),
],
]
);
}
}
83 changes: 83 additions & 0 deletions Tests/CheckRequestSchema/CheckSchemaQueryParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) MarfaTech <https://marfa-tech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Linkin\Bundle\SwaggerResolverBundle\Tests\CheckRequestSchema;

use ArrayObject;
use Exception;
use Linkin\Bundle\SwaggerResolverBundle\Builder\OpenApiResolverBuilder;
use Linkin\Bundle\SwaggerResolverBundle\Configuration\OpenApiConfiguration;
use Linkin\Bundle\SwaggerResolverBundle\Matcher\ParameterTypeMatcher;
use Linkin\Bundle\SwaggerResolverBundle\Validator\NumberMaximumValidator;
use Linkin\Bundle\SwaggerResolverBundle\Validator\NumberMinimumValidator;
use OpenApi\Annotations\Property;
use OpenApi\Annotations\Schema;
use OpenApi\Serializer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class CheckSchemaQueryParameterTest extends KernelTestCase
{
/**
* @throws Exception
*/
public function testSchemaBuilderMissingOptions(): void
{
$fieldName = 'limitField';
$originValue = 2000;

$builder = new OpenApiResolverBuilder(
new ArrayObject(),
new ArrayObject([new NumberMinimumValidator(), new NumberMaximumValidator()]),
[],
$this->createMock(OpenApiConfiguration::class),
new ParameterTypeMatcher(),
new Serializer()
);

$schema = $this->createSchemaDefinitionQueryOptions();
$optionsResolver = $builder->build($schema);
$result = $optionsResolver->resolve(['limitField' => 2000]);

self::assertSame($result[$fieldName], $originValue);
}

private function createSchemaDefinitionQueryOptions(): Schema
{
return new Schema([
'required' =>
[
'limitField' => 'limitField',
],
'title' => 'query',
'properties' =>
[
'search' =>
new Property([
'type' => 'string',
'example' => 'search',
]),
'hah' =>
new Property([
'type' => 'string',
'example' => 'cap',
]),
'limitField' =>
new Property([
'type' => 'integer',
'example' => 150,
]),
],
'type' => 'object',
]);
}
}
68 changes: 68 additions & 0 deletions Tests/DeprecatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) MarfaTech <https://marfa-tech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Linkin\Bundle\SwaggerResolverBundle\Tests;

use Exception;
use Linkin\Bundle\SwaggerResolverBundle\Builder\OpenApiResolverBuilder;
use Linkin\Bundle\SwaggerResolverBundle\Configuration\OpenApiConfiguration;
use Linkin\Bundle\SwaggerResolverBundle\Matcher\ParameterTypeMatcher;
use OpenApi\Annotations\Property;
use OpenApi\Annotations\Schema;
use OpenApi\Serializer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class DeprecatedTest extends KernelTestCase
{
/**
* @throws Exception
*/
public function testSchemaBuilder(): void
{
$fieldName = 'testDeprecated';
$fieldType = 'string';

$builder = new OpenApiResolverBuilder(
new \ArrayObject(),
new \ArrayObject(),
[],
$this->createMock(OpenApiConfiguration::class),
new ParameterTypeMatcher(),
new Serializer()
);

$schema = $this->createSchemaDefinition($fieldName, $fieldType);
$optionsResolver = $builder->build($schema);

self::assertTrue($optionsResolver->isDeprecated($fieldName));
}

private function createSchemaDefinition(string $fieldName, string $type): Schema
{
return new Schema(
[
'type' => 'object',
'required' => [],
'properties' => [
new Property(
[
'property' => $fieldName,
'type' => $type,
'deprecated' => true
]
),
],
]
);
}
}
91 changes: 91 additions & 0 deletions Tests/EnumTest/EnumParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) MarfaTech <https://marfa-tech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Linkin\Bundle\SwaggerResolverBundle\Tests\EnumTest;

use ArrayObject;
use Linkin\Bundle\SwaggerResolverBundle\Builder\OpenApiResolverBuilder;
use Linkin\Bundle\SwaggerResolverBundle\Configuration\OpenApiConfiguration;
use Linkin\Bundle\SwaggerResolverBundle\Matcher\ParameterTypeMatcher;
use OpenApi\Annotations\Property;
use OpenApi\Annotations\Schema;
use OpenApi\Serializer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

class EnumParameterTest extends KernelTestCase
{
public function testSchemaEnumOptions(): void
{
$fieldName = 'a';
$fieldType = 'string';

$builder = new OpenApiResolverBuilder(
new ArrayObject(),
new ArrayObject(),
[],
$this->createMock(OpenApiConfiguration::class),
new ParameterTypeMatcher(),
new Serializer()
);

$schema = $this->createSchemaDefinitionOptions($fieldName, $fieldType);
$optionsResolver = $builder->build($schema);

$optionsResolver->resolve(['a' => 'first']);

self::assertTrue(true);
}

public function testSchemaWrongEnumOptions(): void
{
$fieldName = 'a';
$fieldType = 'string';

$builder = new OpenApiResolverBuilder(
new ArrayObject(),
new ArrayObject(),
[],
$this->createMock(OpenApiConfiguration::class),
new ParameterTypeMatcher(),
new Serializer()
);

$schema = $this->createSchemaDefinitionOptions($fieldName, $fieldType);
$optionsResolver = $builder->build($schema);

$this->expectException(InvalidOptionsException::class);
$optionsResolver->resolve(['a' => 'not enum value']);
}

private function createSchemaDefinitionOptions(string $fieldName, string $type): Schema
{
return new Schema(
[
'type' => 'object',
'required' => [$fieldName],
'properties' => [
$fieldName => new Property(
[
'property' => $fieldName,
'example' => 'first',
'type' => $type,
'required' => true,
'enum' => ['first', 'second']
]
),
],
]
);
}
}
47 changes: 47 additions & 0 deletions Tests/EnumTest/ParameterCollectionFormatEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) MarfaTech <https://marfa-tech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Linkin\Bundle\SwaggerResolverBundle\Tests\EnumTest;

use Linkin\Bundle\SwaggerResolverBundle\Enum\ParameterCollectionFormatEnum;
use PHPUnit\Framework\TestCase;
use RuntimeException;

class ParameterCollectionFormatEnumTest extends TestCase
{
public function testGetAll(): void
{
self::assertSame(ParameterCollectionFormatEnum::getAll(), [
'csv',
'ssv',
'tsv',
'pipes',
'multi',
]);
}

public function testGetDelimiter(): void
{
self::assertSame(',', ParameterCollectionFormatEnum::getDelimiter('csv'));
self::assertSame(' ', ParameterCollectionFormatEnum::getDelimiter('ssv'));
self::assertSame("\t", ParameterCollectionFormatEnum::getDelimiter('tsv'));
self::assertSame('|', ParameterCollectionFormatEnum::getDelimiter('pipes'));
self::assertSame('&', ParameterCollectionFormatEnum::getDelimiter('multi'));
}

public function testFailGetDelimiterByUnexpectedFormat(): void
{
$this->expectException(RuntimeException::class);
ParameterCollectionFormatEnum::getDelimiter('_undefined_');
}
}
29 changes: 29 additions & 0 deletions Tests/EnumTest/ParameterExtensionEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) MarfaTech <https://marfa-tech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Linkin\Bundle\SwaggerResolverBundle\Tests\EnumTest;

use Linkin\Bundle\SwaggerResolverBundle\Enum\ParameterExtensionEnum;
use PHPUnit\Framework\TestCase;

class ParameterExtensionEnumTest extends TestCase
{
public function testGetAll(): void
{
self::assertSame(ParameterExtensionEnum::getAll(), [
'parameter_location',
'option_resolve',
'class',
]);
}
}
Loading