Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function setOptions($options = array())
*/
public function getMessages()
{
return $this->abstractOptions['messages'];
return array_unique($this->abstractOptions['messages']);
}

/**
Expand Down
54 changes: 52 additions & 2 deletions test/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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('[email protected]'));
$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('[email protected]'));
$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
*
Expand Down
6 changes: 3 additions & 3 deletions test/ExplodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions test/TestAsset/ConcreteValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 92c9b0f

Please sign in to comment.