TimeValidatorTest.php 2.4 KB

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