MaxValidatorTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public function testNullIsValid()
  21. {
  22. $this->assertTrue($this->validator->isValid(null, new Max(array('limit' => 10))));
  23. }
  24. public function testExpectsNumericType()
  25. {
  26. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  27. $this->validator->isValid(new \stdClass(), new Max(array('limit' => 10)));
  28. }
  29. /**
  30. * @dataProvider getValidValues
  31. */
  32. public function testValidValues($value)
  33. {
  34. $constraint = new Max(array('limit' => 10));
  35. $this->assertTrue($this->validator->isValid($value, $constraint));
  36. }
  37. public function getValidValues()
  38. {
  39. return array(
  40. array(9.999999),
  41. array(10),
  42. array(10.0),
  43. array('10'),
  44. );
  45. }
  46. /**
  47. * @dataProvider getInvalidValues
  48. */
  49. public function testInvalidValues($value)
  50. {
  51. $constraint = new Max(array('limit' => 10));
  52. $this->assertFalse($this->validator->isValid($value, $constraint));
  53. }
  54. public function getInvalidValues()
  55. {
  56. return array(
  57. array(10.00001),
  58. array('10.00001'),
  59. );
  60. }
  61. public function testMessageIsSet()
  62. {
  63. $constraint = new Max(array(
  64. 'limit' => 10,
  65. 'message' => 'myMessage'
  66. ));
  67. $this->assertFalse($this->validator->isValid(11, $constraint));
  68. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  69. $this->assertEquals($this->validator->getMessageParameters(), array(
  70. '{{ value }}' => 11,
  71. '{{ limit }}' => 10,
  72. ));
  73. }
  74. }