Skip to content

Commit

Permalink
Merge pull request #777 from divine/fix-depreciation
Browse files Browse the repository at this point in the history
fix: UniqueEntity::$service is considered final
  • Loading branch information
malarzm authored Aug 24, 2023
2 parents e8d2be2 + 3320f06 commit 0135a16
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
43 changes: 43 additions & 0 deletions Tests/Validator/Constraints/UniqueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\Validator\Constraints;

use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;

use function assert;

use const PHP_VERSION_ID;

final class UniqueTest extends TestCase
{
public function testWithDefaultProperty(): void
{
$metadata = new ClassMetadata(UniqueDocumentDummyOne::class);

if (PHP_VERSION_ID >= 80000) {
$loader = new AnnotationLoader();
} else {
$loader = new AnnotationLoader(new AnnotationReader());
}

self::assertTrue($loader->loadClassMetadata($metadata));

[$constraint] = $metadata->getConstraints();
assert($constraint instanceof Unique);
self::assertSame(['email'], $constraint->fields);
self::assertSame('doctrine_odm.mongodb.unique', $constraint->validatedBy());
}
}

/** @Unique(fields={"email"}) */
#[Unique(['email'])]
class UniqueDocumentDummyOne
{
private string $email;
}
34 changes: 32 additions & 2 deletions Validator/Constraints/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Unique extends UniqueEntity
{
/** @var string */
public $service = 'doctrine_odm.mongodb.unique';
/**
* @param string[]|string $fields The combination of fields that must contain unique values or a set of options
* @param bool|string[]|string $ignoreNull The combination of fields that ignore null values
* @param mixed $payload
*/
public function __construct(
$fields,
?string $message = null,
string $service = 'doctrine_odm.mongodb.unique',
?string $em = null,
?string $entityClass = null,
?string $repositoryMethod = null,
?string $errorPath = null,
$ignoreNull = null,
?array $groups = null,
$payload = null,
array $options = []
) {
parent::__construct(
$fields,
$message,
$service,
$em,
$entityClass,
$repositoryMethod,
$errorPath,
$ignoreNull,
$groups,
$payload,
$options,
);
}
}

0 comments on commit 0135a16

Please sign in to comment.