Skip to content

Commit 3b0b446

Browse files
authored
Merge release 5.4.1 into 5.5.x #920
Merge release 5.4.1 into 5.5.x
2 parents 6202864 + 739c432 commit 3b0b446

File tree

5 files changed

+101
-68
lines changed

5 files changed

+101
-68
lines changed

phpcs.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131
<exclude name="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat.DisallowedShortNullable" />
3232
</rule>
3333

34+
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint">
35+
<exclude-pattern>src/Validator/Constraints/Unique.php</exclude-pattern>
36+
</rule>
37+
3438
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
3539
<exclude-pattern>tests/*</exclude-pattern>
40+
<exclude-pattern>src/Validator/Constraints/Unique.php</exclude-pattern>
3641
</rule>
3742

3843
<rule ref="Squiz.Classes.ClassFileName.NoMatch">

phpstan-baseline.neon

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ parameters:
210210
count: 1
211211
path: src/DependencyInjection/Configuration.php
212212

213+
-
214+
message: '#^Call to function method_exists\(\) with ''Doctrine\\\\ODM\\\\MongoDB\\\\Configuration'' and ''setAutoEncryption'' will always evaluate to true\.$#'
215+
identifier: function.alreadyNarrowedType
216+
count: 1
217+
path: src/DependencyInjection/DoctrineMongoDBExtension.php
218+
213219
-
214220
message: '#^Call to function method_exists\(\) with ''Doctrine\\\\ODM\\\\MongoDB\\\\Configuration'' and ''setUseTransactional…'' will always evaluate to true\.$#'
215221
identifier: function.alreadyNarrowedType
@@ -414,12 +420,6 @@ parameters:
414420
count: 1
415421
path: src/Repository/ContainerRepositoryFactory.php
416422

417-
-
418-
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\Validator\\Constraints\\Unique\:\:__construct\(\) has parameter \$options with no value type specified in iterable type array\.$#'
419-
identifier: missingType.iterableValue
420-
count: 1
421-
path: src/Validator/Constraints/Unique.php
422-
423423
-
424424
message: '#^Call to method PHPUnit\\Framework\\Assert\:\:assertFalse\(\) with false will always evaluate to false\.$#'
425425
identifier: method.impossibleType
@@ -534,24 +534,6 @@ parameters:
534534
count: 1
535535
path: tests/DependencyInjection/ConfigurationTest.php
536536

537-
-
538-
message: '#^Call to an undefined method Doctrine\\ODM\\MongoDB\\Configuration\:\:getDefaultKmsProvider\(\)\.$#'
539-
identifier: method.notFound
540-
count: 3
541-
path: tests/DependencyInjection/DoctrineMongoDBExtensionTest.php
542-
543-
-
544-
message: '#^Call to an undefined method Doctrine\\ODM\\MongoDB\\Configuration\:\:getDefaultMasterKey\(\)\.$#'
545-
identifier: method.notFound
546-
count: 3
547-
path: tests/DependencyInjection/DoctrineMongoDBExtensionTest.php
548-
549-
-
550-
message: '#^Call to an undefined method Doctrine\\ODM\\MongoDB\\Configuration\:\:getDriverOptions\(\)\.$#'
551-
identifier: method.notFound
552-
count: 7
553-
path: tests/DependencyInjection/DoctrineMongoDBExtensionTest.php
554-
555537
-
556538
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\Tests\\DependencyInjection\\DoctrineMongoDBExtensionTest\:\:assertDICDefinitionMethodCall\(\) has parameter \$params with no value type specified in iterable type array\.$#'
557539
identifier: missingType.iterableValue
@@ -791,9 +773,3 @@ parameters:
791773
identifier: argument.type
792774
count: 1
793775
path: tests/ServiceRepositoryTest.php
794-
795-
-
796-
message: '#^Property Doctrine\\Bundle\\MongoDBBundle\\Tests\\Validator\\Constraints\\UniqueDocumentDummyOne\:\:\$email is unused\.$#'
797-
identifier: property.unused
798-
count: 1
799-
path: tests/Validator/Constraints/UniqueTest.php

phpstan.neon.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ parameters:
88
- config
99
- src
1010
- tests
11+
excludePaths:
12+
# Error "overriding property ... should also have native type" is not ignorable
13+
- src/Validator/Constraints/Unique.php
1114

1215
ignoreErrors:
1316
- message: '# with generic class Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata#'

src/Validator/Constraints/Unique.php

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,33 @@
55
namespace Doctrine\Bundle\MongoDBBundle\Validator\Constraints;
66

77
use Attribute;
8+
use ReflectionProperty;
89
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
910

10-
/**
11-
* Constraint for the unique document validator
12-
*
13-
* @Annotation
14-
* @Target({"CLASS", "ANNOTATION"})
15-
*/
16-
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
17-
class Unique extends UniqueEntity
18-
{
11+
if ((new ReflectionProperty(UniqueEntity::class, 'service'))->hasType()) {
1912
/**
20-
* @param string[]|string $fields The combination of fields that must contain unique values or a set of options
21-
* @param bool|string[]|string $ignoreNull The combination of fields that ignore null values
13+
* Constraint for the unique document validator
14+
*
15+
* @Annotation
16+
* @Target({"CLASS", "ANNOTATION"})
2217
*/
23-
public function __construct(
24-
array|string $fields,
25-
?string $message = null,
26-
string $service = 'doctrine_odm.mongodb.unique',
27-
?string $em = null,
28-
?string $entityClass = null,
29-
?string $repositoryMethod = null,
30-
?string $errorPath = null,
31-
bool|array|string|null $ignoreNull = null,
32-
?array $groups = null,
33-
mixed $payload = null,
34-
array $options = [],
35-
) {
36-
parent::__construct(
37-
$fields,
38-
$message,
39-
$service,
40-
$em,
41-
$entityClass,
42-
$repositoryMethod,
43-
$errorPath,
44-
$ignoreNull,
45-
$groups,
46-
$payload,
47-
$options,
48-
);
18+
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
19+
class Unique extends UniqueEntity
20+
{
21+
public string $service = 'doctrine_odm.mongodb.unique';
22+
}
23+
} else {
24+
// Compatibility for symfony/doctrine-bridge < 7.0
25+
/**
26+
* Constraint for the unique document validator
27+
*
28+
* @Annotation
29+
* @Target({"CLASS", "ANNOTATION"})
30+
*/
31+
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
32+
class Unique extends UniqueEntity
33+
{
34+
/** @var string $service */
35+
public $service = 'doctrine_odm.mongodb.unique';
4936
}
5037
}

tests/Validator/Constraints/UniqueTest.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
use Composer\InstalledVersions;
8+
use Composer\Semver\VersionParser;
79
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique;
810
use PHPUnit\Framework\TestCase;
911
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -23,11 +25,71 @@ public function testWithDefaultProperty(): void
2325
self::assertInstanceOf(Unique::class, $constraint);
2426
self::assertSame(['email'], $constraint->fields);
2527
self::assertSame('doctrine_odm.mongodb.unique', $constraint->validatedBy());
28+
self::assertNull($constraint->em);
29+
}
30+
31+
public function testWithGroups(): void
32+
{
33+
$metadata = new ClassMetadata(UniqueDocumentWithGroups::class);
34+
35+
$loader = new AttributeLoader();
36+
self::assertTrue($loader->loadClassMetadata($metadata));
37+
38+
[$constraint] = $metadata->getConstraints();
39+
self::assertInstanceOf(Unique::class, $constraint);
40+
self::assertSame(['email'], (array) $constraint->fields);
41+
self::assertSame(['group1'], $constraint->groups);
42+
self::assertSame('doctrine_odm.mongodb.unique', $constraint->validatedBy());
43+
self::assertNull($constraint->em);
44+
}
45+
46+
public function testWithIdentifierFieldNames(): void
47+
{
48+
if (! InstalledVersions::satisfies(new VersionParser(), 'symfony/doctrine-bridge', '>= 7.3')) {
49+
self::markTestSkipped('Requires symfony/doctrine-bridge 7.3 or higher, with $identifierFieldNames field');
50+
}
51+
52+
$metadata = new ClassMetadata(UniqueDocumentWithIdentifierFieldNames::class);
53+
54+
$loader = new AttributeLoader();
55+
56+
self::assertTrue($loader->loadClassMetadata($metadata));
57+
58+
[$constraint] = $metadata->getConstraints();
59+
self::assertInstanceOf(Unique::class, $constraint);
60+
self::assertSame(['email'], $constraint->fields);
61+
self::assertSame('Custom message', $constraint->message);
62+
self::assertSame('custom_em', $constraint->em);
63+
self::assertFalse($constraint->ignoreNull);
64+
self::assertSame(['id', 'name'], $constraint->identifierFieldNames);
65+
self::assertSame(['group1', 'group2'], $constraint->groups);
66+
self::assertSame('doctrine_odm.mongodb.unique', $constraint->validatedBy());
2667
}
2768
}
2869

2970
#[Unique(['email'])]
3071
class UniqueDocumentDummyOne
3172
{
32-
private string $email;
73+
public string $email;
74+
}
75+
76+
#[Unique(fields: 'email', groups: ['group1'])]
77+
class UniqueDocumentWithGroups
78+
{
79+
public string $email;
80+
}
81+
82+
#[Unique(
83+
['email'],
84+
'Custom message',
85+
em: 'custom_em',
86+
ignoreNull: false,
87+
identifierFieldNames: ['id', 'name'],
88+
groups: ['group1', 'group2'],
89+
)]
90+
class UniqueDocumentWithIdentifierFieldNames
91+
{
92+
public string $name;
93+
94+
public string $email;
3395
}

0 commit comments

Comments
 (0)