TimeValidatorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Time;
  12. use Symfony\Component\Validator\Constraints\TimeValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class TimeValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new TimeValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new Time());
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new Time());
  32. $this->assertNoViolation();
  33. }
  34. public function testDateTimeClassIsValid()
  35. {
  36. $this->validator->validate(new \DateTime(), new Time());
  37. $this->assertNoViolation();
  38. }
  39. /**
  40. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  41. */
  42. public function testExpectsStringCompatibleType()
  43. {
  44. $this->validator->validate(new \stdClass(), new Time());
  45. }
  46. /**
  47. * @dataProvider getValidTimes
  48. */
  49. public function testValidTimes($time)
  50. {
  51. $this->validator->validate($time, new Time());
  52. $this->assertNoViolation();
  53. }
  54. public function getValidTimes()
  55. {
  56. return array(
  57. array('01:02:03'),
  58. array('00:00:00'),
  59. array('23:59:59'),
  60. );
  61. }
  62. /**
  63. * @dataProvider getInvalidTimes
  64. */
  65. public function testInvalidTimes($time, $code)
  66. {
  67. $constraint = new Time(array(
  68. 'message' => 'myMessage',
  69. ));
  70. $this->validator->validate($time, $constraint);
  71. $this->buildViolation('myMessage')
  72. ->setParameter('{{ value }}', '"'.$time.'"')
  73. ->setCode($code)
  74. ->assertRaised();
  75. }
  76. public function getInvalidTimes()
  77. {
  78. return array(
  79. array('foobar', Time::INVALID_FORMAT_ERROR),
  80. array('foobar 12:34:56', Time::INVALID_FORMAT_ERROR),
  81. array('12:34:56 foobar', Time::INVALID_FORMAT_ERROR),
  82. array('00:00', Time::INVALID_FORMAT_ERROR),
  83. array('24:00:00', Time::INVALID_TIME_ERROR),
  84. array('00:60:00', Time::INVALID_TIME_ERROR),
  85. array('00:00:60', Time::INVALID_TIME_ERROR),
  86. );
  87. }
  88. }