ConstraintTest.php 2.5 KB

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