CompositeTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Composite;
  13. use Symfony\Component\Validator\Constraints\NotBlank;
  14. use Symfony\Component\Validator\Constraints\NotNull;
  15. use Symfony\Component\Validator\Constraints\Valid;
  16. class ConcreteComposite extends Composite
  17. {
  18. public $constraints;
  19. protected function getCompositeOption()
  20. {
  21. return 'constraints';
  22. }
  23. public function getDefaultOption()
  24. {
  25. return 'constraints';
  26. }
  27. }
  28. /**
  29. * @author Bernhard Schussek <bschussek@gmail.com>
  30. */
  31. class CompositeTest extends TestCase
  32. {
  33. public function testMergeNestedGroupsIfNoExplicitParentGroup()
  34. {
  35. $constraint = new ConcreteComposite(array(
  36. new NotNull(array('groups' => 'Default')),
  37. new NotBlank(array('groups' => array('Default', 'Strict'))),
  38. ));
  39. $this->assertEquals(array('Default', 'Strict'), $constraint->groups);
  40. $this->assertEquals(array('Default'), $constraint->constraints[0]->groups);
  41. $this->assertEquals(array('Default', 'Strict'), $constraint->constraints[1]->groups);
  42. }
  43. public function testSetImplicitNestedGroupsIfExplicitParentGroup()
  44. {
  45. $constraint = new ConcreteComposite(array(
  46. 'constraints' => array(
  47. new NotNull(),
  48. new NotBlank(),
  49. ),
  50. 'groups' => array('Default', 'Strict'),
  51. ));
  52. $this->assertEquals(array('Default', 'Strict'), $constraint->groups);
  53. $this->assertEquals(array('Default', 'Strict'), $constraint->constraints[0]->groups);
  54. $this->assertEquals(array('Default', 'Strict'), $constraint->constraints[1]->groups);
  55. }
  56. public function testExplicitNestedGroupsMustBeSubsetOfExplicitParentGroups()
  57. {
  58. $constraint = new ConcreteComposite(array(
  59. 'constraints' => array(
  60. new NotNull(array('groups' => 'Default')),
  61. new NotBlank(array('groups' => 'Strict')),
  62. ),
  63. 'groups' => array('Default', 'Strict'),
  64. ));
  65. $this->assertEquals(array('Default', 'Strict'), $constraint->groups);
  66. $this->assertEquals(array('Default'), $constraint->constraints[0]->groups);
  67. $this->assertEquals(array('Strict'), $constraint->constraints[1]->groups);
  68. }
  69. /**
  70. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  71. */
  72. public function testFailIfExplicitNestedGroupsNotSubsetOfExplicitParentGroups()
  73. {
  74. new ConcreteComposite(array(
  75. 'constraints' => array(
  76. new NotNull(array('groups' => array('Default', 'Foobar'))),
  77. ),
  78. 'groups' => array('Default', 'Strict'),
  79. ));
  80. }
  81. public function testImplicitGroupNamesAreForwarded()
  82. {
  83. $constraint = new ConcreteComposite(array(
  84. new NotNull(array('groups' => 'Default')),
  85. new NotBlank(array('groups' => 'Strict')),
  86. ));
  87. $constraint->addImplicitGroupName('ImplicitGroup');
  88. $this->assertEquals(array('Default', 'Strict', 'ImplicitGroup'), $constraint->groups);
  89. $this->assertEquals(array('Default', 'ImplicitGroup'), $constraint->constraints[0]->groups);
  90. $this->assertEquals(array('Strict'), $constraint->constraints[1]->groups);
  91. }
  92. public function testSingleConstraintsAccepted()
  93. {
  94. $nestedConstraint = new NotNull();
  95. $constraint = new ConcreteComposite($nestedConstraint);
  96. $this->assertEquals(array($nestedConstraint), $constraint->constraints);
  97. }
  98. /**
  99. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  100. */
  101. public function testFailIfNoConstraint()
  102. {
  103. new ConcreteComposite(array(
  104. new NotNull(array('groups' => 'Default')),
  105. 'NotBlank',
  106. ));
  107. }
  108. /**
  109. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  110. */
  111. public function testFailIfNoConstraintObject()
  112. {
  113. new ConcreteComposite(array(
  114. new NotNull(array('groups' => 'Default')),
  115. new \ArrayObject(),
  116. ));
  117. }
  118. /**
  119. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  120. */
  121. public function testValidCantBeNested()
  122. {
  123. new ConcreteComposite(array(
  124. new Valid(),
  125. ));
  126. }
  127. }