Skip to content

Commit

Permalink
merge get+set timestamp
Browse files Browse the repository at this point in the history
and change possible $when values to "always" "new" "existing"
  • Loading branch information
AD7six committed Nov 4, 2013
1 parent dc2238c commit dc571ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 89 deletions.
44 changes: 16 additions & 28 deletions Cake/Model/Behavior/TimestampBehavior.php
Expand Up @@ -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
Expand All @@ -40,7 +40,7 @@ class TimestampBehavior extends Behavior {
'events' => [
'Model.beforeSave' => [
'created' => 'new',
'modified' => true,
'modified' => 'always'
]
],
'refreshTimestamp' => true
Expand Down Expand Up @@ -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)
) {
Expand All @@ -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
*
Expand All @@ -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));
}
}
78 changes: 17 additions & 61 deletions Cake/Test/TestCase/Model/Behavior/TimestampBehaviorTest.php
Expand Up @@ -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 = [
Expand All @@ -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']);
Expand All @@ -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');
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -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'
);
}

Expand All @@ -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,
Expand All @@ -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
*
Expand All @@ -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'
);
}
}

0 comments on commit dc571ba

Please sign in to comment.