TimeValidatorTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\Time;
  4. use Symfony\Component\Validator\Constraints\TimeValidator;
  5. class TimeValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. protected function setUp()
  9. {
  10. $this->validator = new TimeValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new Time()));
  15. }
  16. public function testEmptyStringIsValid()
  17. {
  18. $this->assertTrue($this->validator->isValid('', new Time()));
  19. }
  20. public function testExpectsStringCompatibleType()
  21. {
  22. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  23. $this->validator->isValid(new \stdClass(), new Time());
  24. }
  25. /**
  26. * @dataProvider getValidTimes
  27. */
  28. public function testValidTimes($time)
  29. {
  30. $this->assertTrue($this->validator->isValid($time, new Time()));
  31. }
  32. public function getValidTimes()
  33. {
  34. return array(
  35. array('01:02:03'),
  36. array('00:00:00'),
  37. array('23:59:59'),
  38. );
  39. }
  40. /**
  41. * @dataProvider getInvalidTimes
  42. */
  43. public function testInvalidTimes($time)
  44. {
  45. $this->assertFalse($this->validator->isValid($time, new Time()));
  46. }
  47. public function getInvalidTimes()
  48. {
  49. return array(
  50. array('foobar'),
  51. array('00:00'),
  52. array('24:00:00'),
  53. array('00:60:00'),
  54. array('00:00:60'),
  55. );
  56. }
  57. public function testMessageIsSet()
  58. {
  59. $constraint = new Time(array(
  60. 'message' => 'myMessage'
  61. ));
  62. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  63. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  64. $this->assertEquals($this->validator->getMessageParameters(), array(
  65. '{{ value }}' => 'foobar',
  66. ));
  67. }
  68. }