This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
158 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Zend\InputFilter\Validator; | ||
|
||
use Zend\InputFilter\Input; | ||
use Zend\Validator\AbstractValidator; | ||
|
||
class Required extends AbstractValidator | ||
{ | ||
const INVALID = 'inputInvalid'; | ||
const REQUIRED = 'inputRequired'; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected $messageTemplates = [ | ||
self::INVALID => 'Invalid type given. Zend\InputFilter\Input is required', | ||
self::REQUIRED => 'Value is required', | ||
]; | ||
|
||
public function isValid($value) | ||
{ | ||
if (!($value instanceof Input)) { | ||
$this->error(self::INVALID); | ||
return false; | ||
} | ||
|
||
$input = $value; | ||
|
||
if ($input->hasValue()) { // If has value then all is ok | ||
return true; | ||
} | ||
|
||
if ($input->isRequired()) { // It's Required and value was not set. | ||
$this->error(self::REQUIRED); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace ZendTest\InputFilter\Validator; | ||
|
||
use PHPUnit_Framework_MockObject_MockObject as MockObject; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\InputFilter\Input; | ||
use Zend\InputFilter\Validator\Required; | ||
|
||
class RequiredTest extends TestCase | ||
{ | ||
/** | ||
* @var Required | ||
*/ | ||
protected $validator; | ||
|
||
protected function setUp() | ||
{ | ||
$this->validator = new Required(); | ||
} | ||
|
||
/** | ||
* @dataProvider inputProvider | ||
*/ | ||
public function testValid($input, $expectedIsValid, $expectedMessages) | ||
{ | ||
$this->assertEquals( | ||
$expectedIsValid, | ||
$this->validator->isValid($input), | ||
'isValid() value not match. Detail: ' . json_encode($this->validator->getMessages()) | ||
); | ||
|
||
$this->assertEquals( | ||
$expectedMessages, | ||
$this->validator->getMessages(), | ||
'getMessages() value not match.' | ||
); | ||
} | ||
|
||
public function inputProvider() | ||
{ | ||
$requiredMsg = [ | ||
Required::REQUIRED => 'Value is required', | ||
]; | ||
|
||
// @codingStandardsIgnoreStart | ||
return [ | ||
// Description => [$input, isValid, getMessages] | ||
'Required: T. Value: Set' => [$this->createInputMock(true, true) , true , []], | ||
'Required: T. Value: Not set' => [$this->createInputMock(true, false) , false, $requiredMsg], | ||
'Required: F. Value: set' => [$this->createInputMock(false, true) , true , []], | ||
'Required: F. Value: Not set' => [$this->createInputMock(false, false), true , []], | ||
]; | ||
// @codingStandardsIgnoreEnd | ||
} | ||
|
||
/** | ||
* @param bool $required | ||
* @param bool $hasValue | ||
* | ||
* @return Input|MockObject | ||
*/ | ||
protected function createInputMock($required, $hasValue) | ||
{ | ||
/** @var Input|MockObject $input */ | ||
$input = $this->getMock(Input::class); | ||
|
||
$input->method('isRequired') | ||
->willReturn($required) | ||
; | ||
|
||
$input->method('hasValue') | ||
->willReturn($hasValue) | ||
; | ||
|
||
return $input; | ||
} | ||
} |