LocaleValidatorTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Locale;
  12. use Symfony\Component\Validator\Constraints\LocaleValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class LocaleValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new LocaleValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new Locale());
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new Locale());
  32. $this->assertNoViolation();
  33. }
  34. /**
  35. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  36. */
  37. public function testExpectsStringCompatibleType()
  38. {
  39. $this->validator->validate(new \stdClass(), new Locale());
  40. }
  41. /**
  42. * @dataProvider getValidLocales
  43. */
  44. public function testValidLocales($locale)
  45. {
  46. $this->validator->validate($locale, new Locale());
  47. $this->assertNoViolation();
  48. }
  49. public function getValidLocales()
  50. {
  51. return array(
  52. array('en'),
  53. array('en_US'),
  54. array('pt'),
  55. array('pt_PT'),
  56. array('zh_Hans'),
  57. array('fil_PH'),
  58. );
  59. }
  60. /**
  61. * @dataProvider getInvalidLocales
  62. */
  63. public function testInvalidLocales($locale)
  64. {
  65. $constraint = new Locale(array(
  66. 'message' => 'myMessage',
  67. ));
  68. $this->validator->validate($locale, $constraint);
  69. $this->buildViolation('myMessage')
  70. ->setParameter('{{ value }}', '"'.$locale.'"')
  71. ->setCode(Locale::NO_SUCH_LOCALE_ERROR)
  72. ->assertRaised();
  73. }
  74. public function getInvalidLocales()
  75. {
  76. return array(
  77. array('EN'),
  78. array('foobar'),
  79. );
  80. }
  81. }