d9bd4d04872b44a9b62c167e53b21d250900c967
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / LanguageValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use Symfony\Component\Intl\Util\IntlTestHelper;
15 use Symfony\Component\Validator\Constraints\Language;
16 use Symfony\Component\Validator\Constraints\LanguageValidator;
17 use Symfony\Component\Validator\Validation;
18
19 class LanguageValidatorTest extends AbstractConstraintValidatorTest
20 {
21     protected function getApiVersion()
22     {
23         return Validation::API_VERSION_2_5;
24     }
25
26     protected function createValidator()
27     {
28         return new LanguageValidator();
29     }
30
31     public function testNullIsValid()
32     {
33         $this->validator->validate(null, new Language());
34
35         $this->assertNoViolation();
36     }
37
38     public function testEmptyStringIsValid()
39     {
40         $this->validator->validate('', new Language());
41
42         $this->assertNoViolation();
43     }
44
45     /**
46      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
47      */
48     public function testExpectsStringCompatibleType()
49     {
50         $this->validator->validate(new \stdClass(), new Language());
51     }
52
53     /**
54      * @dataProvider getValidLanguages
55      */
56     public function testValidLanguages($language)
57     {
58         $this->validator->validate($language, new Language());
59
60         $this->assertNoViolation();
61     }
62
63     public function getValidLanguages()
64     {
65         return array(
66             array('en'),
67             array('en_US'),
68             array('my'),
69         );
70     }
71
72     /**
73      * @dataProvider getInvalidLanguages
74      */
75     public function testInvalidLanguages($language)
76     {
77         $constraint = new Language(array(
78             'message' => 'myMessage',
79         ));
80
81         $this->validator->validate($language, $constraint);
82
83         $this->buildViolation('myMessage')
84             ->setParameter('{{ value }}', '"'.$language.'"')
85             ->setCode(Language::NO_SUCH_LANGUAGE_ERROR)
86             ->assertRaised();
87     }
88
89     public function getInvalidLanguages()
90     {
91         return array(
92             array('EN'),
93             array('foobar'),
94         );
95     }
96
97     public function testValidateUsingCountrySpecificLocale()
98     {
99         IntlTestHelper::requireFullIntl($this, false);
100
101         \Locale::setDefault('fr_FR');
102         $existingLanguage = 'en';
103
104         $this->validator->validate($existingLanguage, new Language(array(
105             'message' => 'aMessage',
106         )));
107
108         $this->assertNoViolation();
109     }
110 }