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
12namespace Symfony\Component\Validator\Tests\Constraints;
13
14use Symfony\Component\Validator\Constraints\Locale;
15use Symfony\Component\Validator\Constraints\LocaleValidator;
16use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17
18class LocaleValidatorTest extends ConstraintValidatorTestCase
19{
20    protected function createValidator()
21    {
22        return new LocaleValidator();
23    }
24
25    public function testNullIsValid()
26    {
27        $this->validator->validate(null, new Locale());
28
29        $this->assertNoViolation();
30    }
31
32    public function testEmptyStringIsValid()
33    {
34        $this->validator->validate('', new Locale());
35
36        $this->assertNoViolation();
37    }
38
39    public function testExpectsStringCompatibleType()
40    {
41        $this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
42        $this->validator->validate(new \stdClass(), new Locale());
43    }
44
45    /**
46     * @dataProvider getValidLocales
47     */
48    public function testValidLocales($locale)
49    {
50        $this->validator->validate($locale, new Locale());
51
52        $this->assertNoViolation();
53    }
54
55    public function getValidLocales()
56    {
57        return [
58            ['en'],
59            ['en_US'],
60            ['pt'],
61            ['pt_PT'],
62            ['zh_Hans'],
63            ['fil_PH'],
64        ];
65    }
66
67    /**
68     * @dataProvider getInvalidLocales
69     */
70    public function testInvalidLocales($locale)
71    {
72        $constraint = new Locale([
73            'message' => 'myMessage',
74        ]);
75
76        $this->validator->validate($locale, $constraint);
77
78        $this->buildViolation('myMessage')
79            ->setParameter('{{ value }}', '"'.$locale.'"')
80            ->setCode(Locale::NO_SUCH_LOCALE_ERROR)
81            ->assertRaised();
82    }
83
84    public function getInvalidLocales()
85    {
86        return [
87            ['EN'],
88            ['foobar'],
89        ];
90    }
91}
92