MaxValidatorTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\Max;
  4. use Symfony\Component\Validator\Constraints\MaxValidator;
  5. class MaxValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. public function setUp()
  9. {
  10. $this->validator = new MaxValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new Max(array('limit' => 10))));
  15. }
  16. public function testExpectsNumericType()
  17. {
  18. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  19. $this->validator->isValid(new \stdClass(), new Max(array('limit' => 10)));
  20. }
  21. /**
  22. * @dataProvider getValidValues
  23. */
  24. public function testValidValues($value)
  25. {
  26. $constraint = new Max(array('limit' => 10));
  27. $this->assertTrue($this->validator->isValid($value, $constraint));
  28. }
  29. public function getValidValues()
  30. {
  31. return array(
  32. array(9.999999),
  33. array(10),
  34. array(10.0),
  35. array('10'),
  36. );
  37. }
  38. /**
  39. * @dataProvider getInvalidValues
  40. */
  41. public function testInvalidValues($value)
  42. {
  43. $constraint = new Max(array('limit' => 10));
  44. $this->assertFalse($this->validator->isValid($value, $constraint));
  45. }
  46. public function getInvalidValues()
  47. {
  48. return array(
  49. array(10.00001),
  50. array('10.00001'),
  51. );
  52. }
  53. public function testMessageIsSet()
  54. {
  55. $constraint = new Max(array(
  56. 'limit' => 10,
  57. 'message' => 'myMessage'
  58. ));
  59. $this->assertFalse($this->validator->isValid(11, $constraint));
  60. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  61. $this->assertEquals($this->validator->getMessageParameters(), array(
  62. '{{ value }}' => 11,
  63. '{{ limit }}' => 10,
  64. ));
  65. }
  66. }