LanguageValidatorTest.php 2.3 KB

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