ConstraintTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
  14. use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
  15. use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
  16. use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
  17. use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValue;
  18. use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValueAsDefault;
  19. class ConstraintTest extends TestCase
  20. {
  21. public function testSetProperties()
  22. {
  23. $constraint = new ConstraintA(array(
  24. 'property1' => 'foo',
  25. 'property2' => 'bar',
  26. ));
  27. $this->assertEquals('foo', $constraint->property1);
  28. $this->assertEquals('bar', $constraint->property2);
  29. }
  30. public function testSetNotExistingPropertyThrowsException()
  31. {
  32. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
  33. new ConstraintA(array(
  34. 'foo' => 'bar',
  35. ));
  36. }
  37. public function testMagicPropertiesAreNotAllowed()
  38. {
  39. $constraint = new ConstraintA();
  40. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
  41. $constraint->foo = 'bar';
  42. }
  43. public function testInvalidAndRequiredOptionsPassed()
  44. {
  45. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\InvalidOptionsException');
  46. new ConstraintC(array(
  47. 'option1' => 'default',
  48. 'foo' => 'bar',
  49. ));
  50. }
  51. public function testSetDefaultProperty()
  52. {
  53. $constraint = new ConstraintA('foo');
  54. $this->assertEquals('foo', $constraint->property2);
  55. }
  56. public function testSetDefaultPropertyDoctrineStyle()
  57. {
  58. $constraint = new ConstraintA(array('value' => 'foo'));
  59. $this->assertEquals('foo', $constraint->property2);
  60. }
  61. public function testSetDefaultPropertyDoctrineStylePlusOtherProperty()
  62. {
  63. $constraint = new ConstraintA(array('value' => 'foo', 'property1' => 'bar'));
  64. $this->assertEquals('foo', $constraint->property2);
  65. $this->assertEquals('bar', $constraint->property1);
  66. }
  67. public function testSetDefaultPropertyDoctrineStyleWhenDefaultPropertyIsNamedValue()
  68. {
  69. $constraint = new ConstraintWithValueAsDefault(array('value' => 'foo'));
  70. $this->assertEquals('foo', $constraint->value);
  71. $this->assertNull($constraint->property);
  72. }
  73. public function testDontSetDefaultPropertyIfValuePropertyExists()
  74. {
  75. $constraint = new ConstraintWithValue(array('value' => 'foo'));
  76. $this->assertEquals('foo', $constraint->value);
  77. $this->assertNull($constraint->property);
  78. }
  79. public function testSetUndefinedDefaultProperty()
  80. {
  81. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  82. new ConstraintB('foo');
  83. }
  84. public function testRequiredOptionsMustBeDefined()
  85. {
  86. $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\MissingOptionsException');
  87. new ConstraintC();
  88. }
  89. public function testRequiredOptionsPassed()
  90. {
  91. $constraint = new ConstraintC(array('option1' => 'default'));
  92. $this->assertSame('default', $constraint->option1);
  93. }
  94. public function testGroupsAreConvertedToArray()
  95. {
  96. $constraint = new ConstraintA(array('groups' => 'Foo'));
  97. $this->assertEquals(array('Foo'), $constraint->groups);
  98. }
  99. public function testAddDefaultGroupAddsGroup()
  100. {
  101. $constraint = new ConstraintA(array('groups' => 'Default'));
  102. $constraint->addImplicitGroupName('Foo');
  103. $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
  104. }
  105. public function testAllowsSettingZeroRequiredPropertyValue()
  106. {
  107. $constraint = new ConstraintA(0);
  108. $this->assertEquals(0, $constraint->property2);
  109. }
  110. public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
  111. {
  112. $constraint = new ConstraintB(array());
  113. $this->assertSame(array(Constraint::PROPERTY_CONSTRAINT, Constraint::CLASS_CONSTRAINT), $constraint->getTargets());
  114. }
  115. public function testGetTargetsCanBeString()
  116. {
  117. $constraint = new ClassConstraint();
  118. $this->assertEquals('class', $constraint->getTargets());
  119. }
  120. public function testGetTargetsCanBeArray()
  121. {
  122. $constraint = new ConstraintA();
  123. $this->assertEquals(array('property', 'class'), $constraint->getTargets());
  124. }
  125. public function testSerialize()
  126. {
  127. $constraint = new ConstraintA(array(
  128. 'property1' => 'foo',
  129. 'property2' => 'bar',
  130. ));
  131. $restoredConstraint = unserialize(serialize($constraint));
  132. $this->assertEquals($constraint, $restoredConstraint);
  133. }
  134. public function testSerializeInitializesGroupsOptionToDefault()
  135. {
  136. $constraint = new ConstraintA(array(
  137. 'property1' => 'foo',
  138. 'property2' => 'bar',
  139. ));
  140. $constraint = unserialize(serialize($constraint));
  141. $expected = new ConstraintA(array(
  142. 'property1' => 'foo',
  143. 'property2' => 'bar',
  144. 'groups' => 'Default',
  145. ));
  146. $this->assertEquals($expected, $constraint);
  147. }
  148. public function testSerializeKeepsCustomGroups()
  149. {
  150. $constraint = new ConstraintA(array(
  151. 'property1' => 'foo',
  152. 'property2' => 'bar',
  153. 'groups' => 'MyGroup',
  154. ));
  155. $constraint = unserialize(serialize($constraint));
  156. $this->assertSame(array('MyGroup'), $constraint->groups);
  157. }
  158. /**
  159. * @expectedException \Symfony\Component\Validator\Exception\InvalidArgumentException
  160. */
  161. public function testGetErrorNameForUnknownCode()
  162. {
  163. Constraint::getErrorName(1);
  164. }
  165. public function testOptionsAsDefaultOption()
  166. {
  167. $constraint = new ConstraintA($options = array('value1'));
  168. $this->assertEquals($options, $constraint->property2);
  169. $constraint = new ConstraintA($options = array('value1', 'property1' => 'value2'));
  170. $this->assertEquals($options, $constraint->property2);
  171. }
  172. /**
  173. * @expectedException \Symfony\Component\Validator\Exception\InvalidOptionsException
  174. * @expectedExceptionMessage The options "0", "5" do not exist
  175. */
  176. public function testInvalidOptions()
  177. {
  178. new ConstraintA(array('property2' => 'foo', 'bar', 5 => 'baz'));
  179. }
  180. public function testOptionsWithInvalidInternalPointer()
  181. {
  182. $options = array('property1' => 'foo');
  183. next($options);
  184. next($options);
  185. $constraint = new ConstraintA($options);
  186. $this->assertEquals('foo', $constraint->property1);
  187. }
  188. }