|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Validator\Constraints\Timezone; |
| 15 | +use Symfony\Component\Validator\Constraints\TimezoneValidator; |
| 16 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Javier Spagnoletti <[email protected]> |
| 20 | + */ |
| 21 | +class TimezoneValidatorTest extends ConstraintValidatorTestCase |
| 22 | +{ |
| 23 | + protected function createValidator() |
| 24 | + { |
| 25 | + return new TimezoneValidator(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testNullIsValid() |
| 29 | + { |
| 30 | + $this->validator->validate(null, new Timezone()); |
| 31 | + |
| 32 | + $this->assertNoViolation(); |
| 33 | + } |
| 34 | + |
| 35 | + public function testEmptyStringIsValid() |
| 36 | + { |
| 37 | + $this->validator->validate('', new Timezone()); |
| 38 | + |
| 39 | + $this->assertNoViolation(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException |
| 44 | + */ |
| 45 | + public function testExpectsStringCompatibleType() |
| 46 | + { |
| 47 | + $this->validator->validate(new \stdClass(), new Timezone()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @dataProvider getValidTimezones |
| 52 | + */ |
| 53 | + public function testValidTimezones($timezone) |
| 54 | + { |
| 55 | + $this->validator->validate($timezone, new Timezone()); |
| 56 | + |
| 57 | + $this->assertNoViolation(); |
| 58 | + } |
| 59 | + |
| 60 | + public function getValidTimezones() |
| 61 | + { |
| 62 | + return array( |
| 63 | + array('America/Argentina/Buenos_Aires'), |
| 64 | + array('America/Barbados'), |
| 65 | + array('Antarctica/Syowa'), |
| 66 | + array('Africa/Douala'), |
| 67 | + array('Atlantic/Canary'), |
| 68 | + array('Asia/Gaza'), |
| 69 | + array('Europe/Copenhagen'), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @dataProvider getValidGroupedTimezones |
| 75 | + */ |
| 76 | + public function testValidGroupedTimezones($timezone, $what) |
| 77 | + { |
| 78 | + $constraint = new Timezone(array( |
| 79 | + 'value' => $what, |
| 80 | + )); |
| 81 | + |
| 82 | + $this->validator->validate($timezone, $constraint); |
| 83 | + |
| 84 | + $this->assertNoViolation(); |
| 85 | + } |
| 86 | + |
| 87 | + public function getValidGroupedTimezones() |
| 88 | + { |
| 89 | + return array( |
| 90 | + array('America/Argentina/Cordoba', \DateTimeZone::AMERICA), |
| 91 | + array('America/Barbados', \DateTimeZone::AMERICA), |
| 92 | + array('Africa/Cairo', \DateTimeZone::AFRICA), |
| 93 | + array('Atlantic/Cape_Verde', \DateTimeZone::ATLANTIC), |
| 94 | + array('Europe/Bratislava', \DateTimeZone::EUROPE), |
| 95 | + array('Indian/Christmas', \DateTimeZone::INDIAN), |
| 96 | + array('Pacific/Kiritimati', \DateTimeZone::ALL), |
| 97 | + array('Pacific/Kiritimati', \DateTimeZone::ALL_WITH_BC), |
| 98 | + array('Pacific/Kiritimati', \DateTimeZone::PACIFIC), |
| 99 | + array('Arctic/Longyearbyen', \DateTimeZone::ARCTIC), |
| 100 | + array('Asia/Beirut', \DateTimeZone::ASIA), |
| 101 | + array('Atlantic/Bermuda', \DateTimeZone::ASIA | \DateTimeZone::ATLANTIC), |
| 102 | + array('Atlantic/Azores', \DateTimeZone::ATLANTIC | \DateTimeZone::ASIA), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @dataProvider getInvalidTimezones |
| 108 | + */ |
| 109 | + public function testInvalidTimezones($timezone) |
| 110 | + { |
| 111 | + $constraint = new Timezone(array( |
| 112 | + 'message' => 'myMessage', |
| 113 | + )); |
| 114 | + |
| 115 | + $this->validator->validate($timezone, $constraint); |
| 116 | + |
| 117 | + $this->buildViolation('myMessage') |
| 118 | + ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') |
| 119 | + ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) |
| 120 | + ->assertRaised(); |
| 121 | + } |
| 122 | + |
| 123 | + public function getInvalidTimezones() |
| 124 | + { |
| 125 | + return array( |
| 126 | + array('Buenos_Aires/Argentina/America'), |
| 127 | + array('Mayotte/Indian'), |
| 128 | + array('foobar'), |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @dataProvider getInvalidGroupedTimezones |
| 134 | + */ |
| 135 | + public function testInvalidGroupedTimezones($timezone, $what) |
| 136 | + { |
| 137 | + $constraint = new Timezone(array( |
| 138 | + 'value' => $what, |
| 139 | + 'message' => 'myMessage', |
| 140 | + )); |
| 141 | + |
| 142 | + $this->validator->validate($timezone, $constraint); |
| 143 | + |
| 144 | + $this->buildViolation('myMessage') |
| 145 | + ->setParameter('{{ timezone_group }}', '"'.$timezone.'"') |
| 146 | + ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) |
| 147 | + ->assertRaised(); |
| 148 | + } |
| 149 | + |
| 150 | + public function getInvalidGroupedTimezones() |
| 151 | + { |
| 152 | + return array( |
| 153 | + array('Antarctica/McMurdo', \DateTimeZone::AMERICA), |
| 154 | + array('America/Barbados', \DateTimeZone::ANTARCTICA), |
| 155 | + array('Europe/Kiev', \DateTimeZone::ARCTIC), |
| 156 | + array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN), |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
0 commit comments