DateValidatorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Date;
  12. use Symfony\Component\Validator\Constraints\DateValidator;
  13. class DateValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new DateValidator();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new Date()));
  27. }
  28. public function testEmptyStringIsValid()
  29. {
  30. $this->assertTrue($this->validator->isValid('', new Date()));
  31. }
  32. public function testDateTimeClassIsValid()
  33. {
  34. $this->assertTrue($this->validator->isValid(new \DateTime(), new Date()));
  35. }
  36. public function testExpectsStringCompatibleType()
  37. {
  38. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  39. $this->validator->isValid(new \stdClass(), new Date());
  40. }
  41. /**
  42. * @dataProvider getValidDates
  43. */
  44. public function testValidDates($date)
  45. {
  46. $this->assertTrue($this->validator->isValid($date, new Date()));
  47. }
  48. public function getValidDates()
  49. {
  50. return array(
  51. array('2010-01-01'),
  52. array('1955-12-12'),
  53. array('2030-05-31'),
  54. );
  55. }
  56. /**
  57. * @dataProvider getInvalidDates
  58. */
  59. public function testInvalidDates($date)
  60. {
  61. $this->assertFalse($this->validator->isValid($date, new Date()));
  62. $this->assertEquals('This value is not a valid date', $this->validator->getMessageTemplate());
  63. }
  64. public function getInvalidDates()
  65. {
  66. return array(
  67. array('foobar'),
  68. array('foobar 2010-13-01'),
  69. array('2010-13-01 foobar'),
  70. array('2010-13-01'),
  71. array('2010-04-32'),
  72. array('2010-02-29'),
  73. );
  74. }
  75. public function testMessageIsSet()
  76. {
  77. $constraint = new Date(array(
  78. 'message' => 'myMessage'
  79. ));
  80. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  81. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  82. $this->assertEquals($this->validator->getMessageParameters(), array(
  83. '{{ value }}' => 'foobar',
  84. ));
  85. }
  86. }