Skip to content

Loading…

Test Aid Fix #44

Merged
merged 1 commit into from

2 participants

@nicholas-eden

Fixes the problem where if a test aid is set, it's time zone would override any time zone passed in code.

Instead will alter the test time relative to the time zone passed.

@briannesbitt briannesbitt merged commit 3d97282 into briannesbitt:master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 18 deletions.
  1. +13 −18 src/Carbon/Carbon.php
  2. +10 −0 tests/TestingAidsTest.php
View
31 src/Carbon/Carbon.php
@@ -146,12 +146,19 @@ public function __construct($time = null, $tz = null)
// If the class has a test now set and we are trying to create a now()
// instance then override as required
if (static::hasTestNow() && (empty($time) || $time === 'now' || self::hasRelativeKeywords($time))) {
- if (self::hasRelativeKeywords($time)) {
- $time = static::getRelativeTest($time)->toDateTimeString();
- } else {
- $time = static::getTestNow()->toDateTimeString();
- }
- $tz = static::getTestNow()->tz;
+ $testInstance = clone static::getTestNow();
+ if (self::hasRelativeKeywords($time)) {
+ $testInstance->modify($time);
+ }
+
+ //shift the time according to the given time zone
+ if ($tz !== NULL && $tz != static::getTestNow()->tz) {
+ $testInstance->setTimezone($tz);
+ } else {
+ $tz = $testInstance->tz;
+ }
+
+ $time = $testInstance->toDateTimeString();
}
if ($tz !== null) {
@@ -751,18 +758,6 @@ public static function hasRelativeKeywords($time) {
return false;
}
- /**
- * Gets a Carbon instance relative to the current test instance.
- * e.g.: last thursday, tomorrow
- *
- * @return Carbon relative to the current instance used for testing
- */
- public static function getRelativeTest($time) {
- $instance = new static();
- $instance->modify($time);
- return $instance;
- }
-
///////////////////////////////////////////////////////////////////
/////////////////////// STRING FORMATTING /////////////////////////
///////////////////////////////////////////////////////////////////
View
10 tests/TestingAidsTest.php
@@ -101,4 +101,14 @@ public function testParseRelativeWithTestValueSet()
$this->assertEquals('2013-10-01 05:15:05', Carbon::parse('first day of next month')->toDateTimeString());
$this->assertEquals('2013-09-30 05:15:05', Carbon::parse('last day of this month')->toDateTimeString());
}
+
+ public function testTimeZoneWithTestValueSet()
+ {
+ $notNow = Carbon::parse('2013-07-01 12:00:00', 'America/New_York');
+ Carbon::setTestNow($notNow);
+
+ $this->assertEquals('2013-07-01T12:00:00-0400', Carbon::parse('now')->toISO8601String());
+ $this->assertEquals('2013-07-01T11:00:00-0500', Carbon::parse('now', 'America/Mexico_City')->toISO8601String());
+ $this->assertEquals('2013-07-01T09:00:00-0700', Carbon::parse('now', 'America/Vancouver')->toISO8601String());
+ }
}
Something went wrong with that request. Please try again.