LocaleValidatorTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Locale;
  12. use Symfony\Component\Validator\Constraints\LocaleValidator;
  13. class LocaleValidatorTest extends LocalizedTestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $this->validator = new LocaleValidator();
  20. }
  21. protected function tearDown()
  22. {
  23. $this->validator = null;
  24. }
  25. public function testNullIsValid()
  26. {
  27. $this->assertTrue($this->validator->isValid(null, new Locale()));
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->assertTrue($this->validator->isValid('', new Locale()));
  32. }
  33. public function testExpectsStringCompatibleType()
  34. {
  35. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  36. $this->validator->isValid(new \stdClass(), new Locale());
  37. }
  38. /**
  39. * @dataProvider getValidLocales
  40. */
  41. public function testValidLocales($date)
  42. {
  43. $this->assertTrue($this->validator->isValid($date, new Locale()));
  44. }
  45. public function getValidLocales()
  46. {
  47. return array(
  48. array('en'),
  49. array('en_US'),
  50. array('my'),
  51. array('zh_Hans'),
  52. );
  53. }
  54. /**
  55. * @dataProvider getInvalidLocales
  56. */
  57. public function testInvalidLocales($date)
  58. {
  59. $this->assertFalse($this->validator->isValid($date, new Locale()));
  60. }
  61. public function getInvalidLocales()
  62. {
  63. return array(
  64. array('EN'),
  65. array('foobar'),
  66. );
  67. }
  68. public function testMessageIsSet()
  69. {
  70. $constraint = new Locale(array(
  71. 'message' => 'myMessage'
  72. ));
  73. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  74. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  75. $this->assertEquals($this->validator->getMessageParameters(), array(
  76. '{{ value }}' => 'foobar',
  77. ));
  78. }
  79. }