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\Intl\Util\IntlTestHelper;
15use Symfony\Component\Validator\Constraints\Currency;
16use Symfony\Component\Validator\Constraints\CurrencyValidator;
17use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18
19class CurrencyValidatorTest extends ConstraintValidatorTestCase
20{
21    private $defaultLocale;
22
23    protected function setUp()
24    {
25        parent::setUp();
26
27        $this->defaultLocale = \Locale::getDefault();
28    }
29
30    protected function tearDown()
31    {
32        parent::tearDown();
33
34        \Locale::setDefault($this->defaultLocale);
35    }
36
37    protected function createValidator()
38    {
39        return new CurrencyValidator();
40    }
41
42    public function testNullIsValid()
43    {
44        $this->validator->validate(null, new Currency());
45
46        $this->assertNoViolation();
47    }
48
49    public function testEmptyStringIsValid()
50    {
51        $this->validator->validate('', new Currency());
52
53        $this->assertNoViolation();
54    }
55
56    public function testExpectsStringCompatibleType()
57    {
58        $this->expectException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
59        $this->validator->validate(new \stdClass(), new Currency());
60    }
61
62    /**
63     * @dataProvider getValidCurrencies
64     */
65    public function testValidCurrencies($currency)
66    {
67        $this->validator->validate($currency, new Currency());
68
69        $this->assertNoViolation();
70    }
71
72    /**
73     * @dataProvider getValidCurrencies
74     **/
75    public function testValidCurrenciesWithCountrySpecificLocale($currency)
76    {
77        IntlTestHelper::requireFullIntl($this, false);
78
79        \Locale::setDefault('en_GB');
80
81        $this->validator->validate($currency, new Currency());
82
83        $this->assertNoViolation();
84    }
85
86    public function getValidCurrencies()
87    {
88        return [
89            ['EUR'],
90            ['USD'],
91            ['SIT'],
92            ['AUD'],
93            ['CAD'],
94        ];
95    }
96
97    /**
98     * @dataProvider getInvalidCurrencies
99     */
100    public function testInvalidCurrencies($currency)
101    {
102        $constraint = new Currency([
103            'message' => 'myMessage',
104        ]);
105
106        $this->validator->validate($currency, $constraint);
107
108        $this->buildViolation('myMessage')
109            ->setParameter('{{ value }}', '"'.$currency.'"')
110            ->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
111            ->assertRaised();
112    }
113
114    public function getInvalidCurrencies()
115    {
116        return [
117            ['EN'],
118            ['foobar'],
119        ];
120    }
121}
122