|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Zend Framework (http://framework.zend.com/) |
| 4 | + * |
| 5 | + * @link http://github.com/zendframework/zf2 for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
| 7 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ZendTest\InputFilter; |
| 11 | + |
| 12 | +use ArrayIterator; |
| 13 | +use Zend\InputFilter\Input; |
| 14 | +use Zend\InputFilter\InputFilter; |
| 15 | +use Zend\InputFilter\InputFilterInterface; |
| 16 | +use Zend\InputFilter\OptionalInputFilter; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * @covers \Zend\InputFilter\OptionalInputFilter |
| 21 | + */ |
| 22 | +class OptionalInputFilterTest extends TestCase |
| 23 | +{ |
| 24 | + public function testValidatesSuccessfullyWhenSetDataIsNeverCalled() |
| 25 | + { |
| 26 | + $this->assertTrue($this->getNestedCarInputFilter()->get('car')->isValid()); |
| 27 | + } |
| 28 | + |
| 29 | + public function testValidatesSuccessfullyWhenValidNonEmptyDataSetProvided() |
| 30 | + { |
| 31 | + $data = [ |
| 32 | + 'car' => [ |
| 33 | + 'brand' => 'Volkswagen', |
| 34 | + 'model' => 'Golf', |
| 35 | + ] |
| 36 | + ]; |
| 37 | + |
| 38 | + $inputFilter = $this->getNestedCarInputFilter(); |
| 39 | + $inputFilter->setData($data); |
| 40 | + |
| 41 | + $this->assertTrue($inputFilter->isValid()); |
| 42 | + $this->assertEquals($data, $inputFilter->getValues()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testValidatesSuccessfullyWhenEmptyDataSetProvided() |
| 46 | + { |
| 47 | + $data = [ |
| 48 | + 'car' => null, |
| 49 | + ]; |
| 50 | + |
| 51 | + $inputFilter = $this->getNestedCarInputFilter(); |
| 52 | + $inputFilter->setData($data); |
| 53 | + |
| 54 | + $this->assertTrue($inputFilter->isValid()); |
| 55 | + $this->assertEquals($data, $inputFilter->getValues()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testValidatesSuccessfullyWhenNoDataProvided() |
| 59 | + { |
| 60 | + $data = []; |
| 61 | + |
| 62 | + $inputFilter = $this->getNestedCarInputFilter(); |
| 63 | + $inputFilter->setData($data); |
| 64 | + |
| 65 | + $this->assertTrue($inputFilter->isValid()); |
| 66 | + $this->assertEquals(['car' => null], $inputFilter->getValues()); |
| 67 | + } |
| 68 | + |
| 69 | + public function testValidationFailureWhenInvalidDataSetIsProvided() |
| 70 | + { |
| 71 | + $inputFilter = $this->getNestedCarInputFilter(); |
| 72 | + $inputFilter->setData([ |
| 73 | + 'car' => [ |
| 74 | + 'brand' => 'Volkswagen', |
| 75 | + ] |
| 76 | + ]); |
| 77 | + |
| 78 | + $this->assertFalse($inputFilter->isValid()); |
| 79 | + $this->assertGetValuesThrows($inputFilter); |
| 80 | + } |
| 81 | + |
| 82 | + public function testStateIsClearedBetweenValidationAttempts() |
| 83 | + { |
| 84 | + $data = [ |
| 85 | + 'car' => null, |
| 86 | + ]; |
| 87 | + |
| 88 | + $inputFilter = $this->getNestedCarInputFilter(); |
| 89 | + $inputFilter->setData($data); |
| 90 | + |
| 91 | + $this->assertTrue($inputFilter->isValid()); |
| 92 | + $this->assertEquals($data, $inputFilter->getValues()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * We are doing some boolean shenanigans in the implementation |
| 97 | + * we want to check that Iterator objects work the same as arrays |
| 98 | + */ |
| 99 | + public function testIteratorBehavesTheSameAsArray() |
| 100 | + { |
| 101 | + $optionalInputFilter = new OptionalInputFilter; |
| 102 | + $optionalInputFilter->add(new Input('brand')); |
| 103 | + |
| 104 | + $optionalInputFilter->setData(['model' => 'Golf']); |
| 105 | + $this->assertFalse($optionalInputFilter->isValid()); |
| 106 | + |
| 107 | + $optionalInputFilter->setData(new ArrayIterator([])); |
| 108 | + $this->assertTrue($optionalInputFilter->isValid()); |
| 109 | + |
| 110 | + $optionalInputFilter->setData([]); |
| 111 | + $this->assertTrue($optionalInputFilter->isValid()); |
| 112 | + } |
| 113 | + |
| 114 | + protected function assertGetValuesThrows(InputFilterInterface $inputFilter) |
| 115 | + { |
| 116 | + try { |
| 117 | + $inputFilter->getValues(); |
| 118 | + $this->assertTrue(false); |
| 119 | + // TODO: issue #143 narrow which exception should be thrown |
| 120 | + } catch (\Exception $exception) { |
| 121 | + $this->assertTrue(true); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private $nestedCarInputFilter; |
| 126 | + |
| 127 | + protected function getNestedCarInputFilter() |
| 128 | + { |
| 129 | + if (! $this->nestedCarInputFilter) { |
| 130 | + $optionalInputFilter = new OptionalInputFilter; |
| 131 | + $optionalInputFilter->add(new Input('brand')); |
| 132 | + $optionalInputFilter->add(new Input('model')); |
| 133 | + |
| 134 | + $this->nestedCarInputFilter = new InputFilter; |
| 135 | + $this->nestedCarInputFilter->add($optionalInputFilter, 'car'); |
| 136 | + } |
| 137 | + |
| 138 | + return $this->nestedCarInputFilter; |
| 139 | + } |
| 140 | +} |
0 commit comments