Browse Source

Updating the TimestapBehavior so it uses the new Time lib

Jose Lorenzo Rodriguez 12 years ago
parent
commit
0d5717bbcf

+ 4 - 3
src/Model/Behavior/TimestampBehavior.php

@@ -18,6 +18,7 @@ use Cake\Event\Event;
 use Cake\ORM\Behavior;
 use Cake\ORM\Entity;
 use Cake\ORM\Table;
+use Cake\Utility\Time;
 
 class TimestampBehavior extends Behavior {
 
@@ -131,16 +132,16 @@ class TimestampBehavior extends Behavior {
  *
  * @param \DateTime $ts
  * @param bool $refreshTimestamp
- * @return \DateTime
+ * @return \Cake\Utility\Time
  */
 	public function timestamp(\DateTime $ts = null, $refreshTimestamp = false) {
 		if ($ts) {
 			if ($this->_config['refreshTimestamp']) {
 				$this->_config['refreshTimestamp'] = false;
 			}
-			$this->_ts = $ts;
+			$this->_ts = new Time($ts);
 		} elseif ($this->_ts === null || $refreshTimestamp) {
-			$this->_ts = new \DateTime();
+			$this->_ts = new Time();
 		}
 
 		return $this->_ts;

+ 1 - 1
src/Utility/Time.php

@@ -105,7 +105,7 @@ class Time extends Carbon implements JsonSerializable {
  */
 	public function __construct($time = null, $tz = null) {
 		if ($time instanceof \DateTime) {
-			list($time, $tz) = [$dt->format('Y-m-d H:i:s'), $dt->getTimeZone()];
+			list($time, $tz) = [$time->format('Y-m-d H:i:s'), $time->getTimeZone()];
 		}
 
 		if (is_numeric($time)) {

+ 9 - 6
tests/TestCase/Model/Behavior/TimestampBehaviorTest.php

@@ -92,7 +92,8 @@ class TimestampBehaviorTest extends TestCase {
 
 		$return = $this->Behavior->handleEvent($event, $entity);
 		$this->assertTrue($return, 'Handle Event is expected to always return true');
-		$this->assertSame($ts, $entity->created, 'Created timestamp is expected to be the mocked value');
+		$this->assertInstanceOf('Cake\Utility\Time', $entity->created);
+		$this->assertSame($ts->format('c'), $entity->created->format('c'), 'Created timestamp is not the same');
 	}
 
 /**
@@ -152,7 +153,8 @@ class TimestampBehaviorTest extends TestCase {
 
 		$return = $this->Behavior->handleEvent($event, $entity);
 		$this->assertTrue($return, 'Handle Event is expected to always return true');
-		$this->assertSame($ts, $entity->modified, 'Modified timestamp is expected to be the mocked value');
+		$this->assertInstanceOf('Cake\Utility\Time', $entity->modified);
+		$this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is not the same');
 	}
 
 /**
@@ -174,7 +176,8 @@ class TimestampBehaviorTest extends TestCase {
 
 		$return = $this->Behavior->handleEvent($event, $entity);
 		$this->assertTrue($return, 'Handle Event is expected to always return true');
-		$this->assertSame($ts, $entity->modified, 'Modified timestamp is expected to be updated');
+		$this->assertInstanceOf('Cake\Utility\Time', $entity->modified);
+		$this->assertSame($ts->format('c'), $entity->modified->format('c'), 'Modified timestamp is expected to be updated');
 	}
 
 /**
@@ -269,9 +272,9 @@ class TimestampBehaviorTest extends TestCase {
 		$this->Behavior->timestamp($ts);
 		$return = $this->Behavior->timestamp();
 
-		$this->assertSame(
-			$ts,
-			$return,
+		$this->assertEquals(
+			$ts->format('c'),
+			$return->format('c'),
 			'Should return the same value as initially set'
 		);
 	}