From dc571bab2985cb4539ba1f1ee2d9bd45da0348f5 Mon Sep 17 00:00:00 2001 From: AD7six Date: Mon, 4 Nov 2013 01:36:45 +0000 Subject: [PATCH] merge get+set timestamp and change possible $when values to "always" "new" "existing" --- Cake/Model/Behavior/TimestampBehavior.php | 44 ++++------- .../Model/Behavior/TimestampBehaviorTest.php | 78 ++++--------------- 2 files changed, 33 insertions(+), 89 deletions(-) diff --git a/Cake/Model/Behavior/TimestampBehavior.php b/Cake/Model/Behavior/TimestampBehavior.php index 3e9d6b0714e..481e116becf 100644 --- a/Cake/Model/Behavior/TimestampBehavior.php +++ b/Cake/Model/Behavior/TimestampBehavior.php @@ -27,8 +27,8 @@ class TimestampBehavior extends Behavior { * These are merged with user-provided settings when the behavior is used. * * events - an event-name keyed array of which fields to update, and when, for a given event - * possible values for when a field will be updated are true, "new" or "existing" to set the - * field value always, only when a new record or only when an existing record. + * possible values for when a field will be updated are "always", "new" or "existing", to set + * the field value always, only when a new record or only when an existing record. * * refreshTimestamp - if true (the default) the timestamp used will be the current time when * the code is executed, to set to an explicit date time value - set refreshTimetamp to false @@ -40,7 +40,7 @@ class TimestampBehavior extends Behavior { 'events' => [ 'Model.beforeSave' => [ 'created' => 'new', - 'modified' => true, + 'modified' => 'always' ] ], 'refreshTimestamp' => true @@ -83,9 +83,9 @@ public function handleEvent(Event $event, Entity $entity) { $new = $entity->isNew() !== false; - foreach($settings['events'][$eventName] as $field => $when) { + foreach ($settings['events'][$eventName] as $field => $when) { if ( - $when === true || + $when === 'always' || ($when === 'new' && $new) || ($when === 'existing' && !$new) ) { @@ -105,43 +105,31 @@ public function handleEvent(Event $event, Entity $entity) { */ public function implementedEvents() { $events = array_flip(array_keys($this->_settings['events'])); - foreach($events as &$method) { + foreach ($events as &$method) { $method = 'handleEvent'; } return $events; } /** - * getTimestamp + * Get or set the timestamp to be used * - * Gets the current timestamp. If $refreshTimestamp is not truthy, the existing timestamp will be - * returned + * Set the timestamp to the given DateTime object, or if not passed a new DateTime object * + * @param \DateTime $ts + * @param bool $refreshTimestamp * @return \DateTime */ - public function getTimestamp($refreshTimestamp = null) { - if ($this->_ts === null || $refreshTimestamp) { - $this->setTimestamp(); + public function timestamp(\DateTime $ts = null, $refreshTimestamp = false) { + if ($ts) { + $this->_ts = $ts; + } elseif ($this->_ts === null || $refreshTimestamp) { + $this->_ts = new \DateTime(); } return $this->_ts; } -/** - * setTimestamp - * - * Set the timestamp to the given DateTime object, or if not passed a new DateTime object - * - * @param int $ts - * @return void - */ - public function setTimestamp(\DateTime $ts = null) { - if ($ts === null) { - $ts = new \DateTime(); - } - $this->_ts = $ts; - } - /** * Update a field, if it hasn't been updated already * @@ -154,6 +142,6 @@ protected function _updateField(Entity $entity, $field, $refreshTimestamp) { if ($entity->dirty($field)) { return; } - $entity->set($field, $this->getTimestamp($refreshTimestamp)); + $entity->set($field, $this->timestamp(null, $refreshTimestamp)); } } diff --git a/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php b/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php index 79843bafecc..52686db7d89 100644 --- a/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php +++ b/Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php @@ -48,7 +48,7 @@ public function testImplementedEventsDefault() { */ public function testImplementedEventsCustom() { $table = $this->getMock('Cake\ORM\Table'); - $settings = ['events' => ['Something.special' => ['date_specialed' => true]]]; + $settings = ['events' => ['Something.special' => ['date_specialed' => 'always']]]; $this->Behavior = new TimestampBehavior($table, $settings); $expected = [ @@ -66,7 +66,7 @@ public function testCreatedAbsent() { $table = $this->getMock('Cake\ORM\Table'); $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); $ts = new \DateTime('2000-01-01'); - $this->Behavior->setTimestamp($ts); + $this->Behavior->timestamp($ts); $event = new Event('Model.beforeSave'); $entity = new Entity(['name' => 'Foo']); @@ -85,7 +85,7 @@ public function testCreatedPresent() { $table = $this->getMock('Cake\ORM\Table'); $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); $ts = new \DateTime('2000-01-01'); - $this->Behavior->setTimestamp($ts); + $this->Behavior->timestamp($ts); $event = new Event('Model.beforeSave'); $existingValue = new \DateTime('2011-11-11'); @@ -105,7 +105,7 @@ public function testCreatedNotNew() { $table = $this->getMock('Cake\ORM\Table'); $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); $ts = new \DateTime('2000-01-01'); - $this->Behavior->setTimestamp($ts); + $this->Behavior->timestamp($ts); $event = new Event('Model.beforeSave'); $entity = new Entity(['name' => 'Foo']); @@ -125,7 +125,7 @@ public function testModifiedAbsent() { $table = $this->getMock('Cake\ORM\Table'); $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); $ts = new \DateTime('2000-01-01'); - $this->Behavior->setTimestamp($ts); + $this->Behavior->timestamp($ts); $event = new Event('Model.beforeSave'); $entity = new Entity(['name' => 'Foo']); @@ -145,7 +145,7 @@ public function testModifiedPresent() { $table = $this->getMock('Cake\ORM\Table'); $this->Behavior = new TimestampBehavior($table, ['refreshTimestamp' => false]); $ts = new \DateTime('2000-01-01'); - $this->Behavior->setTimestamp($ts); + $this->Behavior->timestamp($ts); $event = new Event('Model.beforeSave'); $existingValue = new \DateTime('2011-11-11'); @@ -167,16 +167,11 @@ public function testGetTimestamp() { $table = $this->getMock('Cake\ORM\Table'); $this->Behavior = new TimestampBehavior($table); - $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); - $property->setAccessible(true); - - $this->assertNull($property->getValue($this->Behavior), 'Should be null be default'); - - $return = $this->Behavior->getTimestamp(); + $return = $this->Behavior->timestamp(); $this->assertInstanceOf( 'DateTime', $return, - 'After calling for the first time, should be a date time object' + 'Should return a timestamp object' ); return $this->Behavior; @@ -191,17 +186,13 @@ public function testGetTimestamp() { public function testGetTimestampPersists($behavior) { $this->Behavior = $behavior; - $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); - $property->setAccessible(true); - - $initialValue = $property->getValue($this->Behavior); - $this->Behavior->getTimestamp(); - $postValue = $property->getValue($this->Behavior); + $initialValue = $this->Behavior->timestamp(); + $postValue = $this->Behavior->timestamp(); $this->assertSame( $initialValue, $postValue, - 'The timestamp should be exactly the same value' + 'The timestamp should be exactly the same object' ); } @@ -214,12 +205,8 @@ public function testGetTimestampPersists($behavior) { public function testGetTimestampRefreshes($behavior) { $this->Behavior = $behavior; - $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); - $property->setAccessible(true); - - $initialValue = $property->getValue($this->Behavior); - $this->Behavior->getTimestamp(true); - $postValue = $property->getValue($this->Behavior); + $initialValue = $this->Behavior->timestamp(); + $postValue = $this->Behavior->timestamp(null, true); $this->assertNotSame( $initialValue, @@ -228,28 +215,6 @@ public function testGetTimestampRefreshes($behavior) { ); } -/** - * testSetTimestampDefault - * - * @return void - */ - public function testSetTimestampDefault() { - $table = $this->getMock('Cake\ORM\Table'); - $this->Behavior = new TimestampBehavior($table); - - $this->Behavior->setTimestamp(); - - $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); - $property->setAccessible(true); - $set = $property->getValue($this->Behavior); - - $this->assertInstanceOf( - 'DateTime', - $set, - 'After calling for the first time, should be a date time object' - ); - } - /** * testSetTimestampExplicit * @@ -260,22 +225,13 @@ public function testSetTimestampExplicit() { $this->Behavior = new TimestampBehavior($table); $ts = new \DateTime(); - $this->Behavior->setTimestamp($ts); - - $property = new \ReflectionProperty('Cake\Model\Behavior\TimestampBehavior', '_ts'); - $property->setAccessible(true); - $set = $property->getValue($this->Behavior); - - $this->assertInstanceOf( - 'DateTime', - $set, - 'After calling for the first time, should be a date time object' - ); + $this->Behavior->timestamp($ts); + $return = $this->Behavior->timestamp(); $this->assertSame( $ts, - $set, - 'Should have set the same object passed in' + $return, + 'Should return the same value as initially set' ); } }