ConstraintTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. require_once __DIR__.'/Fixtures/ConstraintA.php';
  4. require_once __DIR__.'/Fixtures/ConstraintB.php';
  5. require_once __DIR__.'/Fixtures/ConstraintC.php';
  6. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  7. use Symfony\Tests\Component\Validator\Fixtures\ConstraintB;
  8. use Symfony\Tests\Component\Validator\Fixtures\ConstraintC;
  9. class ConstraintTest extends \PHPUnit_Framework_TestCase
  10. {
  11. public function testSetProperties()
  12. {
  13. $constraint = new ConstraintA(array(
  14. 'property1' => 'foo',
  15. 'property2' => 'bar',
  16. ));
  17. $this->assertEquals('foo', $constraint->property1);
  18. $this->assertEquals('bar', $constraint->property2);
  19. }
  20. public function testSetNotExistingPropertyThrowsException()
  21. {
  22. $this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
  23. new ConstraintA(array(
  24. 'foo' => 'bar',
  25. ));
  26. }
  27. public function testMagicPropertiesAreNotAllowed()
  28. {
  29. $constraint = new ConstraintA();
  30. $this->setExpectedException('Symfony\Component\Validator\Exception\InvalidOptionsException');
  31. $constraint->foo = 'bar';
  32. }
  33. public function testSetDefaultProperty()
  34. {
  35. $constraint = new ConstraintA('foo');
  36. $this->assertEquals('foo', $constraint->property2);
  37. }
  38. public function testSetDefaultPropertyDoctrineStyle()
  39. {
  40. $constraint = new ConstraintA(array('value' => 'foo'));
  41. $this->assertEquals('foo', $constraint->property2);
  42. }
  43. public function testSetUndefinedDefaultProperty()
  44. {
  45. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  46. new ConstraintB('foo');
  47. }
  48. public function testRequiredOptionsMustBeDefined()
  49. {
  50. $this->setExpectedException('Symfony\Component\Validator\Exception\MissingOptionsException');
  51. new ConstraintC();
  52. }
  53. public function testGroupsAreConvertedToArray()
  54. {
  55. $constraint = new ConstraintA(array('groups' => 'Foo'));
  56. $this->assertEquals(array('Foo'), $constraint->groups);
  57. }
  58. public function testAddDefaultGroupAddsGroup()
  59. {
  60. $constraint = new ConstraintA(array('groups' => 'Default'));
  61. $constraint->addImplicitGroupName('Foo');
  62. $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
  63. }
  64. public function testAllowsSettingZeroRequiredPropertyValue()
  65. {
  66. $constraint = new ConstraintA(0);
  67. $this->assertEquals(0, $constraint->property2);
  68. }
  69. public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
  70. {
  71. new ConstraintB(array());
  72. }
  73. }