TimeValidatorTest.php 2.2 KB

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