LocaleValidatorTest.php 2.2 KB

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