Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #38 from nicholas-eden/master

Add test aid for relative terms
  • Loading branch information...
commit ca70fce1c9a61e8bff8640f06bee4f7a944f7234 2 parents 6a8b7fd + afe75f1
@briannesbitt briannesbitt authored
View
26 readme.md
@@ -177,13 +177,13 @@ To accompany `now()`, a few other static instantiation helpers exist to create w
```php
$now = Carbon::now();
-echo $now; // 2013-08-21 00:33:54
+echo $now; // 2013-09-05 23:38:31
$today = Carbon::today();
-echo $today; // 2013-08-21 00:00:00
+echo $today; // 2013-09-05 00:00:00
$tomorrow = Carbon::tomorrow('Europe/London');
-echo $tomorrow; // 2013-08-22 00:00:00
+echo $tomorrow; // 2013-09-07 00:00:00
$yesterday = Carbon::yesterday();
-echo $yesterday; // 2013-08-20 00:00:00
+echo $yesterday; // 2013-09-04 00:00:00
```
The next group of static helpers are the `createXXX()` helpers. Most of the static `create` functions allow you to provide as many or as few arguments as you want and will provide default values for all others. Generally default values are the current date, time or timezone. Higher values will wrap appropriately but invalid values will throw an `InvalidArgumentException` with an informative message. The message is obtained from an [DateTime::getLastErrors()](http://php.net/manual/en/datetime.getlasterrors.php) call.
@@ -261,7 +261,7 @@ echo Carbon::parse('now'); // 2001-05-21 12:00:00
var_dump(Carbon::hasTestNow()); // bool(true)
Carbon::setTestNow(); // clear the mock
var_dump(Carbon::hasTestNow()); // bool(false)
-echo Carbon::now(); // 2013-08-21 00:33:54
+echo Carbon::now(); // 2013-09-05 23:38:31
```
A more meaning full example:
@@ -296,6 +296,18 @@ echo $product->getPrice(); // 100
Carbon::setTestNow();
```
+Relative phrases are also mocked according to the given "now" instance.
+
+```php
+$knownDate = Carbon::create(2001, 5, 21, 12); // create testing date
+Carbon::setTestNow($knownDate); // set the mock
+echo new Carbon('tomorrow'); // 2001-05-22 12:00:00
+echo new Carbon('yesterday'); // 2001-05-20 12:00:00
+echo new Carbon('next wednesday'); // 2001-05-23 12:00:00
+echo new Carbon('last friday'); // 2001-05-18 12:00:00
+echo new Carbon('this thursday'); // 2001-05-24 12:00:00
+```
+
<a name="api-getters"/>
### Getters
@@ -316,7 +328,7 @@ var_dump($dt->dayOfYear); // int(248)
var_dump($dt->weekOfYear); // int(36)
var_dump($dt->daysInMonth); // int(30)
var_dump($dt->timestamp); // int(1346901971)
-var_dump(Carbon::createFromDate(1975, 5, 21)->age); // int(38) calculated vs now in the same tz
+var_dump(Carbon::createFromDate(1975, 5, 21)->age); // int(25) calculated vs now in the same tz
var_dump($dt->quarter); // int(3)
// Returns an int of seconds difference from UTC (+/- sign included)
@@ -414,7 +426,7 @@ Unfortunately the base class DateTime does not have any localization support. T
```php
setlocale(LC_TIME, 'German');
-echo $dt->formatLocalized('%A %d %B %Y'); // Donnerstag 25 Dezember 1975
+echo $dt->formatLocalized('%A %d %B %Y'); // Thursday 25 December 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Thursday 25 December 1975
```
View
12 readme.src.md
@@ -306,6 +306,18 @@ $product = new SeasonalProduct(100);
{{::lint(Carbon::setTestNow();)}}
```
+Relative phrases are also mocked according to the given "now" instance.
+
+```php
+{{::lint($knownDate = Carbon::create(2001, 5, 21, 12);/*pad(54)*/)}} // create testing date
+{{::lint(Carbon::setTestNow($knownDate);/*pad(54)*/)}} // set the mock
+{{testaid6::exec(echo new Carbon('tomorrow');/*pad(54)*/)}} // {{testaid6_eval}}
+{{testaid7::exec(echo new Carbon('yesterday');/*pad(54)*/)}} // {{testaid7_eval}}
+{{testaid8::exec(echo new Carbon('next wednesday');/*pad(54)*/)}} // {{testaid8_eval}}
+{{testaid9::exec(echo new Carbon('last friday');/*pad(54)*/)}} // {{testaid9_eval}}
+{{testaid10::exec(echo new Carbon('this thursday');/*pad(54)*/)}} // {{testaid10_eval}}
+```
+
<a name="api-getters"/>
### Getters
View
54 src/Carbon/Carbon.php
@@ -71,6 +71,24 @@ class Carbon extends DateTime
self::SATURDAY => 'Saturday'
);
+ /**
+ * Terms used to detect if a time passed is a relative date for testing purposes
+ *
+ * @var array
+ */
+ private static $relativeKeywords = array(
+ 'this',
+ 'next',
+ 'last',
+ 'tomorrow',
+ 'yesterday',
+ '+',
+ '-',
+ 'first',
+ 'last',
+ 'ago'
+ );
+
/**
* Number of X in Y
*/
@@ -127,8 +145,12 @@ 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')) {
- $time = static::getTestNow()->toDateTimeString();
+ 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;
}
@@ -714,6 +736,34 @@ public static function hasTestNow()
return static::getTestNow() !== null;
}
+ /**
+ * Determine if there is a relative keyword in the time string, this is to
+ * create dates relative to now for test instances. e.g.: next tuesday
+ *
+ * @return boolean true if there is a keyword, otherwise false
+ */
+ public static function hasRelativeKeywords($time) {
+ foreach(self::$relativeKeywords as $keyword) {
+ if (stripos($time, $keyword) !== false) {
+ return true;
+ }
+ }
+ 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) {
+ $testNow = static::getTestNow();
+ $instance = new static();
+ $instance->modify($time);
+ return $instance;
+ }
+
///////////////////////////////////////////////////////////////////
/////////////////////// STRING FORMATTING /////////////////////////
///////////////////////////////////////////////////////////////////
View
43 tests/TestingAidsTest.php
@@ -58,4 +58,47 @@ public function testParseWithTestValueSet()
$this->assertEquals($notNow, Carbon::parse(''));
$this->assertEquals($notNow, Carbon::parse('now'));
}
+
+ public function testParseRelativeWithTestValueSet()
+ {
+ $notNow = Carbon::parse('2013-09-01 05:15:05');
+ Carbon::setTestNow($notNow);
+
+ $this->assertEquals('2013-09-01 05:10:05', Carbon::parse('5 minutes ago')->toDateTimeString());
+
+ $this->assertEquals('2013-08-25 05:15:05', Carbon::parse('1 week ago')->toDateTimeString());
+
+ $this->assertEquals('2013-09-02', Carbon::parse('tomorrow')->toDateString());
+ $this->assertEquals('2013-08-31', Carbon::parse('yesterday')->toDateString());
+
+ $this->assertEquals('2013-09-02', Carbon::parse('+1 day')->toDateString());
+ $this->assertEquals('2013-08-31', Carbon::parse('-1 day')->toDateString());
+
+ $this->assertEquals('2013-09-02', Carbon::parse('next monday')->toDateString());
+ $this->assertEquals('2013-09-03', Carbon::parse('next tuesday')->toDateString());
+ $this->assertEquals('2013-09-04', Carbon::parse('next wednesday')->toDateString());
+ $this->assertEquals('2013-09-05', Carbon::parse('next thursday')->toDateString());
+ $this->assertEquals('2013-09-06', Carbon::parse('next friday')->toDateString());
+ $this->assertEquals('2013-09-07', Carbon::parse('next saturday')->toDateString());
+ $this->assertEquals('2013-09-08', Carbon::parse('next sunday')->toDateString());
+
+ $this->assertEquals('2013-08-26', Carbon::parse('last monday')->toDateString());
+ $this->assertEquals('2013-08-27', Carbon::parse('last tuesday')->toDateString());
+ $this->assertEquals('2013-08-28', Carbon::parse('last wednesday')->toDateString());
+ $this->assertEquals('2013-08-29', Carbon::parse('last thursday')->toDateString());
+ $this->assertEquals('2013-08-30', Carbon::parse('last friday')->toDateString());
+ $this->assertEquals('2013-08-31', Carbon::parse('last saturday')->toDateString());
+ $this->assertEquals('2013-08-25', Carbon::parse('last sunday')->toDateString());
+
+ $this->assertEquals('2013-09-02', Carbon::parse('this monday')->toDateString());
+ $this->assertEquals('2013-09-03', Carbon::parse('this tuesday')->toDateString());
+ $this->assertEquals('2013-09-04', Carbon::parse('this wednesday')->toDateString());
+ $this->assertEquals('2013-09-05', Carbon::parse('this thursday')->toDateString());
+ $this->assertEquals('2013-09-06', Carbon::parse('this friday')->toDateString());
+ $this->assertEquals('2013-09-07', Carbon::parse('this saturday')->toDateString());
+ $this->assertEquals('2013-09-01', Carbon::parse('this sunday')->toDateString());
+
+ $this->assertEquals('2013-10-01', Carbon::parse('first day of next month')->toDateString());
+ $this->assertEquals('2013-09-30', Carbon::parse('last day of this month')->toDateString());
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.