NotNullValidatorTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\NotNull;
  12. use Symfony\Component\Validator\Constraints\NotNullValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class NotNullValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new NotNullValidator();
  23. }
  24. /**
  25. * @dataProvider getValidValues
  26. */
  27. public function testValidValues($value)
  28. {
  29. $this->validator->validate($value, new NotNull());
  30. $this->assertNoViolation();
  31. }
  32. public function getValidValues()
  33. {
  34. return array(
  35. array(0),
  36. array(false),
  37. array(true),
  38. array(''),
  39. );
  40. }
  41. public function testNullIsInvalid()
  42. {
  43. $constraint = new NotNull(array(
  44. 'message' => 'myMessage',
  45. ));
  46. $this->validator->validate(null, $constraint);
  47. $this->buildViolation('myMessage')
  48. ->setParameter('{{ value }}', 'null')
  49. ->setCode(NotNull::IS_NULL_ERROR)
  50. ->assertRaised();
  51. }
  52. }