Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: UniqueEntity::$service is considered final #777

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
);
}
}