|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2017 DarkWeb Design |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | + * SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace DarkWebDesign\SymfonyAddon\Constraint\Tests; |
| 22 | + |
| 23 | +use DarkWebDesign\SymfonyAddon\Constraint\Collection; |
| 24 | +use DarkWebDesign\SymfonyAddon\Constraint\CollectionValidator; |
| 25 | +use DarkWebDesign\SymfonyAddon\Constraint\Tests\Models\ToStringObject; |
| 26 | +use DarkWebDesign\SymfonyAddon\Constraint\Tests\Models\TraversableObject; |
| 27 | +use PHPUnit_Framework_TestCase; |
| 28 | +use stdClass; |
| 29 | +use Symfony\Component\Validator\Constraints as Assert; |
| 30 | + |
| 31 | +class CollectionValidatorTest extends PHPUnit_Framework_TestCase |
| 32 | +{ |
| 33 | + /** @var \Symfony\Component\Validator\ExecutionContext */ |
| 34 | + private $context; |
| 35 | + |
| 36 | + /** @var \DarkWebDesign\SymfonyAddon\Constraint\CollectionValidator */ |
| 37 | + private $validator; |
| 38 | + |
| 39 | + protected function setUp() |
| 40 | + { |
| 41 | + $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false); |
| 42 | + $this->validator = new CollectionValidator(); |
| 43 | + $this->validator->initialize($this->context); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param array $value |
| 48 | + * |
| 49 | + * @dataProvider providerValidCollection |
| 50 | + */ |
| 51 | + public function testValidate($value) |
| 52 | + { |
| 53 | + $constraints = array( |
| 54 | + new Assert\Email(), |
| 55 | + new Assert\NotBlank(), |
| 56 | + ); |
| 57 | + |
| 58 | + $i = 0; |
| 59 | + |
| 60 | + foreach ($value as $field => $fieldValue) { |
| 61 | + foreach ($constraints as $constraint) { |
| 62 | + $this->context->expects($this->at($i++)) |
| 63 | + ->method('validateValue') |
| 64 | + ->with($fieldValue, $constraint, '[' . $field . ']'); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + $this->validator->validate($value, new Collection($constraints)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 73 | + */ |
| 74 | + public function testValidateNoCollectionConstraint() |
| 75 | + { |
| 76 | + $this->context |
| 77 | + ->expects($this->never()) |
| 78 | + ->method('validateValue'); |
| 79 | + |
| 80 | + $this->validator->validate(array(), new Assert\NotNull()); |
| 81 | + } |
| 82 | + |
| 83 | + public function testValidateNull() |
| 84 | + { |
| 85 | + $this->context |
| 86 | + ->expects($this->never()) |
| 87 | + ->method('validateValue'); |
| 88 | + |
| 89 | + $this->validator->validate(null, new Collection(array( |
| 90 | + new Assert\NotBlank(), |
| 91 | + ))); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param mixed $value |
| 96 | + * |
| 97 | + * @dataProvider providerNoArray |
| 98 | + * |
| 99 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 100 | + */ |
| 101 | + public function testValidateNoArray($value) |
| 102 | + { |
| 103 | + $this->context |
| 104 | + ->expects($this->never()) |
| 105 | + ->method('validateValue'); |
| 106 | + |
| 107 | + $this->validator->validate($value, new Collection(array( |
| 108 | + new Assert\NotBlank(), |
| 109 | + ))); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return array[] |
| 114 | + */ |
| 115 | + public function providerValidCollection() |
| 116 | + { |
| 117 | + return array( |
| 118 | + 'empty' => array(array()), |
| 119 | + 'array' => array( array( '[email protected]')), |
| 120 | + 'traversableObject' => array( new TraversableObject( array( '[email protected]'))), |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return array[] |
| 126 | + */ |
| 127 | + public function providerNoArray() |
| 128 | + { |
| 129 | + return array( |
| 130 | + 'bool' => array(true), |
| 131 | + 'int' => array(1), |
| 132 | + 'float' => array(1.2), |
| 133 | + 'string' => array('foo'), |
| 134 | + 'object' => array(new stdClass()), |
| 135 | + 'resource' => array(tmpfile()), |
| 136 | + 'callable' => array(function () {}) |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments