示例#1
0
 public function testUniqueValidatorFail()
 {
     $publisher = new Publisher();
     $publisher->setName('Happy Reading');
     $publisher->setWebsite('http://www.happyreading.com');
     $publisher->save();
     $publisher1 = new Publisher();
     $publisher1->setName('Happy Reading');
     $this->assertFalse($publisher1->validate());
     $failures = $publisher1->getValidationFailures();
     $this->assertCount(1, $failures);
     $this->assertEquals('name', $failures[0]->getPropertyPath());
     $this->assertEquals('This value is already stored in your database', $failures[0]->getMessage());
     $publisher->delete();
 }
 public function testComplexValidationMultipleFailures()
 {
     //Array of expected failures. key: property failed, value: Class in wich the property has failed
     $failedProperties = array('first_name' => 'Propel\\Tests\\Bookstore\\Behavior\\ValidateAuthor', 'website' => 'Propel\\Tests\\Bookstore\\Behavior\\ValidatePublisher', 'title' => 'Propel\\Tests\\Bookstore\\Behavior\\ValidateBook', 'email' => 'Propel\\Tests\\Bookstore\\Behavior\\ValidateReader', 'last_name' => 'Propel\\Tests\\Bookstore\\Behavior\\ValidateReader');
     $author = new ValidateAuthor();
     $author->setId(1);
     $author->setFirstName(null);
     //failed
     $author->setLastName('Friesen');
     $author->setEmail('*****@*****.**');
     $author->setBirthday('1954-10-16');
     $publisher = new ValidatePublisher();
     $publisher->setId(5);
     $publisher->setName('Huel Ltd');
     $publisher->setWebsite('huel.com');
     //failed
     $book = new ValidateBook();
     $book->setId(1);
     $book->setValidateAuthor($author);
     $book->setValidatePublisher($publisher);
     $book->setTitle(null);
     //failed
     $book->setPrice(12, 90);
     $reader1 = new ValidateReader();
     $reader1->setId(1);
     $reader1->setFirstName('Sigurd');
     $reader1->setLastName('Dare');
     $reader1->setEmail('sig.dare@');
     //failed
     $reader1->setBirthday('1974-11-19');
     $book->addValidateReader($reader1);
     $reader2 = new ValidateReader();
     $reader2->setId(2);
     $reader2->setFirstName('Hans');
     $reader2->setLastName(null);
     //failed
     $reader2->setEmail('*****@*****.**');
     $reader2->setBirthday('1974-11-19');
     $book->addValidateReader($reader2);
     $res = $book->validate();
     $this->assertFalse($res, 'This validation expected to fail');
     $failures = $book->getValidationFailures();
     $this->assertInstanceOf('Symfony\\Component\\Validator\\ConstraintViolationList', $failures);
     $this->assertEquals(5, count($failures), 'Five constraint violation objects expected.');
     foreach ($failures as $failure) {
         $this->assertInstanceOf('Symfony\\Component\\Validator\\ConstraintViolation', $failure);
         $failObject = new \ReflectionObject($failure->getRoot());
         $this->assertTrue(in_array($failure->getPropertyPath(), array_keys($failedProperties)));
         $this->assertEquals($failedProperties[$failure->getPropertyPath()], $failObject->getName());
     }
 }