Skip to content

Commit

Permalink
Use new timezones in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Nov 19, 2023
1 parent 9051283 commit c7ea083
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
16 changes: 16 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Carbon\CarbonInterface;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Carbon\CarbonTimeZone;
use Carbon\Translator;
use Closure;
use DateTime;
Expand Down Expand Up @@ -328,4 +329,19 @@ protected function areSameLocales($a, $b)

return $a === $b;
}

protected function firstValidTimezoneAmong(array $timezones): CarbonTimeZone
{
$firstError = null;

foreach ($timezones as $timezone) {
try {
return new CarbonTimeZone($timezone);
} catch (Throwable $exception) {
$firstError = $firstError ?? $exception;
}
}

throw $firstError;
}
}
8 changes: 5 additions & 3 deletions tests/Carbon/SerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ public function testFromUnserializedWithInvalidValue($value)

public function testDateSerializationReflectionCompatibility()
{
$tz = $this->firstValidTimezoneAmong(['America/Los_Angeles', 'US/Pacific'])->getName();

try {
$reflection = (new ReflectionClass(DateTime::class))->newInstanceWithoutConstructor();

@$reflection->date = '1990-01-17 10:28:07';
@$reflection->timezone_type = 3;
@$reflection->timezone = 'US/Pacific';
@$reflection->timezone = $tz;

$date = unserialize(serialize($reflection));
} catch (Throwable $exception) {
Expand All @@ -118,7 +120,7 @@ public function testDateSerializationReflectionCompatibility()

@$reflection->date = '1990-01-17 10:28:07';
@$reflection->timezone_type = 3;
@$reflection->timezone = 'US/Pacific';
@$reflection->timezone = $tz;

$date = unserialize(serialize($reflection));

Expand Down Expand Up @@ -146,7 +148,7 @@ public function testDateSerializationReflectionCompatibility()

$setValue('date', '1990-01-17 10:28:07');
$setValue('timezone_type', 3);
$setValue('timezone', 'US/Pacific');
$setValue('timezone', $tz);

$date = unserialize(serialize($target));

Expand Down
9 changes: 5 additions & 4 deletions tests/Carbon/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,20 @@ public function testCreateFromPartialFormat()
$this->assertSame('2018-05-06T00:00:00-07:00', Carbon::createFromFormat('Y-m-d|', '2018-05-06')->toIso8601String());
$this->assertSame('1970-01-01T10:20:30-08:00', Carbon::createFromFormat('|H:i:s', '10:20:30')->toIso8601String());

$kyiv = $this->firstValidTimezoneAmong(['Europe/Kyiv', 'Europe/Kiev'])->getName();
// Resetting to epoch (timezone fun).
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('|', '')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('e|', 'Europe/Kiev')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('e|', $kyiv)->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+01:00', Carbon::createFromFormat('|', '', 'Europe/London')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('!', '')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('!e', 'Europe/Kiev')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('!e', $kyiv)->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+01:00', Carbon::createFromFormat('!', '', 'Europe/London')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('e!', 'Europe/Kiev')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('e!', $kyiv)->toIso8601String());

// Escaped epoch resets.
$this->assertSame('2013-09-01T05:10:15-07:00', Carbon::createFromFormat('\|', '|')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15-07:00', Carbon::createFromFormat('\!', '!')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15+03:00', Carbon::createFromFormat('e \!', 'Europe/Kiev !')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15+03:00', Carbon::createFromFormat('e \!', $kyiv.' !')->toIso8601String());
}

public function testCreateFromPartialFormatWithMicroseconds()
Expand Down
12 changes: 8 additions & 4 deletions tests/CarbonImmutable/SerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ public function testFromUnserializedWithInvalidValue($value)

public function testDateSerializationReflectionCompatibility()
{
$tz = $this->firstValidTimezoneAmong(['America/Los_Angeles', 'US/Pacific'])->getName();

try {
$reflection = (new ReflectionClass(DateTimeImmutable::class))->newInstanceWithoutConstructor();

@$reflection->date = '1990-01-17 10:28:07';
@$reflection->timezone_type = 3;
@$reflection->timezone = 'US/Pacific';
@$reflection->timezone = $tz;

$date = unserialize(serialize($reflection));
} catch (Throwable $exception) {
Expand All @@ -115,7 +117,7 @@ public function testDateSerializationReflectionCompatibility()

@$reflection->date = '1990-01-17 10:28:07';
@$reflection->timezone_type = 3;
@$reflection->timezone = 'US/Pacific';
@$reflection->timezone = $tz;

$date = unserialize(serialize($reflection));

Expand Down Expand Up @@ -143,7 +145,7 @@ public function testDateSerializationReflectionCompatibility()

$setValue('date', '1990-01-17 10:28:07');
$setValue('timezone_type', 3);
$setValue('timezone', 'US/Pacific');
$setValue('timezone', $tz);

$date = unserialize(serialize($target));

Expand Down Expand Up @@ -209,12 +211,14 @@ public function testSerializeRawMethod(): void

public function testWakeupRawMethod(): void
{
$tz = $this->firstValidTimezoneAmong(['America/Los_Angeles', 'US/Pacific'])->getName();

/** @var Carbon $date */
$date = (new ReflectionClass(Carbon::class))->newInstanceWithoutConstructor();

@$date->date = '1990-01-17 10:28:07';
@$date->timezone_type = 3;
@$date->timezone = 'US/Pacific';
@$date->timezone = $tz;
@$date->dumpLocale = 'es';

$date->__wakeup();
Expand Down
9 changes: 5 additions & 4 deletions tests/CarbonImmutable/TestingAidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,20 @@ public function testCreateFromPartialFormat()
$this->assertSame('2018-05-06T00:00:00-07:00', Carbon::createFromFormat('Y-m-d|', '2018-05-06')->toIso8601String());
$this->assertSame('1970-01-01T10:20:30-08:00', Carbon::createFromFormat('|H:i:s', '10:20:30')->toIso8601String());

$kyiv = $this->firstValidTimezoneAmong(['Europe/Kyiv', 'Europe/Kiev'])->getName();
// Resetting to epoch (timezone fun).
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('|', '')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('e|', 'Europe/Kiev')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('e|', $kyiv)->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+01:00', Carbon::createFromFormat('|', '', 'Europe/London')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('!', '')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('!e', 'Europe/Kiev')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+03:00', Carbon::createFromFormat('!e', $kyiv)->toIso8601String());
$this->assertSame('1970-01-01T00:00:00+01:00', Carbon::createFromFormat('!', '', 'Europe/London')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('e!', 'Europe/Kiev')->toIso8601String());
$this->assertSame('1970-01-01T00:00:00-08:00', Carbon::createFromFormat('e!', $kyiv)->toIso8601String());

// Escaped epoch resets.
$this->assertSame('2013-09-01T05:10:15-07:00', Carbon::createFromFormat('\|', '|')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15-07:00', Carbon::createFromFormat('\!', '!')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15+03:00', Carbon::createFromFormat('e \!', 'Europe/Kiev !')->toIso8601String());
$this->assertSame('2013-09-01T05:10:15+03:00', Carbon::createFromFormat('e \!', $kyiv.' !')->toIso8601String());

Carbon::setTestNow('2023-12-05 21:09:54');
$this->assertSame('2023-12-05 15:00:00.000000', Carbon::createFromFormat('H', '15')->format('Y-m-d H:i:s.u'));
Expand Down
2 changes: 1 addition & 1 deletion tests/CarbonTimeZone/ConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function testToOffsetNameFromDifferentCreationMethods()
$summer = Carbon::parse('2020-06-15');
$winter = Carbon::parse('2018-12-20');
$this->assertSame('+02:00', (new CarbonTimeZone('Europe/Paris'))->toOffsetName());
$this->assertSame('+05:30', (new CarbonTimeZone('Asia/Calcutta'))->toOffsetName());
$this->assertSame('+05:30', $this->firstValidTimezoneAmong(['Asia/Kolkata', 'Asia/Calcutta'])->toOffsetName());
$this->assertSame('+13:45', CarbonTimeZone::create('Pacific/Chatham')->toOffsetName($winter));
$this->assertSame('+12:00', CarbonTimeZone::create('Pacific/Auckland')->toOffsetName($summer));
$this->assertSame('-05:15', CarbonTimeZone::createFromHourOffset(-5.25)->toOffsetName());
Expand Down

0 comments on commit c7ea083

Please sign in to comment.