MinValidatorTest.php 2.2 KB

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