| Total Complexity | 41 |
| Total Lines | 668 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DateTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DateTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class DateTest extends TestCase |
||
| 12 | { |
||
| 13 | protected function setUp() |
||
| 14 | { |
||
| 15 | parent::setUp(); |
||
| 16 | |||
| 17 | IntlTestHelper::setIntlStatus($this); |
||
|
|
|||
| 18 | |||
| 19 | $this->mockApplication([ |
||
| 20 | 'timeZone' => 'UTC', |
||
| 21 | 'locale' => 'ru-RU', |
||
| 22 | ]); |
||
| 23 | } |
||
| 24 | |||
| 25 | protected function tearDown() |
||
| 26 | { |
||
| 27 | parent::tearDown(); |
||
| 28 | IntlTestHelper::resetIntlStatus(); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function testEnsureMessageIsSet() |
||
| 32 | { |
||
| 33 | $val = new Date(); |
||
| 34 | $this->assertTrue($val->message !== null && strlen($val->message) > 1); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @dataProvider provideTimezones |
||
| 39 | * @param string $timezone |
||
| 40 | */ |
||
| 41 | public function testIntlvalidate($timezone) |
||
| 42 | { |
||
| 43 | date_default_timezone_set($timezone); |
||
| 44 | $this->testvalidate($timezone); |
||
| 45 | |||
| 46 | $this->mockApplication(['locale' => 'en-GB'], null, [ |
||
| 47 | 'formatter' => [ |
||
| 48 | '__class' => \yii\i18n\Formatter::class, |
||
| 49 | 'dateFormat' => 'short', |
||
| 50 | ], |
||
| 51 | ]); |
||
| 52 | $f = $this->container->get('formatter'); |
||
| 53 | $val = new Date(); |
||
| 54 | $this->assertTrue($val->validate('31/5/2017')); |
||
| 55 | $this->assertFalse($val->validate('5/31/2017')); |
||
| 56 | $val = new Date(['format' => 'short', 'locale' => 'en-GB']); |
||
| 57 | $this->assertTrue($val->validate('31/5/2017')); |
||
| 58 | $this->assertFalse($val->validate('5/31/2017')); |
||
| 59 | |||
| 60 | $this->mockApplication(['locale' => 'de-DE'], null, [ |
||
| 61 | 'formatter' => [ |
||
| 62 | '__class' => \yii\i18n\Formatter::class, |
||
| 63 | 'dateFormat' => 'short', |
||
| 64 | ], |
||
| 65 | ]); |
||
| 66 | $val = new Date(); |
||
| 67 | $this->assertTrue($val->validate('31.5.2017')); |
||
| 68 | $this->assertFalse($val->validate('5.31.2017')); |
||
| 69 | $val = new Date(['format' => 'short', 'locale' => 'de-DE']); |
||
| 70 | $this->assertTrue($val->validate('31.5.2017')); |
||
| 71 | $this->assertFalse($val->validate('5.31.2017')); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @dataProvider provideTimezones |
||
| 76 | * @param string $timezone |
||
| 77 | */ |
||
| 78 | public function testvalidate($timezone) |
||
| 79 | { |
||
| 80 | date_default_timezone_set($timezone); |
||
| 81 | |||
| 82 | // test PHP format |
||
| 83 | $val = new Date(['format' => 'php:Y-m-d']); |
||
| 84 | $this->assertFalse($val->validate('3232-32-32')); |
||
| 85 | $this->assertTrue($val->validate('2013-09-13')); |
||
| 86 | $this->assertFalse($val->validate('31.7.2013')); |
||
| 87 | $this->assertFalse($val->validate('31-7-2013')); |
||
| 88 | $this->assertFalse($val->validate('20121212')); |
||
| 89 | $this->assertFalse($val->validate('asdasdfasfd')); |
||
| 90 | $this->assertFalse($val->validate('2012-12-12foo')); |
||
| 91 | $this->assertFalse($val->validate('')); |
||
| 92 | $this->assertFalse($val->validate(time())); |
||
| 93 | $val->format = 'php:U'; |
||
| 94 | $this->assertTrue($val->validate(time())); |
||
| 95 | $val->format = 'php:d.m.Y'; |
||
| 96 | $this->assertTrue($val->validate('31.7.2013')); |
||
| 97 | $val->format = 'php:Y-m-!d H:i:s'; |
||
| 98 | $this->assertTrue($val->validate('2009-02-15 15:16:17')); |
||
| 99 | |||
| 100 | // test ICU format |
||
| 101 | $val = new Date(['format' => 'yyyy-MM-dd']); |
||
| 102 | $this->assertFalse($val->validate('3232-32-32')); |
||
| 103 | $this->assertTrue($val->validate('2013-09-13')); |
||
| 104 | $this->assertFalse($val->validate('31.7.2013')); |
||
| 105 | $this->assertFalse($val->validate('31-7-2013')); |
||
| 106 | $this->assertFalse($val->validate('20121212')); |
||
| 107 | $this->assertFalse($val->validate('asdasdfasfd')); |
||
| 108 | $this->assertFalse($val->validate('2012-12-12foo')); |
||
| 109 | $this->assertFalse($val->validate('')); |
||
| 110 | $this->assertFalse($val->validate(time())); |
||
| 111 | $val->format = 'dd.MM.yyyy'; |
||
| 112 | $this->assertTrue($val->validate('31.7.2013')); |
||
| 113 | $val->format = 'yyyy-MM-dd HH:mm:ss'; |
||
| 114 | $this->assertTrue($val->validate('2009-02-15 15:16:17')); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @dataProvider provideTimezones |
||
| 119 | * @param string $timezone |
||
| 120 | */ |
||
| 121 | public function testIntlValidateAttributePHPFormat($timezone) |
||
| 122 | { |
||
| 123 | $this->testValidateAttributePHPFormat($timezone); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @dataProvider provideTimezones |
||
| 128 | * @param string $timezone |
||
| 129 | */ |
||
| 130 | public function testValidateAttributePHPFormat($timezone) |
||
| 131 | { |
||
| 132 | date_default_timezone_set($timezone); |
||
| 133 | |||
| 134 | // error-array-add |
||
| 135 | $val = new Date(['format' => 'php:Y-m-d']); |
||
| 136 | $model = new FakedValidationModel(); |
||
| 137 | $model->attr_date = '2013-09-13'; |
||
| 138 | $val->validateAttribute($model, 'attr_date'); |
||
| 139 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 140 | $model = new FakedValidationModel(); |
||
| 141 | $model->attr_date = '1375293913'; |
||
| 142 | $val->validateAttribute($model, 'attr_date'); |
||
| 143 | $this->assertTrue($model->hasErrors('attr_date')); |
||
| 144 | //// timestamp attribute |
||
| 145 | $val = new Date(['format' => 'php:Y-m-d', 'timestampAttribute' => 'attr_timestamp']); |
||
| 146 | $model = new FakedValidationModel(); |
||
| 147 | $model->attr_date = '2013-09-13'; |
||
| 148 | $model->attr_timestamp = true; |
||
| 149 | $val->validateAttribute($model, 'attr_date'); |
||
| 150 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 151 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 152 | $this->assertEquals( |
||
| 153 | 1379030400, // 2013-09-13 00:00:00 |
||
| 154 | $model->attr_timestamp |
||
| 155 | ); |
||
| 156 | // array value |
||
| 157 | $val = new Date(['format' => 'php:Y-m-d']); |
||
| 158 | $model = FakedValidationModel::createWithAttributes(['attr_date' => ['2013-09-13']]); |
||
| 159 | $val->validateAttribute($model, 'attr_date'); |
||
| 160 | $this->assertTrue($model->hasErrors('attr_date')); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @dataProvider provideTimezones |
||
| 165 | * @param string $timezone |
||
| 166 | */ |
||
| 167 | public function testIntlValidateAttributeICUFormat($timezone) |
||
| 168 | { |
||
| 169 | $this->testValidateAttributeICUFormat($timezone); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @dataProvider provideTimezones |
||
| 174 | * @param string $timezone |
||
| 175 | */ |
||
| 176 | public function testValidateAttributeICUFormat($timezone) |
||
| 177 | { |
||
| 178 | date_default_timezone_set($timezone); |
||
| 179 | |||
| 180 | // error-array-add |
||
| 181 | $val = new Date(['format' => 'yyyy-MM-dd']); |
||
| 182 | $model = new FakedValidationModel(); |
||
| 183 | $model->attr_date = '2013-09-13'; |
||
| 184 | $val->validateAttribute($model, 'attr_date'); |
||
| 185 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 186 | $model = new FakedValidationModel(); |
||
| 187 | $model->attr_date = '1375293913'; |
||
| 188 | $val->validateAttribute($model, 'attr_date'); |
||
| 189 | $this->assertTrue($model->hasErrors('attr_date')); |
||
| 190 | //// timestamp attribute |
||
| 191 | $val = new Date(['format' => 'yyyy-MM-dd', 'timestampAttribute' => 'attr_timestamp']); |
||
| 192 | $model = new FakedValidationModel(); |
||
| 193 | $model->attr_date = '2013-09-13'; |
||
| 194 | $model->attr_timestamp = true; |
||
| 195 | $val->validateAttribute($model, 'attr_date'); |
||
| 196 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 197 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 198 | $this->assertSame( |
||
| 199 | 1379030400, // 2013-09-13 00:00:00 |
||
| 200 | $model->attr_timestamp |
||
| 201 | ); |
||
| 202 | // array value |
||
| 203 | $val = new Date(['format' => 'yyyy-MM-dd']); |
||
| 204 | $model = FakedValidationModel::createWithAttributes(['attr_date' => ['2013-09-13']]); |
||
| 205 | $val->validateAttribute($model, 'attr_date'); |
||
| 206 | $this->assertTrue($model->hasErrors('attr_date')); |
||
| 207 | // invalid format |
||
| 208 | $val = new Date(['format' => 'yyyy-MM-dd']); |
||
| 209 | $model = FakedValidationModel::createWithAttributes(['attr_date' => '2012-12-12foo']); |
||
| 210 | $val->validateAttribute($model, 'attr_date'); |
||
| 211 | $this->assertTrue($model->hasErrors('attr_date')); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function testIntlMultibyteString() |
||
| 215 | { |
||
| 216 | $val = new Date(['format' => 'dd MMM yyyy', 'locale' => 'de_DE']); |
||
| 217 | $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 Mai 2014']); |
||
| 218 | $val->validateAttribute($model, 'attr_date'); |
||
| 219 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 220 | |||
| 221 | $val = new Date(['format' => 'dd MMM yyyy', 'locale' => 'ru_RU']); |
||
| 222 | $model = FakedValidationModel::createWithAttributes(['attr_date' => '12 мая 2014']); |
||
| 223 | $val->validateAttribute($model, 'attr_date'); |
||
| 224 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function provideTimezones() |
||
| 228 | { |
||
| 229 | return [ |
||
| 230 | ['UTC'], |
||
| 231 | ['Europe/Berlin'], |
||
| 232 | ['America/Jamaica'], |
||
| 233 | ]; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function timestampFormatProvider() |
||
| 237 | { |
||
| 238 | $return = []; |
||
| 239 | foreach ($this->provideTimezones() as $appTz) { |
||
| 240 | foreach ($this->provideTimezones() as $tz) { |
||
| 241 | $return[] = ['yyyy-MM-dd', '2013-09-13', '2013-09-13', $tz[0], $appTz[0]]; |
||
| 242 | // regardless of timezone, a simple date input should always result in 00:00:00 time |
||
| 243 | $return[] = ['yyyy-MM-dd HH:mm:ss', '2013-09-13', '2013-09-13 00:00:00', $tz[0], $appTz[0]]; |
||
| 244 | $return[] = ['php:Y-m-d', '2013-09-13', '2013-09-13', $tz[0], $appTz[0]]; |
||
| 245 | $return[] = ['php:Y-m-d H:i:s', '2013-09-13', '2013-09-13 00:00:00', $tz[0], $appTz[0]]; |
||
| 246 | $return[] = ['php:U', '2013-09-13', '1379030400', $tz[0], $appTz[0]]; |
||
| 247 | $return[] = [null, '2013-09-13', 1379030400, $tz[0], $appTz[0]]; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | return $return; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @dataProvider timestampFormatProvider |
||
| 256 | * @param string|null $format |
||
| 257 | * @param string $date |
||
| 258 | * @param string|int $expectedDate |
||
| 259 | * @param string $timezone |
||
| 260 | * @param string $appTimezone |
||
| 261 | */ |
||
| 262 | public function testIntlTimestampAttributeFormat($format, $date, $expectedDate, $timezone, $appTimezone) |
||
| 263 | { |
||
| 264 | $this->testTimestampAttributeFormat($format, $date, $expectedDate, $timezone, $appTimezone); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @dataProvider timestampFormatProvider |
||
| 269 | * @param string|null $format |
||
| 270 | * @param string $date |
||
| 271 | * @param string|int $expectedDate |
||
| 272 | * @param string $timezone |
||
| 273 | * @param string $appTimezone |
||
| 274 | */ |
||
| 275 | public function testTimestampAttributeFormat($format, $date, $expectedDate, $timezone, $appTimezone) |
||
| 276 | { |
||
| 277 | date_default_timezone_set($timezone); |
||
| 278 | |||
| 279 | $val = new Date(['format' => 'yyyy-MM-dd', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => $format, 'timeZone' => $appTimezone]); |
||
| 280 | $model = new FakedValidationModel(); |
||
| 281 | $model->attr_date = $date; |
||
| 282 | $model->attr_timestamp = true; |
||
| 283 | $val->validateAttribute($model, 'attr_date'); |
||
| 284 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 285 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 286 | $this->assertSame($expectedDate, $model->attr_timestamp); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @dataProvider provideTimezones |
||
| 291 | * @param string $timezone |
||
| 292 | */ |
||
| 293 | public function testIntlValidationWithTime($timezone) |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @dataProvider provideTimezones |
||
| 344 | * @param string $timezone |
||
| 345 | */ |
||
| 346 | public function testValidationWithTime($timezone) |
||
| 347 | { |
||
| 348 | date_default_timezone_set($timezone); |
||
| 349 | |||
| 350 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timeZone' => 'UTC']); |
||
| 351 | $model = new FakedValidationModel(); |
||
| 352 | $model->attr_date = '2013-09-13 14:23:15'; |
||
| 353 | $model->attr_timestamp = true; |
||
| 354 | $val->validateAttribute($model, 'attr_date'); |
||
| 355 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 356 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 357 | $this->assertSame(1379082195, $model->attr_timestamp); |
||
| 358 | |||
| 359 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timeZone' => 'Europe/Berlin']); |
||
| 360 | $model = new FakedValidationModel(); |
||
| 361 | $model->attr_date = '2013-09-13 16:23:15'; |
||
| 362 | $model->attr_timestamp = true; |
||
| 363 | $val->validateAttribute($model, 'attr_date'); |
||
| 364 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 365 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 366 | $this->assertSame(1379082195, $model->attr_timestamp); |
||
| 367 | |||
| 368 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timeZone' => 'UTC']); |
||
| 369 | $model = new FakedValidationModel(); |
||
| 370 | $model->attr_date = '2013-09-13 14:23:15'; |
||
| 371 | $model->attr_timestamp = true; |
||
| 372 | $val->validateAttribute($model, 'attr_date'); |
||
| 373 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 374 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 375 | $this->assertSame('2013-09-13 14:23:15', $model->attr_timestamp); |
||
| 376 | |||
| 377 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timeZone' => 'Europe/Berlin']); |
||
| 378 | $model = new FakedValidationModel(); |
||
| 379 | $model->attr_date = '2013-09-13 16:23:15'; |
||
| 380 | $model->attr_timestamp = true; |
||
| 381 | $val->validateAttribute($model, 'attr_date'); |
||
| 382 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 383 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 384 | $this->assertSame('2013-09-13 14:23:15', $model->attr_timestamp); |
||
| 385 | |||
| 386 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'php:Y-m-d H:i:s', 'timeZone' => 'UTC']); |
||
| 387 | $model = new FakedValidationModel(); |
||
| 388 | $model->attr_date = '2013-09-13 14:23:15'; |
||
| 389 | $model->attr_timestamp = true; |
||
| 390 | $val->validateAttribute($model, 'attr_date'); |
||
| 391 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 392 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 393 | $this->assertSame('2013-09-13 14:23:15', $model->attr_timestamp); |
||
| 394 | |||
| 395 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'php:Y-m-d H:i:s', 'timeZone' => 'Europe/Berlin']); |
||
| 396 | $model = new FakedValidationModel(); |
||
| 397 | $model->attr_date = '2013-09-13 16:23:15'; |
||
| 398 | $model->attr_timestamp = true; |
||
| 399 | $val->validateAttribute($model, 'attr_date'); |
||
| 400 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 401 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 402 | $this->assertSame('2013-09-13 14:23:15', $model->attr_timestamp); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @dataProvider provideTimezones |
||
| 407 | * @param string $timezone |
||
| 408 | */ |
||
| 409 | public function testIntlValidationWithTimeAndOutputTimeZone($timezone) |
||
| 410 | { |
||
| 411 | $this->testValidationWithTime($timezone); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @dataProvider provideTimezones |
||
| 416 | * @param string $timezone |
||
| 417 | */ |
||
| 418 | public function testValidationWithTimeAndOutputTimeZone($timezone) |
||
| 419 | { |
||
| 420 | date_default_timezone_set($timezone); |
||
| 421 | |||
| 422 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'UTC']); |
||
| 423 | $model = new FakedValidationModel(); |
||
| 424 | $model->attr_date = '2013-09-13 14:23:15'; |
||
| 425 | $model->attr_timestamp = true; |
||
| 426 | $val->validateAttribute($model, 'attr_date'); |
||
| 427 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 428 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 429 | $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); |
||
| 430 | $val = new Date(['format' => 'php:Y-m-d H:i:s', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'UTC']); |
||
| 431 | $model = new FakedValidationModel(); |
||
| 432 | $model->attr_date = '2013-09-13 14:23:15'; |
||
| 433 | $model->attr_timestamp = true; |
||
| 434 | $val->validateAttribute($model, 'attr_date'); |
||
| 435 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 436 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 437 | $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); |
||
| 438 | |||
| 439 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'Europe/Berlin']); |
||
| 440 | $model = new FakedValidationModel(); |
||
| 441 | $model->attr_date = '2013-09-13 16:23:15'; |
||
| 442 | $model->attr_timestamp = true; |
||
| 443 | $val->validateAttribute($model, 'attr_date'); |
||
| 444 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 445 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 446 | $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); |
||
| 447 | $val = new Date(['format' => 'php:Y-m-d H:i:s', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'Europe/Berlin', 'timeZone' => 'Europe/Berlin']); |
||
| 448 | $model = new FakedValidationModel(); |
||
| 449 | $model->attr_date = '2013-09-13 16:23:15'; |
||
| 450 | $model->attr_timestamp = true; |
||
| 451 | $val->validateAttribute($model, 'attr_date'); |
||
| 452 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 453 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 454 | $this->assertSame('2013-09-13 16:23:15', $model->attr_timestamp); |
||
| 455 | |||
| 456 | $val = new Date(['format' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'America/New_York', 'timeZone' => 'Europe/Berlin']); |
||
| 457 | $model = new FakedValidationModel(); |
||
| 458 | $model->attr_date = '2013-09-13 16:23:15'; |
||
| 459 | $model->attr_timestamp = true; |
||
| 460 | $val->validateAttribute($model, 'attr_date'); |
||
| 461 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 462 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 463 | $this->assertSame('2013-09-13 10:23:15', $model->attr_timestamp); |
||
| 464 | $val = new Date(['format' => 'php:Y-m-d H:i:s', 'timestampAttribute' => 'attr_timestamp', 'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss', 'timestampAttributeTimeZone' => 'America/New_York', 'timeZone' => 'Europe/Berlin']); |
||
| 465 | $model = new FakedValidationModel(); |
||
| 466 | $model->attr_date = '2013-09-13 16:23:15'; |
||
| 467 | $model->attr_timestamp = true; |
||
| 468 | $val->validateAttribute($model, 'attr_date'); |
||
| 469 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 470 | $this->assertFalse($model->hasErrors('attr_timestamp')); |
||
| 471 | $this->assertSame('2013-09-13 10:23:15', $model->attr_timestamp); |
||
| 472 | } |
||
| 473 | |||
| 474 | public function testIntlValidateRange() |
||
| 475 | { |
||
| 476 | $this->testvalidateRange(); |
||
| 477 | } |
||
| 478 | |||
| 479 | public function testvalidateRange() |
||
| 480 | { |
||
| 481 | if (PHP_INT_SIZE == 8) { // this passes only on 64bit systems |
||
| 482 | // intl parser allows 14 for yyyy pattern, see the following for more details: |
||
| 483 | // https://github.com/yiisoft/yii2/blob/a003a8fb487dfa60c0f88ecfacf18a7407ced18b/framework/validators/DateValidator.php#L51-L57 |
||
| 484 | $date = '14-09-13'; |
||
| 485 | $val = new Date(['format' => 'yyyy-MM-dd']); |
||
| 486 | $this->assertTrue($val->validate($date), "$date is valid"); |
||
| 487 | |||
| 488 | $min = '1900-01-01'; |
||
| 489 | $beforeMin = '1899-12-31'; |
||
| 490 | } else { |
||
| 491 | $min = '1920-01-01'; |
||
| 492 | $beforeMin = '1919-12-31'; |
||
| 493 | } |
||
| 494 | |||
| 495 | $val = new Date(['format' => 'yyyy-MM-dd', 'min' => $min]); |
||
| 496 | $date = '1958-01-12'; |
||
| 497 | $this->assertTrue($val->validate($date), "$date is valid"); |
||
| 498 | |||
| 499 | $val = new Date(['format' => 'yyyy-MM-dd', 'max' => '2000-01-01']); |
||
| 500 | $date = '2014-09-13'; |
||
| 501 | $this->assertFalse($val->validate($date), "$date is too big"); |
||
| 502 | $date = '1958-01-12'; |
||
| 503 | $this->assertTrue($val->validate($date), "$date is valid"); |
||
| 504 | |||
| 505 | $val = new Date(['format' => 'yyyy-MM-dd', 'min' => $min, 'max' => '2000-01-01']); |
||
| 506 | $this->assertTrue($val->validate('1999-12-31'), 'max -1 day is valid'); |
||
| 507 | $this->assertTrue($val->validate('2000-01-01'), 'max is inside range'); |
||
| 508 | $this->assertTrue($val->validate($min), 'min is inside range'); |
||
| 509 | $this->assertFalse($val->validate($beforeMin), 'min -1 day is invalid'); |
||
| 510 | $this->assertFalse($val->validate('2000-01-02'), 'max +1 day is invalid'); |
||
| 511 | } |
||
| 512 | |||
| 513 | private function validateModelAttribute($validator, $date, $expected, $message = '') |
||
| 514 | { |
||
| 515 | $model = new FakedValidationModel(); |
||
| 516 | $model->attr_date = $date; |
||
| 517 | $validator->validateAttribute($model, 'attr_date'); |
||
| 518 | if (!$expected) { |
||
| 519 | $this->assertTrue($model->hasErrors('attr_date'), $message); |
||
| 520 | } else { |
||
| 521 | $this->assertFalse($model->hasErrors('attr_date'), $message); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | public function testIntlValidateAttributeRange() |
||
| 528 | } |
||
| 529 | |||
| 530 | public function testValidateAttributeRange() |
||
| 562 | } |
||
| 563 | |||
| 564 | public function testIntlvalidateRangeOld() |
||
| 565 | { |
||
| 566 | if ($this->checkOldIcuBug()) { |
||
| 567 | $this->markTestSkipped('ICU is too old.'); |
||
| 568 | } |
||
| 569 | $date = '14-09-13'; |
||
| 570 | $val = new Date(['format' => 'yyyy-MM-dd', 'min' => '1920-01-01']); |
||
| 571 | $this->assertFalse($val->validate($date), "$date is too small"); |
||
| 572 | } |
||
| 573 | |||
| 574 | public function testIntlValidateAttributeRangeOld() |
||
| 575 | { |
||
| 576 | if ($this->checkOldIcuBug()) { |
||
| 577 | $this->markTestSkipped('ICU is too old.'); |
||
| 578 | } |
||
| 579 | $date = '14-09-13'; |
||
| 580 | $val = new Date(['format' => 'yyyy-MM-dd', 'min' => '1920-01-01']); |
||
| 581 | $this->validateModelAttribute($val, $date, false, "$date is too small"); |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Returns true if the version of ICU is old and has a bug that makes it |
||
| 586 | * impossible to parse two digit years properly. |
||
| 587 | * @see http://bugs.icu-project.org/trac/ticket/9836 |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | private function checkOldIcuBug() |
||
| 591 | { |
||
| 592 | $date = '14'; |
||
| 593 | $formatter = new IntlDateFormatter('en-US', IntlDateFormatter::NONE, IntlDateFormatter::NONE, null, null, 'yyyy'); |
||
| 594 | $parsePos = 0; |
||
| 595 | $parsedDate = @$formatter->parse($date, $parsePos); |
||
| 596 | |||
| 597 | if (is_int($parsedDate) && $parsedDate > 0) { |
||
| 598 | return true; |
||
| 599 | } |
||
| 600 | |||
| 601 | return false; |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * @depends testValidateAttributePHPFormat |
||
| 606 | */ |
||
| 607 | public function testTimestampAttributeSkipValidation() |
||
| 608 | { |
||
| 609 | // timestamp as integer |
||
| 610 | $val = new Date(['format' => 'php:Y/m/d', 'timestampAttribute' => 'attr_date']); |
||
| 611 | $model = new FakedValidationModel(); |
||
| 612 | $model->attr_date = 1379030400; |
||
| 613 | $val->validateAttribute($model, 'attr_date'); |
||
| 614 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 615 | |||
| 616 | $val = new Date(['format' => 'php:Y/m/d', 'timestampAttribute' => 'attr_date']); |
||
| 617 | $model = new FakedValidationModel(); |
||
| 618 | $model->attr_date = 'invalid'; |
||
| 619 | $val->validateAttribute($model, 'attr_date'); |
||
| 620 | $this->assertTrue($model->hasErrors('attr_date')); |
||
| 621 | |||
| 622 | // timestamp as formatted date |
||
| 623 | $val = new Date(['format' => 'php:Y/m/d', 'timestampAttribute' => 'attr_date', 'timestampAttributeFormat' => 'php:Y-m-d']); |
||
| 624 | $model = new FakedValidationModel(); |
||
| 625 | $model->attr_date = '2013-09-13'; |
||
| 626 | $val->validateAttribute($model, 'attr_date'); |
||
| 627 | $this->assertFalse($model->hasErrors('attr_date')); |
||
| 628 | |||
| 629 | $val = new Date(['format' => 'php:Y/m/d', 'timestampAttribute' => 'attr_date', 'timestampAttributeFormat' => 'php:Y-m-d']); |
||
| 630 | $model = new FakedValidationModel(); |
||
| 631 | $model->attr_date = '2013-09-2013'; |
||
| 632 | $val->validateAttribute($model, 'attr_date'); |
||
| 633 | $this->assertTrue($model->hasErrors('attr_date')); |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * @depends testValidateAttributePHPFormat |
||
| 638 | */ |
||
| 639 | public function testTimestampAttributeOnEmpty() |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Tests that DateValidator with format `php:U` does not truncate timestamp to date. |
||
| 667 | * @see https://github.com/yiisoft/yii2/issues/15628 |
||
| 668 | */ |
||
| 669 | public function testIssue15628() |
||
| 670 | { |
||
| 671 | $validator = new Date(['format' => 'php:U' , 'type' => Date::TYPE_DATETIME, 'timestampAttribute' => 'attr_date']); |
||
| 672 | $model = new FakedValidationModel(); |
||
| 673 | $value = 1518023610; |
||
| 674 | $model->attr_date = $value; |
||
| 675 | |||
| 679 | } |
||
| 680 | } |
||
| 681 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths