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

Commit ff81186

Browse files
committed
Fixed validation failure messages for required input
- Updates the test case to create an assertion method; the method now uses `getOption('messageTemplates')` instead of reflection to be less brittle, and is used in two separate test methods. - Updated `Input` to add a `prepareRequiredValidationFailureMessage()` method taht creates the required message format. Each of `Input`, `ArrayInput`, and `FileInput` now assign the return of that method to the `$errorMessage` property directly.
1 parent edc58cb commit ff81186

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

src/ArrayInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function isValid($context = null)
7171

7272
if (! $hasValue && $required) {
7373
if ($this->errorMessage === null) {
74-
$this->setErrorMessage('Value is required');
74+
$this->errorMessage = $this->prepareRequiredValidationFailureMessage();
7575
}
7676
return false;
7777
}

src/FileInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function isValid($context = null)
125125

126126
if (! $hasValue && $required && ! $this->hasFallback()) {
127127
if ($this->errorMessage === null) {
128-
$this->setErrorMessage('Value is required');
128+
$this->errorMessage = $this->prepareRequiredValidationFailureMessage();
129129
}
130130
return false;
131131
}

src/Input.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public function isValid($context = null)
404404

405405
if (! $hasValue && $required) {
406406
if ($this->errorMessage === null) {
407-
$this->setErrorMessage('Value is required');
407+
$this->errorMessage = $this->prepareRequiredValidationFailureMessage();
408408
}
409409
return false;
410410
}
@@ -483,4 +483,18 @@ protected function injectNotEmptyValidator()
483483

484484
$chain->prependValidator(new NotEmpty(), true);
485485
}
486+
487+
/**
488+
* Create and return the validation failure message for required input.
489+
*
490+
* @return string[]
491+
*/
492+
protected function prepareRequiredValidationFailureMessage()
493+
{
494+
$notEmpty = new NotEmpty();
495+
$templates = $notEmpty->getOption('messageTemplates');
496+
return [
497+
NotEmpty::IS_EMPTY => $templates[NotEmpty::IS_EMPTY],
498+
];
499+
}
486500
}

test/InputTest.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1313
use PHPUnit_Framework_TestCase as TestCase;
14-
use ReflectionProperty;
1514
use stdClass;
1615
use Zend\Filter;
1716
use Zend\Filter\FilterChain;
@@ -36,6 +35,24 @@ public function setUp()
3635
$this->input = new Input('foo');
3736
}
3837

38+
public function assertRequiredValidationErrorMessage($input, $message = '')
39+
{
40+
$message = $message ?: 'Expected failure message for required input';
41+
$message .= ';';
42+
43+
$messages = $input->getMessages();
44+
$this->assertInternalType('array', $messages, $message . ' non-array messages array');
45+
$this->assertArrayHasKey(NotEmpty::IS_EMPTY, $messages, $message . ' missing NotEmpty::IS_EMPTY key');
46+
47+
$notEmpty = new NotEmpty();
48+
$messageTemplates = $notEmpty->getOption('messageTemplates');
49+
$this->assertEquals(
50+
$messageTemplates[NotEmpty::IS_EMPTY],
51+
$messages[NotEmpty::IS_EMPTY],
52+
$message . ' NotEmpty::IS_EMPTY message differs'
53+
);
54+
}
55+
3956
public function testConstructorRequiresAName()
4057
{
4158
$this->assertEquals('foo', $this->input->getName());
@@ -174,7 +191,7 @@ public function testRequiredWithoutFallbackAndValueNotSetThenFail()
174191
$input->isValid(),
175192
'isValid() should be return always false when no fallback value, is required, and not data is set.'
176193
);
177-
$this->assertEquals(['Value is required'], $input->getMessages(), 'getMessages() value not match');
194+
$this->assertRequiredValidationErrorMessage($input);
178195
}
179196

180197
/**
@@ -191,19 +208,7 @@ public function testRequiredWithoutFallbackAndValueNotSetProvidesNotEmptyValidat
191208
'isValid() should always return false when no fallback value is present, '
192209
. 'the input is required, and no data is set.'
193210
);
194-
$messages = $input->getMessages();
195-
$this->assertInternalType('array', $messages);
196-
$this->assertArrayHasKey(NotEmpty::IS_EMPTY, $messages);
197-
198-
$notEmpty = new NotEmpty();
199-
$r = new ReflectionProperty($notEmpty, 'messageTemplates');
200-
$r->setAccessible(true);
201-
$messageTemplates = $r->getValue($notEmpty);
202-
$this->assertEquals(
203-
$messageTemplates[NotEmpty::IS_EMPTY],
204-
$messages[NotEmpty::IS_EMPTY],
205-
'getMessages() values do not match'
206-
);
211+
$this->assertRequiredValidationErrorMessage($input);
207212
}
208213

209214
public function testNotRequiredWithoutFallbackAndValueNotSetThenIsValid()

0 commit comments

Comments
 (0)