MaxValidatorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Constraints;
  11. use Symfony\Component\Validator\Constraints\Max;
  12. use Symfony\Component\Validator\Constraints\MaxValidator;
  13. class MaxValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new MaxValidator();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new Max(array('limit' => 10))));
  27. }
  28. public function testExpectsNumericType()
  29. {
  30. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  31. $this->validator->isValid(new \stdClass(), new Max(array('limit' => 10)));
  32. }
  33. /**
  34. * @dataProvider getValidValues
  35. */
  36. public function testValidValues($value)
  37. {
  38. $constraint = new Max(array('limit' => 10));
  39. $this->assertTrue($this->validator->isValid($value, $constraint));
  40. }
  41. public function getValidValues()
  42. {
  43. return array(
  44. array(9.999999),
  45. array(10),
  46. array(10.0),
  47. array('10'),
  48. );
  49. }
  50. /**
  51. * @dataProvider getInvalidValues
  52. */
  53. public function testInvalidValues($value)
  54. {
  55. $constraint = new Max(array('limit' => 10));
  56. $this->assertFalse($this->validator->isValid($value, $constraint));
  57. }
  58. public function getInvalidValues()
  59. {
  60. return array(
  61. array(10.00001),
  62. array('10.00001'),
  63. );
  64. }
  65. public function testMessageIsSet()
  66. {
  67. $constraint = new Max(array(
  68. 'limit' => 10,
  69. 'message' => 'myMessage'
  70. ));
  71. $this->assertFalse($this->validator->isValid(11, $constraint));
  72. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  73. $this->assertEquals($this->validator->getMessageParameters(), array(
  74. '{{ value }}' => 11,
  75. '{{ limit }}' => 10,
  76. ));
  77. }
  78. public function testConstraintGetDefaultOption()
  79. {
  80. $constraint = new Max(array(
  81. 'limit' => 10,
  82. ));
  83. $this->assertEquals('limit', $constraint->getDefaultOption());
  84. }
  85. }