1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\Validator\Tests\Constraints; 13 14use PHPUnit\Framework\TestCase; 15use Symfony\Component\Validator\Constraints\Collection; 16use Symfony\Component\Validator\Constraints\Email; 17use Symfony\Component\Validator\Constraints\Optional; 18use Symfony\Component\Validator\Constraints\Required; 19use Symfony\Component\Validator\Constraints\Valid; 20 21/** 22 * @author Bernhard Schussek <bschussek@gmail.com> 23 */ 24class CollectionTest extends TestCase 25{ 26 public function testRejectInvalidFieldsOption() 27 { 28 $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); 29 new Collection([ 30 'fields' => 'foo', 31 ]); 32 } 33 34 public function testRejectNonConstraints() 35 { 36 $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); 37 new Collection([ 38 'foo' => 'bar', 39 ]); 40 } 41 42 public function testRejectValidConstraint() 43 { 44 $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); 45 new Collection([ 46 'foo' => new Valid(), 47 ]); 48 } 49 50 public function testRejectValidConstraintWithinOptional() 51 { 52 $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); 53 new Collection([ 54 'foo' => new Optional(new Valid()), 55 ]); 56 } 57 58 public function testRejectValidConstraintWithinRequired() 59 { 60 $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException'); 61 new Collection([ 62 'foo' => new Required(new Valid()), 63 ]); 64 } 65 66 public function testAcceptOptionalConstraintAsOneElementArray() 67 { 68 $collection1 = new Collection([ 69 'fields' => [ 70 'alternate_email' => [ 71 new Optional(new Email()), 72 ], 73 ], 74 ]); 75 76 $collection2 = new Collection([ 77 'fields' => [ 78 'alternate_email' => new Optional(new Email()), 79 ], 80 ]); 81 82 $this->assertEquals($collection1, $collection2); 83 } 84 85 public function testAcceptRequiredConstraintAsOneElementArray() 86 { 87 $collection1 = new Collection([ 88 'fields' => [ 89 'alternate_email' => [ 90 new Required(new Email()), 91 ], 92 ], 93 ]); 94 95 $collection2 = new Collection([ 96 'fields' => [ 97 'alternate_email' => new Required(new Email()), 98 ], 99 ]); 100 101 $this->assertEquals($collection1, $collection2); 102 } 103 104 public function testConstraintHasDefaultGroupWithOptionalValues() 105 { 106 $constraint = new Collection([ 107 'foo' => new Required(), 108 'bar' => new Optional(), 109 ]); 110 111 $this->assertEquals(['Default'], $constraint->groups); 112 $this->assertEquals(['Default'], $constraint->fields['foo']->groups); 113 $this->assertEquals(['Default'], $constraint->fields['bar']->groups); 114 } 115} 116