NotBlankValidatorTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\NotBlank;
  12. use Symfony\Component\Validator\Constraints\NotBlankValidator;
  13. class NotBlankValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new NotBlankValidator();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. /**
  25. * @dataProvider getValidValues
  26. */
  27. public function testValidValues($date)
  28. {
  29. $this->assertTrue($this->validator->isValid($date, new NotBlank()));
  30. }
  31. public function getValidValues()
  32. {
  33. return array(
  34. array('foobar'),
  35. array(0),
  36. array(0.0),
  37. array('0'),
  38. array(1234),
  39. );
  40. }
  41. public function testNullIsInvalid()
  42. {
  43. $this->assertFalse($this->validator->isValid(null, new NotBlank()));
  44. }
  45. public function testBlankIsInvalid()
  46. {
  47. $this->assertFalse($this->validator->isValid('', new NotBlank()));
  48. }
  49. public function testFalseIsInvalid()
  50. {
  51. $this->assertFalse($this->validator->isValid(false, new NotBlank()));
  52. }
  53. public function testEmptyArrayIsInvalid()
  54. {
  55. $this->assertFalse($this->validator->isValid(array(), new NotBlank()));
  56. }
  57. public function testSetMessage()
  58. {
  59. $constraint = new NotBlank(array(
  60. 'message' => 'myMessage'
  61. ));
  62. $this->assertFalse($this->validator->isValid('', $constraint));
  63. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  64. $this->assertEquals($this->validator->getMessageParameters(), array());
  65. }
  66. }