MaxValidatorTest.php 2.0 KB

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