NotBlankValidatorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Validator\Constraints\NotBlank;
  12. use Symfony\Component\Validator\Constraints\NotBlankValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class NotBlankValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new NotBlankValidator();
  23. }
  24. /**
  25. * @dataProvider getValidValues
  26. */
  27. public function testValidValues($value)
  28. {
  29. $this->validator->validate($value, new NotBlank());
  30. $this->assertNoViolation();
  31. }
  32. public function getValidValues()
  33. {
  34. return array(
  35. array('foobar'),
  36. array(0),
  37. array(0.0),
  38. array('0'),
  39. array(1234),
  40. );
  41. }
  42. public function testNullIsInvalid()
  43. {
  44. $constraint = new NotBlank(array(
  45. 'message' => 'myMessage',
  46. ));
  47. $this->validator->validate(null, $constraint);
  48. $this->buildViolation('myMessage')
  49. ->setParameter('{{ value }}', 'null')
  50. ->setCode(NotBlank::IS_BLANK_ERROR)
  51. ->assertRaised();
  52. }
  53. public function testBlankIsInvalid()
  54. {
  55. $constraint = new NotBlank(array(
  56. 'message' => 'myMessage',
  57. ));
  58. $this->validator->validate('', $constraint);
  59. $this->buildViolation('myMessage')
  60. ->setParameter('{{ value }}', '""')
  61. ->setCode(NotBlank::IS_BLANK_ERROR)
  62. ->assertRaised();
  63. }
  64. public function testFalseIsInvalid()
  65. {
  66. $constraint = new NotBlank(array(
  67. 'message' => 'myMessage',
  68. ));
  69. $this->validator->validate(false, $constraint);
  70. $this->buildViolation('myMessage')
  71. ->setParameter('{{ value }}', 'false')
  72. ->setCode(NotBlank::IS_BLANK_ERROR)
  73. ->assertRaised();
  74. }
  75. public function testEmptyArrayIsInvalid()
  76. {
  77. $constraint = new NotBlank(array(
  78. 'message' => 'myMessage',
  79. ));
  80. $this->validator->validate(array(), $constraint);
  81. $this->buildViolation('myMessage')
  82. ->setParameter('{{ value }}', 'array')
  83. ->setCode(NotBlank::IS_BLANK_ERROR)
  84. ->assertRaised();
  85. }
  86. }