LanguageValidatorTest.php 2.3 KB

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