13
13
14
14
use Symfony \Bridge \Doctrine \Tests \DoctrineOrmTestCase ;
15
15
use Symfony \Bridge \Doctrine \Tests \Fixtures \SingleIdentEntity ;
16
+ use Symfony \Bridge \Doctrine \Tests \Fixtures \DoubleIdentEntity ;
16
17
use Symfony \Bridge \Doctrine \Tests \Fixtures \CompositeIdentEntity ;
17
18
use Symfony \Bridge \Doctrine \Tests \Fixtures \AssociationEntity ;
18
19
use Symfony \Bridge \Doctrine \Validator \Constraints \UniqueEntity ;
@@ -113,7 +114,7 @@ protected function createValidatorFactory($uniqueValidator)
113
114
return $ validatorFactory ;
114
115
}
115
116
116
- public function createValidator ($ entityManagerName , $ em , $ validateClass = null , $ uniqueFields = null , $ errorPath = null , $ repositoryMethod = 'findBy ' )
117
+ public function createValidator ($ entityManagerName , $ em , $ validateClass = null , $ uniqueFields = null , $ errorPath = null , $ repositoryMethod = 'findBy ' , $ ignoreNull = true )
117
118
{
118
119
if (!$ validateClass ) {
119
120
$ validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity ' ;
@@ -131,7 +132,8 @@ public function createValidator($entityManagerName, $em, $validateClass = null,
131
132
'fields ' => $ uniqueFields ,
132
133
'em ' => $ entityManagerName ,
133
134
'errorPath ' => $ errorPath ,
134
- 'repositoryMethod ' => $ repositoryMethod
135
+ 'repositoryMethod ' => $ repositoryMethod ,
136
+ 'ignoreNull ' => $ ignoreNull
135
137
));
136
138
$ metadata ->addConstraint ($ constraint );
137
139
@@ -146,6 +148,7 @@ private function createSchema($em)
146
148
$ schemaTool = new SchemaTool ($ em );
147
149
$ schemaTool ->createSchema (array (
148
150
$ em ->getClassMetadata ('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity ' ),
151
+ $ em ->getClassMetadata ('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity ' ),
149
152
$ em ->getClassMetadata ('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity ' ),
150
153
$ em ->getClassMetadata ('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity ' ),
151
154
));
@@ -223,6 +226,35 @@ public function testValidateUniquenessWithNull()
223
226
$ this ->assertEquals (0 , $ violationsList ->count (), "No violations found on entity having a null value. " );
224
227
}
225
228
229
+ public function testValidateUniquenessWithIgnoreNull ()
230
+ {
231
+ $ entityManagerName = "foo " ;
232
+ $ validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity ' ;
233
+ $ em = $ this ->createTestEntityManager ();
234
+ $ this ->createSchema ($ em );
235
+ $ validator = $ this ->createValidator ($ entityManagerName , $ em , $ validateClass , array ('name ' , 'name2 ' ), 'bar ' , 'findby ' , false );
236
+
237
+ $ entity1 = new DoubleIdentEntity (1 , 'Foo ' , null );
238
+ $ violationsList = $ validator ->validate ($ entity1 );
239
+ $ this ->assertEquals (0 , $ violationsList ->count (), "No violations found on entity before it is saved to the database. " );
240
+
241
+ $ em ->persist ($ entity1 );
242
+ $ em ->flush ();
243
+
244
+ $ violationsList = $ validator ->validate ($ entity1 );
245
+ $ this ->assertEquals (0 , $ violationsList ->count (), "No violations found on entity after it was saved to the database. " );
246
+
247
+ $ entity2 = new DoubleIdentEntity (2 , 'Foo ' , null );
248
+
249
+ $ violationsList = $ validator ->validate ($ entity2 );
250
+ $ this ->assertEquals (1 , $ violationsList ->count (), "Violation found on entity with conflicting entity existing in the database. " );
251
+
252
+ $ violation = $ violationsList [0 ];
253
+ $ this ->assertEquals ('This value is already used. ' , $ violation ->getMessage ());
254
+ $ this ->assertEquals ('bar ' , $ violation ->getPropertyPath ());
255
+ $ this ->assertEquals ('Foo ' , $ violation ->getInvalidValue ());
256
+ }
257
+
226
258
public function testValidateUniquenessAfterConsideringMultipleQueryResults ()
227
259
{
228
260
$ entityManagerName = "foo " ;
0 commit comments