Permalink
Browse files

Refactor tests.

Use generators to reduce some duplication.
  • Loading branch information...
1 parent 3f78216 commit ad5345887a3cff8adceefe7f2298aaa675a50abd @markstory markstory committed Jun 25, 2012
Showing with 95 additions and 106 deletions.
  1. +95 −106 lib/Cake/Test/Case/Utility/CakeTimeTest.php
View
201 lib/Cake/Test/Case/Utility/CakeTimeTest.php
@@ -61,6 +61,7 @@ public function tearDown() {
protected function _restoreSystemTimezone() {
date_default_timezone_set($this->_systemTimezoneIdentifier);
}
+
/**
* testToQuarter method
*
@@ -90,71 +91,103 @@ public function testToQuarter() {
}
/**
- * testTimeAgoInWords method
+ * provider for timeAgoInWords() tests
*
- * @return void
+ * @return array
*/
- public function testTimeAgoInWords() {
- $result = $this->Time->timeAgoInWords('-1 week');
- $this->assertEquals('1 week ago', $result);
-
- $result = $this->Time->timeAgoInWords('+1 week');
- $this->assertEquals('1 week', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+4 months +2 weeks +3 days'),
- array('end' => '8 years')
- );
- $this->assertEquals('4 months, 2 weeks, 3 days', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+4 months +2 weeks +2 days'),
- array('end' => '8 years')
- );
- $this->assertEquals('4 months, 2 weeks, 2 days', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+4 months +2 weeks +1 day'),
- array('end' => '8 years')
- );
- $this->assertEquals('4 months, 2 weeks, 1 day', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+3 months +2 weeks +1 day'),
- array('end' => '8 years')
- );
- $this->assertEquals('3 months, 2 weeks, 1 day', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+3 months +2 weeks'),
- array('end' => '8 years')
- );
- $this->assertEquals('3 months, 2 weeks', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+3 months +1 week +6 days'),
- array('end' => '8 years')
+ public static function timeAgoProvider() {
+ return array(
+ array('-12 seconds', '12 seconds ago'),
+ array('-12 minutes', '12 minutes ago'),
+ array('-2 hours', '2 hours ago'),
+ array('-1 day', '1 day ago'),
+ array('-2 days', '2 days ago'),
+ array('-2 days -3 hours', '2 days, 3 hours ago'),
+ array('-1 week', '1 week ago'),
+ array('-2 weeks -2 days', '2 weeks, 2 days ago'),
+ array('+1 week', '1 week'),
+ array('+1 week 1 day', '1 week, 1 day'),
+ array('+2 weeks 2 day', '2 weeks, 2 days'),
+ array('2007-9-24', 'on 24/9/07'),
+ array('now', 'just now'),
);
- $this->assertEquals('3 months, 1 week, 6 days', $result);
+ }
- $result = $this->Time->timeAgoInWords(
- strtotime('+2 months +2 weeks +1 day'),
- array('end' => '8 years')
- );
- $this->assertEquals('2 months, 2 weeks, 1 day', $result);
+/**
+ * testTimeAgoInWords method
+ *
+ * @dataProvider timeAgoProvider
+ * @return void
+ */
+ public function testTimeAgoInWords($input, $expected) {
+ $result = $this->Time->timeAgoInWords($input);
+ $this->assertEquals($expected, $result);
+ }
- $result = $this->Time->timeAgoInWords(
- strtotime('+2 months +2 weeks'),
- array('end' => '8 years')
+/**
+ * provider for timeAgo with an end date.
+ *
+ * @return void
+ */
+ public function timeAgoEndProvider() {
+ return array(
+ array(
+ '+4 months +2 weeks +3 days',
+ '4 months, 2 weeks, 3 days',
+ '8 years'
+ ),
+ array(
+ '+4 months +2 weeks +1 day',
+ '4 months, 2 weeks, 1 day',
+ '8 years'
+ ),
+ array(
+ '+3 months +2 weeks',
+ '3 months, 2 weeks',
+ '8 years'
+ ),
+ array(
+ '+3 months +2 weeks +1 day',
+ '3 months, 2 weeks, 1 day',
+ '8 years'
+ ),
+ array(
+ '+1 months +1 week +1 day',
+ '1 month, 1 week, 1 day',
+ '8 years'
+ ),
+ array(
+ '+2 months +2 days',
+ '2 months, 2 days',
+ 'on ' . date('j/n/y', strtotime('+2 months +2 days'))
+ ),
+ array(
+ '+2 months +12 days',
+ '2 months, 1 week, 5 days',
+ '3 months'
+ ),
);
- $this->assertEquals('2 months, 2 weeks', $result);
+ }
+/**
+ * test the end option for timeAgoInWords
+ *
+ * @dataProvider timeAgoEndProvider
+ * @return void
+ */
+ public function testTimeAgoInWordsEnd($input, $expected, $end) {
$result = $this->Time->timeAgoInWords(
- strtotime('+2 months +1 week +6 days'),
- array('end' => '8 years')
+ $input, array('end' => $end)
);
- $this->assertEquals('2 months, 1 week, 6 days', $result);
+ $this->assertEquals($expected, $result);
+ }
+/**
+ * Test the accuracy option for timeAgoInWords()
+ *
+ * @return void
+ */
+ public function testTimeAgoInWordsAccuracy() {
$result = $this->Time->timeAgoInWords(
strtotime('+8 years +4 months +2 weeks +3 days'),
array('accuracy' => array('year' => 'year'), 'end' => '+10 years')
@@ -189,13 +222,14 @@ public function testTimeAgoInWords() {
);
$expected = '1 year';
$this->assertEquals($expected, $result);
+ }
- $result = $this->Time->timeAgoInWords(time());
- $this->assertEquals('just now', $result);
-
- $result = $this->Time->timeAgoInWords('2007-9-25');
- $this->assertEquals('on 25/9/07', $result);
-
+/**
+ * Test the format option of timeAgoInWords()
+ *
+ * @return void
+ */
+ public function testTimeAgoInWordsWithFormat() {
$result = $this->Time->timeAgoInWords('2007-9-25', 'Y-m-d');
$this->assertEquals('on 2007-09-25', $result);
@@ -210,39 +244,6 @@ public function testTimeAgoInWords() {
$result = $this->Time->timeAgoInWords(
strtotime('+2 months +2 days'),
- array('end' => '1 month')
- );
- $this->assertEquals(
- 'on ' . date('j/n/y', strtotime('+2 months +2 days')),
- $result
- );
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+2 months +2 days'),
- array('end' => '3 month')
- );
- $this->assertRegExp('/2 months/', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+2 months +12 days'),
- array('end' => '3 month')
- );
- $this->assertRegExp('/2 months, 1 week/', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+3 months +5 days'),
- array('end' => '4 month')
- );
- $this->assertEquals('3 months, 5 days', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+2 months +2 days'),
- array('end' => '3 month')
- );
- $this->assertRegExp('/2 months/', $result);
-
- $result = $this->Time->timeAgoInWords(
- strtotime('+2 months +2 days'),
array('end' => '1 month', 'format' => 'Y-m-d')
);
$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
@@ -308,18 +309,6 @@ public function testTimeAgoInWordsNegativeValues() {
array('end' => '2 years')
);
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
-
- $result = $this->Time->timeAgoInWords(strtotime('-2 hours'));
- $expected = '2 hours ago';
- $this->assertEquals($expected, $result);
-
- $result = $this->Time->timeAgoInWords(strtotime('-12 minutes'));
- $expected = '12 minutes ago';
- $this->assertEquals($expected, $result);
-
- $result = $this->Time->timeAgoInWords(strtotime('-12 seconds'));
- $expected = '12 seconds ago';
- $this->assertEquals($expected, $result);
}
/**

0 comments on commit ad53458

Please sign in to comment.