LanguageValidatorTest.php 2.2 KB

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