Преглед изворни кода

[DoctrineBridge] Issue #1376 - Unique Validator does not work with null values

Benjamin Eberlei пре 14 година
родитељ
комит
08b4219347

+ 4 - 0
src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

@@ -66,6 +66,10 @@ class UniqueEntityValidator extends ConstraintValidator
             }
 
             $criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
+
+            if ($criteria[$fieldName] === null) {
+                return true;
+            }
         }
 
         $repository = $em->getRepository($className);

+ 1 - 1
tests/Symfony/Tests/Bridge/Doctrine/Fixtures/SingleIdentEntity.php

@@ -12,7 +12,7 @@ class SingleIdentEntity
     /** @Id @Column(type="integer") */
     protected $id;
 
-    /** @Column(type="string") */
+    /** @Column(type="string", nullable=true) */
     public $name;
 
     public function __construct($id, $name) {

+ 40 - 14
tests/Symfony/Tests/Bridge/Doctrine/Validator/Constraints/UniqueValidatorTest.php

@@ -58,20 +58,8 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
         return $validatorFactory;
     }
 
-    /**
-     * This is a functional test as there is a large integration necessary to get the validator working.
-     */
-    public function testValidateUniqueness()
+    public function createValidator($entityManagerName, $em)
     {
-        $entityManagerName = "foo";
-        $em = $this->createTestEntityManager();
-        $schemaTool = new SchemaTool($em);
-        $schemaTool->createSchema(array(
-            $em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity')
-        ));
-
-        $entity1 = new SingleIdentEntity(1, 'Foo');
-
         $registry = $this->createRegistryMock($entityManagerName, $em);
 
         $uniqueValidator = new UniqueEntityValidator($registry);
@@ -82,8 +70,28 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
         $metadataFactory = $this->createMetadataFactoryMock($metadata);
         $validatorFactory = $this->createValidatorFactory($uniqueValidator);
 
-        $validator = new Validator($metadataFactory, $validatorFactory);
+        return new Validator($metadataFactory, $validatorFactory);
+    }
+
+    private function createSchema($em)
+    {
+        $schemaTool = new SchemaTool($em);
+        $schemaTool->createSchema(array(
+            $em->getClassMetadata('Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity')
+        ));
+    }
 
+    /**
+     * This is a functional test as there is a large integration necessary to get the validator working.
+     */
+    public function testValidateUniqueness()
+    {
+        $entityManagerName = "foo";
+        $em = $this->createTestEntityManager();
+        $this->createSchema($em);
+        $validator = $this->createValidator($entityManagerName, $em);
+
+        $entity1 = new SingleIdentEntity(1, 'Foo');
         $violationsList = $validator->validate($entity1);
         $this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database.");
 
@@ -103,4 +111,22 @@ class UniqueValidatorTest extends DoctrineOrmTestCase
         $this->assertEquals('name', $violation->getPropertyPath());
         $this->assertEquals('Foo', $violation->getInvalidValue());
     }
+
+    public function testValidateUniquenessWithNull()
+    {
+        $entityManagerName = "foo";
+        $em = $this->createTestEntityManager();
+        $this->createSchema($em);
+        $validator = $this->createValidator($entityManagerName, $em);
+
+        $entity1 = new SingleIdentEntity(1, null);
+        $entity2 = new SingleIdentEntity(2, null);
+
+        $em->persist($entity1);
+        $em->persist($entity2);
+        $em->flush();
+
+        $violationsList = $validator->validate($entity1);
+        $this->assertEquals(0, $violationsList->count(), "No violations found on entity having a null value.");
+    }
 }