Browse Source

Fixing tests for toQuarter

Jose Lorenzo Rodriguez 12 years ago
parent
commit
1afd8b48df
2 changed files with 22 additions and 23 deletions
  1. 2 2
      src/Utility/Time.php
  2. 20 21
      tests/TestCase/Utility/TimeTest.php

+ 2 - 2
src/Utility/Time.php

@@ -132,7 +132,7 @@ class Time extends Carbon {
  *
  * @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;
@@ -250,7 +250,7 @@ class Time extends Carbon {
 			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));
 		}
 

+ 20 - 21
tests/TestCase/Utility/TimeTest.php

@@ -67,31 +67,30 @@ class TimeTest extends TestCase {
 	}
 
 /**
+ * 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));
 	}
 
 /**