Skip to content

Commit

Permalink
Merge a40f9a6 into 1086183
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenalinkin committed Aug 22, 2021
2 parents 1086183 + a40f9a6 commit e5dd09e
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 4 deletions.
146 changes: 146 additions & 0 deletions Tests/Validator/FormatTimeValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) Viktor Linkin <adrenalinkin@gmail.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\Validator;

use EXSyst\Component\Swagger\Schema;
use Linkin\Bundle\SwaggerResolverBundle\Validator\FormatTimeValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

/**
* @author Viktor Linkin <adrenalinkin@gmail.com>
*/
class FormatTimeValidatorTest extends TestCase
{
private const FORMAT_TIME = 'time';

/**
* @var FormatTimeValidator
*/
private $sut;

protected function setUp(): void
{
$this->sut = new FormatTimeValidator();
}

/**
* @dataProvider supportsDataProvider
*/
public function testSupports(string $format, bool $expectedResult): void
{
$schema = new Schema([
'format' => $format,
]);

$isSupported = $this->sut->supports($schema);

self::assertSame($isSupported, $expectedResult);
}

public function supportsDataProvider(): array
{
return [
'Fail with unsupported format' => [
'format' => '_invalid_format_',
'expectedResult' => false,
],
'Success with right format' => [
'type' => self::FORMAT_TIME,
'expectedResult' => true,
],
];
}

/**
* @dataProvider failToPassValidationDataProvider
*/
public function testFailToPassValidation(?string $pattern, $value): void
{
$schema = new Schema([
'format' => self::FORMAT_TIME,
'pattern' => $pattern,
]);

$this->expectException(InvalidOptionsException::class);

$this->sut->validate($schema, 'savedAtTime', $value);
}

public function failToPassValidationDataProvider(): array
{
return [
'Fail when true value' => [
'pattern' => null,
'value' => true,
],
'Fail when value with incorrect time pattern - number' => [
'pattern' => null,
'value' => '100',
],
'Fail when value with incorrect time pattern - more than 2 digit' => [
'pattern' => null,
'value' => '100:05:55',
],
'Fail when pattern set and value can NOT convert into DateTime' => [
'pattern' => 'any-string-here', // TODO: should check format
'value' => '10_05_45',
],
];
}

/**
* @dataProvider canPassValidationDataProvider
*/
public function testCanPassValidation(?string $pattern, $value): void
{
$schema = new Schema([
'format' => self::FORMAT_TIME,
'pattern' => $pattern,
]);

$this->sut->validate($schema, 'savedAtTime', $value);
self::assertTrue(true);
}

public function canPassValidationDataProvider(): array
{
return [
'Pass when null value' => [
'pattern' => null,
'value' => null,
],
'Pass when empty string value' => [
'pattern' => null,
'value' => '',
],
'Pass when empty zero value' => [ // TODO: should not pass validation
'pattern' => null,
'value' => '0',
],
'Pass when false value' => [ // TODO: should not pass validation
'pattern' => null,
'value' => false,
],
'Pass when value with correct time pattern' => [
'pattern' => null,
'value' => '10:05:45',
],
'Pass when pattern set and value can convert into DateTime' => [
'pattern' => 'any-string-here', // TODO: should check format
'value' => '10/05/45',
],
];
}
}
142 changes: 142 additions & 0 deletions Tests/Validator/FormatTimestampValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) Viktor Linkin <adrenalinkin@gmail.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\Validator;

use EXSyst\Component\Swagger\Schema;
use Linkin\Bundle\SwaggerResolverBundle\Validator\FormatTimestampValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

