|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @see https://github.com/zendframework/zend-form for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com) |
| 5 | + * @license https://github.com/zendframework/zend-form/blob/master/LICENSE.md New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +namespace ZendTest\Form\Integration; |
| 9 | + |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Zend\Form\Form; |
| 12 | +use Zend\Form\InputFilterProviderFieldset; |
| 13 | +use Zend\Validator; |
| 14 | + |
| 15 | +class FormCreatesCollectionInputFilterTest extends TestCase |
| 16 | +{ |
| 17 | + public static function assertValidatorFound($class, array $validators, $message = null) |
| 18 | + { |
| 19 | + $message = $message ?: sprintf('Failed to find validator of type %s in validator list', $class); |
| 20 | + foreach ($validators as $instance) { |
| 21 | + $validator = $instance['instance']; |
| 22 | + if ($validator instanceof $class) { |
| 23 | + return true; |
| 24 | + } |
| 25 | + } |
| 26 | + var_export($validators); |
| 27 | + self::fail($message); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @see https://github.com/zendframework/zend-form/issues/78 |
| 32 | + */ |
| 33 | + public function testCollectionInputFilterContainsExpectedValidators() |
| 34 | + { |
| 35 | + $form = new Form; |
| 36 | + $form->add([ |
| 37 | + 'name' => 'collection', |
| 38 | + 'type' => 'collection', |
| 39 | + 'options' => [ |
| 40 | + 'target_element' => [ |
| 41 | + 'type' => InputFilterProviderFieldset::class, |
| 42 | + 'elements' => [ |
| 43 | + [ |
| 44 | + 'spec' => [ |
| 45 | + 'name' => 'date', |
| 46 | + 'type' => 'date' |
| 47 | + ], |
| 48 | + ], |
| 49 | + ], |
| 50 | + 'options' => ['input_filter_spec' => [ |
| 51 | + 'date' => [ |
| 52 | + 'required' => false, |
| 53 | + 'validators' => [ |
| 54 | + ['name' => 'StringLength'], |
| 55 | + ], |
| 56 | + ], |
| 57 | + ]], |
| 58 | + ], |
| 59 | + ], |
| 60 | + ]); |
| 61 | + $inputFilter = $form->getInputFilter(); |
| 62 | + $filter = $inputFilter->get('collection')->getInputFilter()->get('date'); |
| 63 | + |
| 64 | + $validators = $filter->getValidatorChain()->getValidators(); |
| 65 | + $this->assertCount(3, $validators); |
| 66 | + $this->assertValidatorFound(Validator\StringLength::class, $validators); |
| 67 | + $this->assertValidatorFound(Validator\Date::class, $validators); |
| 68 | + $this->assertValidatorFound(Validator\DateStep::class, $validators); |
| 69 | + |
| 70 | + return $form; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @depends testCollectionInputFilterContainsExpectedValidators |
| 75 | + */ |
| 76 | + public function testCollectionElementDoesNotCreateDiscreteElementInInputFilter(Form $form) |
| 77 | + { |
| 78 | + $inputFilter = $form->getInputFilter(); |
| 79 | + $this->assertFalse($inputFilter->has('date')); |
| 80 | + } |
| 81 | +} |
0 commit comments