CountryValidatorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Intl\Util\IntlTestHelper;
  12. use Symfony\Component\Validator\Constraints\Country;
  13. use Symfony\Component\Validator\Constraints\CountryValidator;
  14. use Symfony\Component\Validator\Validation;
  15. class CountryValidatorTest extends AbstractConstraintValidatorTest
  16. {
  17. protected function getApiVersion()
  18. {
  19. return Validation::API_VERSION_2_5;
  20. }
  21. protected function createValidator()
  22. {
  23. return new CountryValidator();
  24. }
  25. public function testNullIsValid()
  26. {
  27. $this->validator->validate(null, new Country());
  28. $this->assertNoViolation();
  29. }
  30. public function testEmptyStringIsValid()
  31. {
  32. $this->validator->validate('', new Country());
  33. $this->assertNoViolation();
  34. }
  35. /**
  36. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  37. */
  38. public function testExpectsStringCompatibleType()
  39. {
  40. $this->validator->validate(new \stdClass(), new Country());
  41. }
  42. /**
  43. * @dataProvider getValidCountries
  44. */
  45. public function testValidCountries($country)
  46. {
  47. $this->validator->validate($country, new Country());
  48. $this->assertNoViolation();
  49. }
  50. public function getValidCountries()
  51. {
  52. return array(
  53. array('GB'),
  54. array('AT'),
  55. array('MY'),
  56. );
  57. }
  58. /**
  59. * @dataProvider getInvalidCountries
  60. */
  61. public function testInvalidCountries($country)
  62. {
  63. $constraint = new Country(array(
  64. 'message' => 'myMessage',
  65. ));
  66. $this->validator->validate($country, $constraint);
  67. $this->buildViolation('myMessage')
  68. ->setParameter('{{ value }}', '"'.$country.'"')
  69. ->setCode(Country::NO_SUCH_COUNTRY_ERROR)
  70. ->assertRaised();
  71. }
  72. public function getInvalidCountries()
  73. {
  74. return array(
  75. array('foobar'),
  76. array('EN'),
  77. );
  78. }
  79. public function testValidateUsingCountrySpecificLocale()
  80. {
  81. // in order to test with "en_GB"
  82. IntlTestHelper::requireFullIntl($this, false);
  83. \Locale::setDefault('en_GB');
  84. $existingCountry = 'GB';
  85. $this->validator->validate($existingCountry, new Country());
  86. $this->assertNoViolation();
  87. }
  88. }