Skip to content

Commit

Permalink
Merge 05b92e7 into 26c99fa
Browse files Browse the repository at this point in the history
  • Loading branch information
guilliamxavier committed Jun 23, 2021
2 parents 26c99fa + 05b92e7 commit 6433b17
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Expand Up @@ -26,17 +26,23 @@ class PropertySchemaRegexRestriction implements PropertySchemaRestrictionMetadat
{
/**
* {@inheritdoc}
*
* @param Regex $constraint
*/
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array
{
return $constraint instanceof Regex && $constraint->getHtmlPattern() ? ['pattern' => '^('.$constraint->getHtmlPattern().')$'] : [];
if (null !== ($htmlPattern = $constraint->getHtmlPattern())) {
return ['pattern' => '^('.$htmlPattern.')$'];
}

return [];
}

/**
* {@inheritdoc}
*/
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool
{
return $constraint instanceof Regex && $constraint->match;
return $constraint instanceof Regex;
}
}
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Bridge\Symfony\Validator\Metadata\Property\Restriction;

use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRegexRestriction;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\Regex;

/**
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
*/
final class PropertySchemaRegexRestrictionTest extends TestCase
{
use ProphecyTrait;

private $propertySchemaRegexRestriction;

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

/**
* @dataProvider supportsProvider
*/
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void
{
self::assertSame($expectedResult, $this->propertySchemaRegexRestriction->supports($constraint, $propertyMetadata));
}

public function supportsProvider(): \Generator
{
yield 'supported' => [new Regex(['pattern' => '/^[0-9]+$/']), new PropertyMetadata(), true];
yield 'supported too' => [new Regex(['pattern' => '/[0-9]/', 'match' => false]), new PropertyMetadata(), true];
yield 'not supported' => [new Positive(), new PropertyMetadata(), false];
}

/**
* @dataProvider createProvider
*/
public function testCreate(Constraint $constraint, PropertyMetadata $propertyMetadata, array $expectedResult): void
{
self::assertSame($expectedResult, $this->propertySchemaRegexRestriction->create($constraint, $propertyMetadata));
}

public function createProvider(): \Generator
{
yield 'anchored' => [new Regex(['pattern' => '/^[0-9]+$/']), new PropertyMetadata(), ['pattern' => '^([0-9]+)$']];
yield 'not anchored' => [new Regex(['pattern' => '/[0-9]/']), new PropertyMetadata(), ['pattern' => '^(.*[0-9].*)$']];
yield 'inverted' => [new Regex(['pattern' => '/[0-9]/', 'match' => false]), new PropertyMetadata(), ['pattern' => '^(((?![0-9]).)*)$']];
}
}

0 comments on commit 6433b17

Please sign in to comment.