Skip to content

Commit

Permalink
Fixing tests for toQuarter
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 21, 2014
1 parent 0534486 commit 1afd8b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Utility/Time.php
Expand Up @@ -132,7 +132,7 @@ public function isThisYear() {
*
* @return mixed 1, 2, 3, or 4 quarter of year or array if $range true
*/
public function toQuarter() {
public function toQuarter($range = false) {
$quarter = ceil($this->format('m') / 3);
if ($range === false) {
return $quarter;
Expand Down Expand Up @@ -250,7 +250,7 @@ public function timeAgoInWords(array $options = []) {
return __d('cake', 'just now', 'just now');
}

if ($diff > abs($now - self::fromString($end))) {
if ($diff > abs($now - (new static($end))->format('U'))) {
return sprintf($absoluteString, date($format, $inSeconds));
}

Expand Down
41 changes: 20 additions & 21 deletions tests/TestCase/Utility/TimeTest.php
Expand Up @@ -66,32 +66,31 @@ protected function _restoreSystemTimezone() {
date_default_timezone_set($this->_systemTimezoneIdentifier);
}

/**
* Provides values and expectations for the toQuarter method
*
* @return array
*/
public function toQuarterProvider() {
return [
['2007-12-25', 4],
['2007-9-25', 3],
['2007-3-25', 1],
['2007-3-25', ['2007-01-01', '2007-03-31'], true],
['2007-5-25', ['2007-04-01', '2007-06-30'], true],
['2007-8-25', ['2007-07-01', '2007-09-30'], true],
['2007-12-25', ['2007-10-01', '2007-12-31'], true],
];
}

/**
* testToQuarter method
*
* @dataProvider toQuarterProvider
* @return void
*/
public function testToQuarter() {
$result = $this->Time->toQuarter('2007-12-25');
$this->assertEquals(4, $result);

$result = $this->Time->toQuarter('2007-9-25');
$this->assertEquals(3, $result);

$result = $this->Time->toQuarter('2007-3-25');
$this->assertEquals(1, $result);

$result = $this->Time->toQuarter('2007-3-25', true);
$this->assertEquals(array('2007-01-01', '2007-03-31'), $result);

$result = $this->Time->toQuarter('2007-5-25', true);
$this->assertEquals(array('2007-04-01', '2007-06-30'), $result);

$result = $this->Time->toQuarter('2007-8-25', true);
$this->assertEquals(array('2007-07-01', '2007-09-30'), $result);

$result = $this->Time->toQuarter('2007-12-25', true);
$this->assertEquals(array('2007-10-01', '2007-12-31'), $result);
public function testToQuarter($date, $expected, $range = false) {
$this->assertEquals($expected, (new Time($date))->toQuarter($range));
}

/**
Expand Down

0 comments on commit 1afd8b4

Please sign in to comment.