ConstraintTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Tests\Component\Validator;
  11. require_once __DIR__.'/Fixtures/ClassConstraint.php';
  12. require_once __DIR__.'/Fixtures/ConstraintA.php';
  13. require_once __DIR__.'/Fixtures/ConstraintB.php';
  14. require_once __DIR__.'/Fixtures/ConstraintC.php';
  15. use Symfony\Tests\Component\Validator\Fixtures\ClassConstraint;
  16. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  17. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  18. use Symfony\Tests\Component\Validator\Fixtures\ConstraintC;
  19. class ConstraintTest extends \PHPUnit_Framework_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->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->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
  41. $constraint->foo = 'bar';
  42. }
  43. public function testInvalidAndRequiredOptionsPassed()
  44. {
  45. $this->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 testSetUndefinedDefaultProperty()
  62. {
  63. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  64. new ConstraintB('foo');
  65. }
  66. public function testRequiredOptionsMustBeDefined()
  67. {
  68. $this->setExpectedException('Symfony\Component\Validator\Exception\MissingOptionsException');
  69. new ConstraintC();
  70. }
  71. public function testRequiredOptionsPassed()
  72. {
  73. new ConstraintC(array('option1' => 'default'));
  74. }
  75. public function testGroupsAreConvertedToArray()
  76. {
  77. $constraint = new ConstraintA(array('groups' => 'Foo'));
  78. $this->assertEquals(array('Foo'), $constraint->groups);
  79. }
  80. public function testAddDefaultGroupAddsGroup()
  81. {
  82. $constraint = new ConstraintA(array('groups' => 'Default'));
  83. $constraint->addImplicitGroupName('Foo');
  84. $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
  85. }
  86. public function testAllowsSettingZeroRequiredPropertyValue()
  87. {
  88. $constraint = new ConstraintA(0);
  89. $this->assertEquals(0, $constraint->property2);
  90. }
  91. public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
  92. {
  93. new ConstraintB(array());
  94. }
  95. public function testGetTargetsCanBeString()
  96. {
  97. $constraint = new ClassConstraint;
  98. $this->assertEquals('class', $constraint->getTargets());
  99. }
  100. public function testGetTargetsCanBeArray()
  101. {
  102. $constraint = new ConstraintA;
  103. $this->assertEquals(array('property', 'class'), $constraint->getTargets());
  104. }
  105. }