ConstraintTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 testSetDefaultProperty()
  44. {
  45. $constraint = new ConstraintA('foo');
  46. $this->assertEquals('foo', $constraint->property2);
  47. }
  48. public function testSetDefaultPropertyDoctrineStyle()
  49. {
  50. $constraint = new ConstraintA(array('value' => 'foo'));
  51. $this->assertEquals('foo', $constraint->property2);
  52. }
  53. public function testSetUndefinedDefaultProperty()
  54. {
  55. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  56. new ConstraintB('foo');
  57. }
  58. public function testRequiredOptionsMustBeDefined()
  59. {
  60. $this->setExpectedException('Symfony\Component\Validator\Exception\MissingOptionsException');
  61. new ConstraintC();
  62. }
  63. public function testRequiredOptionsPassed()
  64. {
  65. new ConstraintC(array('option1' => 'default'));
  66. }
  67. public function testGroupsAreConvertedToArray()
  68. {
  69. $constraint = new ConstraintA(array('groups' => 'Foo'));
  70. $this->assertEquals(array('Foo'), $constraint->groups);
  71. }
  72. public function testAddDefaultGroupAddsGroup()
  73. {
  74. $constraint = new ConstraintA(array('groups' => 'Default'));
  75. $constraint->addImplicitGroupName('Foo');
  76. $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
  77. }
  78. public function testAllowsSettingZeroRequiredPropertyValue()
  79. {
  80. $constraint = new ConstraintA(0);
  81. $this->assertEquals(0, $constraint->property2);
  82. }
  83. public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
  84. {
  85. new ConstraintB(array());
  86. }
  87. public function testGetTargetsCanBeString()
  88. {
  89. $constraint = new ClassConstraint;
  90. $this->assertEquals('class', $constraint->getTargets());
  91. }
  92. public function testGetTargetsCanBeArray()
  93. {
  94. $constraint = new ConstraintA;
  95. $this->assertEquals(array('property', 'class'), $constraint->getTargets());
  96. }
  97. }