/**
* @author Viktor Linkin <adrenalinkin@gmail.com>
*/
class FormatTimestampValidatorTest extends TestCase
{
private const FORMAT_TIMESTAMP = 'timestamp';

/**
* @var FormatTimestampValidator
*/
private $sut;

protected function setUp(): void
{
$this->sut = new FormatTimestampValidator();
}

/**
* @dataProvider supportsDataProvider
*/
public function testSupports(string $format, bool $expectedResult): void
{
$schema = new Schema([
'format' => $format,
]);

$isSupported = $this->sut->supports($schema);

self::assertSame($isSupported, $expectedResult);
}

public function supportsDataProvider(): array
{
return [
'Fail with unsupported format' => [
'format' => '_invalid_format_',
'expectedResult' => false,
],
'Success with right format' => [
'type' => self::FORMAT_TIMESTAMP,
'expectedResult' => true,
],
];
}

/**
* @dataProvider failToPassValidationDataProvider
*/
public function testFailToPassValidation(?string $pattern, $value): void
{
$schema = new Schema([
'format' => self::FORMAT_TIMESTAMP,
'pattern' => $pattern,
]);

$this->expectException(InvalidOptionsException::class);

$this->sut->validate($schema, 'updatedAt', $value);
}

public function failToPassValidationDataProvider(): array
{
return [
'Fail when value with incorrect timestamp pattern' => [
'pattern' => null,
'value' => '2020-10-10',
],
'Fail when pattern set and value can NOT convert into DateTime' => [
'pattern' => 'any-string-here',
'value' => '2020-10-10 00:00:00',
],
];
}

/**
* @dataProvider canPassValidationDataProvider
*/
public function testCanPassValidation(?string $pattern, $value): void
{
$schema = new Schema([
'format' => self::FORMAT_TIMESTAMP,
'pattern' => $pattern,
]);

$this->sut->validate($schema, 'updatedAt', $value);
self::assertTrue(true);
}

public function canPassValidationDataProvider(): array
{
return [
'Pass when null value' => [
'pattern' => null,
'value' => null,
],
'Pass when empty string value' => [
'pattern' => null,
'value' => '',
],
'Pass when empty zero value' => [
'pattern' => null,
'value' => '0',
],
'Pass when false value' => [
'pattern' => null,
'value' => false,
],
'Fail when true value' => [
'pattern' => null,
'value' => true,
],
'Pass when value with correct time pattern' => [
'pattern' => null,
'value' => '1629620000',
],
'Pass when pattern set and value can convert into DateTime' => [
'pattern' => 'any-string-here', // TODO: should check format
'value' => '1620000000',
],
];
}
}
12 changes: 12 additions & 0 deletions Tests/Validator/NumberMaximumValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) Viktor Linkin <adrenalinkin@gmail.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\Validator;

use EXSyst\Component\Swagger\Schema;
use Linkin\Bundle\SwaggerResolverBundle\Validator\NumberMaximumValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

/**
* @author Viktor Linkin <adrenalinkin@gmail.com>
*/
class NumberMaximumValidatorTest extends TestCase
{
private const TYPE_NUMBER = 'number';
Expand Down
12 changes: 12 additions & 0 deletions Tests/Validator/NumberMinimumValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) Viktor Linkin <adrenalinkin@gmail.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\Validator;

use EXSyst\Component\Swagger\Schema;
use Linkin\Bundle\SwaggerResolverBundle\Validator\NumberMinimumValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

/**
* @author Viktor Linkin <adrenalinkin@gmail.com>
*/
class NumberMinimumValidatorTest extends TestCase
{
private const TYPE_NUMBER = 'number';
Expand Down
12 changes: 12 additions & 0 deletions Tests/Validator/NumberMultipleOfValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@

declare(strict_types=1);

/*
* This file is part of the SwaggerResolverBundle package.
*
* (c) Viktor Linkin <adrenalinkin@gmail.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\Validator;

use EXSyst\Component\Swagger\Schema;
use Linkin\Bundle\SwaggerResolverBundle\Validator\NumberMultipleOfValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

/**
* @author Viktor Linkin <adrenalinkin@gmail.com>
*/
class NumberMultipleOfValidatorTest extends TestCase
{
private const TYPE_NUMBER = 'number';
Expand Down
10 changes: 6 additions & 4 deletions Validator/AbstractFormatDateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ abstract class AbstractFormatDateValidator implements SwaggerValidatorInterface
/**
* {@inheritdoc}
*/
public function supports(Schema $property, array $context = []): bool
public function supports(Schema $propertySchema, array $context = []): bool
{
return $this->getSupportedFormatName() === $property->getFormat();
return $this->getSupportedFormatName() === $propertySchema->getFormat();
}

/**
* {@inheritdoc}
*/
public function validate(Schema $property, string $propertyName, $value): void
public function validate(Schema $propertySchema, string $propertyName, $value): void
{
if (empty($value)) {
return;
}

if (null === $property->getPattern()) {
$value = (string) $value;

if (null === $propertySchema->getPattern()) {
$this->validateDatePattern($propertyName, $value);
}

Expand Down

0 comments on commit e5dd09e

Please sign in to comment.