diff --git a/src/AbstractValidator.php b/src/AbstractValidator.php index 62e1edf6b..50e72220f 100644 --- a/src/AbstractValidator.php +++ b/src/AbstractValidator.php @@ -159,7 +159,7 @@ public function setOptions($options = array()) */ public function getMessages() { - return $this->abstractOptions['messages']; + return array_unique($this->abstractOptions['messages']); } /** diff --git a/test/AbstractTest.php b/test/AbstractTest.php index a163f71b1..e1794bf77 100644 --- a/test/AbstractTest.php +++ b/test/AbstractTest.php @@ -13,6 +13,8 @@ use ReflectionMethod; use Zend\I18n\Translator\Translator; use Zend\Validator\AbstractValidator; +use Zend\Validator\EmailAddress; +use Zend\Validator\Hostname; /** * @category Zend @@ -181,10 +183,12 @@ public function testGetMessageTemplates() { $messages = $this->validator->getMessageTemplates(); $this->assertEquals( - array('fooMessage' => '%value% was passed'), $messages); + array('fooMessage' => '%value% was passed', + 'barMessage' => '%value% was wrong'), $messages); $this->assertEquals( - array(TestAsset\ConcreteValidator::FOO_MESSAGE => '%value% was passed'), + array(TestAsset\ConcreteValidator::FOO_MESSAGE => '%value% was passed', + TestAsset\ConcreteValidator::BAR_MESSAGE => '%value% was wrong'), $messages ); } @@ -239,6 +243,52 @@ public function testMessageCreationWithNestedArrayValueDoesNotRaiseNotice() $this->assertContains('baz', $message); } + public function testNonIdenticalMessagesAllReturned() + { + $this->assertFalse($this->validator->isValid('invalid')); + + $messages = $this->validator->getMessages(); + + $this->assertCount(2, $messages); + $this->assertEquals(array( + TestAsset\ConcreteValidator::FOO_MESSAGE => 'invalid was passed', + TestAsset\ConcreteValidator::BAR_MESSAGE => 'invalid was wrong' + ), $messages); + } + + public function testIdenticalMessagesNotReturned() + { + $this->validator->setMessage('Default error message'); + + $this->assertFalse($this->validator->isValid('invalid')); + + $messages = $this->validator->getMessages(); + + $this->assertCount(1, $messages); + $this->assertEquals('Default error message', reset($messages)); + } + + public function testIdenticalAndNonIdenticalMessagesReturned() + { + $validator = new EmailAddress(); + + $this->assertFalse($validator->isValid('invalid@email.coma')); + $this->assertCount(3, $validator->getMessages()); + $this->assertArrayHasKey(EmailAddress::INVALID_HOSTNAME, $validator->getMessages()); + $this->assertArrayHasKey(Hostname::UNKNOWN_TLD, $validator->getMessages()); + $this->assertArrayHasKey(Hostname::LOCAL_NAME_NOT_ALLOWED, $validator->getMessages()); + + $validator->setMessages(array( + EmailAddress::INVALID_HOSTNAME => 'This is the same error message', + Hostname::UNKNOWN_TLD => 'This is the same error message' + )); + + $this->assertFalse($validator->isValid('invalid@email.coma')); + $this->assertCount(2, $validator->getMessages()); + $this->assertArrayHasKey(EmailAddress::INVALID_HOSTNAME, $validator->getMessages()); + $this->assertArrayHasKey(Hostname::LOCAL_NAME_NOT_ALLOWED, $validator->getMessages()); + } + /** * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred * diff --git a/test/ExplodeTest.php b/test/ExplodeTest.php index d8f1a628c..e4b876004 100644 --- a/test/ExplodeTest.php +++ b/test/ExplodeTest.php @@ -33,16 +33,16 @@ public function getExpectedData() // value delim break N valid messages expects array('foo,bar,dev,null', ',', false, 4, true, array(), true), array('foo,bar,dev,null', ',', true, 1, false, array('X'), false), - array('foo,bar,dev,null', ',', false, 4, false, array('X', 'X', 'X', 'X'), false), + array('foo,bar,dev,null', ',', false, 4, false, array('X'), false), array('foo,bar,dev,null', ';', false, 1, true, array(), true), array('foo;bar,dev;null', ',', false, 2, true, array(), true), - array('foo;bar,dev;null', ',', false, 2, false, array('X', 'X'), false), + array('foo;bar,dev;null', ',', false, 2, false, array('X'), false), array('foo;bar;dev;null', ';', false, 4, true, array(), true), array('foo', ',', false, 1, true, array(), true), array('foo', ',', false, 1, false, array('X'), false), array('foo', ',', true, 1, false, array('X'), false), array(array('a', 'b'), null, false, 2, true, array(), true), - array(array('a', 'b'), null, false, 2, false, array('X', 'X'), false), + array(array('a', 'b'), null, false, 2, false, array('X'), false), array('foo', null, false, 1, true, array(), true), array(1, ',', false, 1, true, array(), true), array(null, ',', false, 1, true, array(), true), diff --git a/test/TestAsset/ConcreteValidator.php b/test/TestAsset/ConcreteValidator.php index 468dffe80..87f84b8e3 100644 --- a/test/TestAsset/ConcreteValidator.php +++ b/test/TestAsset/ConcreteValidator.php @@ -15,15 +15,18 @@ class ConcreteValidator extends AbstractValidator { const FOO_MESSAGE = 'fooMessage'; + const BAR_MESSAGE = 'barMessage'; protected $messageTemplates = array( 'fooMessage' => '%value% was passed', + 'barMessage' => '%value% was wrong' ); public function isValid($value) { $this->setValue($value); $this->error(self::FOO_MESSAGE); + $this->error(self::BAR_MESSAGE); return false; } }