DateValidatorTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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;
  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. public function testNullIsValid()
  21. {
  22. $this->assertTrue($this->validator->isValid(null, new Date()));
  23. }
  24. public function testEmptyStringIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid('', new Date()));
  27. }
  28. public function testExpectsStringCompatibleType()
  29. {
  30. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  31. $this->validator->isValid(new \stdClass(), new Date());
  32. }
  33. /**
  34. * @dataProvider getValidDates
  35. */
  36. public function testValidDates($date)
  37. {
  38. $this->assertTrue($this->validator->isValid($date, new Date()));
  39. }
  40. public function getValidDates()
  41. {
  42. return array(
  43. array('2010-01-01'),
  44. array('1955-12-12'),
  45. array('2030-05-31'),
  46. );
  47. }
  48. /**
  49. * @dataProvider getInvalidDates
  50. */
  51. public function testInvalidDates($date)
  52. {
  53. $this->assertFalse($this->validator->isValid($date, new Date()));
  54. }
  55. public function getInvalidDates()
  56. {
  57. return array(
  58. array('foobar'),
  59. array('2010-13-01'),
  60. array('2010-04-32'),
  61. array('2010-02-29'),
  62. );
  63. }
  64. public function testMessageIsSet()
  65. {
  66. $constraint = new Date(array(
  67. 'message' => 'myMessage'
  68. ));
  69. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  70. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  71. $this->assertEquals($this->validator->getMessageParameters(), array(
  72. '{{ value }}' => 'foobar',
  73. ));
  74. }
  75. }