|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ZendTest\InputFilter\Validator; |
| 4 | + |
| 5 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 6 | +use PHPUnit_Framework_TestCase as TestCase; |
| 7 | +use Zend\InputFilter\Input; |
| 8 | +use Zend\InputFilter\Validator\Required; |
| 9 | + |
| 10 | +class RequiredTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var Required |
| 14 | + */ |
| 15 | + protected $validator; |
| 16 | + |
| 17 | + protected function setUp() |
| 18 | + { |
| 19 | + $this->validator = new Required(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @dataProvider inputProvider |
| 24 | + */ |
| 25 | + public function testValid($input, $expectedIsValid, $expectedMessages) |
| 26 | + { |
| 27 | + $this->assertEquals( |
| 28 | + $expectedIsValid, |
| 29 | + $this->validator->isValid($input), |
| 30 | + 'isValid() value not match. Detail: ' . json_encode($this->validator->getMessages()) |
| 31 | + ); |
| 32 | + |
| 33 | + $this->assertEquals( |
| 34 | + $expectedMessages, |
| 35 | + $this->validator->getMessages(), |
| 36 | + 'getMessages() value not match.' |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public function inputProvider() |
| 41 | + { |
| 42 | + $requiredMsg = [ |
| 43 | + Required::REQUIRED => 'Value is required', |
| 44 | + ]; |
| 45 | + |
| 46 | + // @codingStandardsIgnoreStart |
| 47 | + return [ |
| 48 | + // Description => [$input, isValid, getMessages] |
| 49 | + 'Required: T. Value: Set' => [$this->createInputMock(true, true) , true , []], |
| 50 | + 'Required: T. Value: Not set' => [$this->createInputMock(true, false) , false, $requiredMsg], |
| 51 | + 'Required: F. Value: set' => [$this->createInputMock(false, true) , true , []], |
| 52 | + 'Required: F. Value: Not set' => [$this->createInputMock(false, false), true , []], |
| 53 | + ]; |
| 54 | + // @codingStandardsIgnoreEnd |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param bool $required |
| 59 | + * @param bool $hasValue |
| 60 | + * |
| 61 | + * @return Input|MockObject |
| 62 | + */ |
| 63 | + protected function createInputMock($required, $hasValue) |
| 64 | + { |
| 65 | + /** @var Input|MockObject $input */ |
| 66 | + $input = $this->getMock(Input::class); |
| 67 | + |
| 68 | + $input->method('isRequired') |
| 69 | + ->willReturn($required) |
| 70 | + ; |
| 71 | + |
| 72 | + $input->method('hasValue') |
| 73 | + ->willReturn($hasValue) |
| 74 | + ; |
| 75 | + |
| 76 | + return $input; |
| 77 | + } |
| 78 | +} |
0 commit comments