CollectionTest.php 3.0 KB

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