Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...ge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestrictionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]).)*)$']]; | ||
} | ||
} |