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

Provide NotEmpty::IS_EMPTY validation message for required input #61

Merged
merged 4 commits into from
Sep 3, 2015
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions test/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

use PHPUnit_Framework_MockObject_MockObject as MockObject;
use PHPUnit_Framework_TestCase as TestCase;
use ReflectionProperty;
use stdClass;
use Zend\Filter;
use Zend\Filter\FilterChain;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputInterface;
use Zend\Validator;
use Zend\Validator\NotEmpty;
use Zend\Validator\ValidatorChain;

/**
Expand Down Expand Up @@ -175,17 +177,33 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail()
$this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() value not match');
}

public function testRequiredWithoutFallbackAndValueNotSetThenFailWithCustomErrorMessage()
/**
* @group 28
* @group 60
*/
public function testRequiredWithoutFallbackAndValueNotSetProvidesNotEmptyValidatorIsEmptyErrorMessage()
{
$input = $this->input;
$input->setRequired(true);
$input->setErrorMessage('fooErrorMessage');

$this->assertFalse(
$input->isValid(),
'isValid() should be return always false when no fallback value, is required, and not data is set.'
'isValid() should always return false when no fallback value is present, '
. 'the input is required, and no data is set.'
);
$messages = $input->getMessages();
$this->assertInternalType('array', $messages);
$this->assertArrayHasKey(NotEmpty::IS_EMPTY, $messages);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer compare against the whole getMessages return. By this way we are sure any other message appears


$notEmpty = new NotEmpty();
$r = new ReflectionProperty($notEmpty, 'messageTemplates');
$r->setAccessible(true);
$messageTemplates = $r->getValue($notEmpty);
$this->assertEquals(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about assertAttributeEquals

$messageTemplates[NotEmpty::IS_EMPTY],
$messages[NotEmpty::IS_EMPTY],
'getMessages() values do not match'
);
$this->assertEquals(['fooErrorMessage'], $input->getMessages(), 'getMessages() value not match');
}

public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid()
Expand Down