LocaleValidatorTest.php 2.2 KB

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