MinValidatorTest.php 2.4 KB

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