ConstraintTest.php 2.9 KB

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