Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix generating property schema with Unique restriction
- Loading branch information
Showing
7 changed files
with
157 additions
and
0 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
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
40 changes: 40 additions & 0 deletions
40
...ridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaUniqueRestriction.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,40 @@ | ||
<?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\Bridge\Symfony\Validator\Metadata\Property\Restriction; | ||
|
||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\Unique; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaUniqueRestriction implements PropertySchemaRestrictionMetadataInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array | ||
{ | ||
return ['uniqueItems' => true]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool | ||
{ | ||
return $constraint instanceof Unique; | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
...e/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaUniqueRestrictionTest.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,57 @@ | ||
<?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\PropertySchemaUniqueRestriction; | ||
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\Unique; | ||
|
||
/** | ||
* @author Tomas Norkūnas <norkunas.tom@gmail.com> | ||
*/ | ||
final class PropertySchemaUniqueRestrictionTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $propertySchemaUniqueRestriction; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->propertySchemaUniqueRestriction = new PropertySchemaUniqueRestriction(); | ||
} | ||
|
||
/** | ||
* @dataProvider supportsProvider | ||
*/ | ||
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void | ||
{ | ||
self::assertSame($expectedResult, $this->propertySchemaUniqueRestriction->supports($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function supportsProvider(): \Generator | ||
{ | ||
yield 'supported' => [new Unique(), new PropertyMetadata(), true]; | ||
|
||
yield 'not supported' => [new Positive(), new PropertyMetadata(), false]; | ||
} | ||
|
||
public function testCreate(): void | ||
{ | ||
self::assertSame(['uniqueItems' => true], $this->propertySchemaUniqueRestriction->create(new Unique(), new PropertyMetadata())); | ||
} | ||
} |
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
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,26 @@ | ||
<?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\Fixtures; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class DummyUniqueValidatedEntity | ||
{ | ||
/** | ||
* @var string[] | ||
* | ||
* @Assert\Unique | ||
*/ | ||
public $dummyItems; | ||
} |