Skip to content

Commit

Permalink
Fixed TimestampBehavior failing when type isn't DateTimeType
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Mar 19, 2018
1 parent c969cc4 commit 606717f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/ORM/Behavior/TimestampBehavior.php
Expand Up @@ -204,6 +204,13 @@ protected function _updateField($entity, $field, $refreshTimestamp)

/** @var \Cake\Database\Type\DateTimeType $type */
$type = Type::build($columnType);

if (!$type instanceof Type\DateTimeType) {
$entity->set($field, (string)$ts);

return;
}

$class = $type->getDateTimeClassName();

$entity->set($field, new $class($ts));
Expand Down
15 changes: 14 additions & 1 deletion tests/TestCase/ORM/Behavior/TimestampBehaviorTest.php
Expand Up @@ -226,7 +226,15 @@ public function testModifiedMissingColumn()
public function testUseImmutable()
{
$table = $this->getTable();
$this->Behavior = new TimestampBehavior($table);
$this->Behavior = new TimestampBehavior($table, [
'events' => [
'Model.beforeSave' => [
'created' => 'new',
'modified' => 'always',
'timestamp_str' => 'always',
]
],
]);
$entity = new Entity();
$event = new Event('Model.beforeSave');

Expand All @@ -239,6 +247,10 @@ public function testUseImmutable()
$entity->clean();
$this->Behavior->handleEvent($event, $entity);
$this->assertInstanceOf('Cake\I18n\Time', $entity->modified);

$entity->clean();
$this->Behavior->handleEvent($event, $entity);
$this->assertInternalType('string', $entity->timestamp_str);
}

/**
Expand Down Expand Up @@ -457,6 +469,7 @@ protected function getTable()
'created' => ['type' => 'datetime'],
'modified' => ['type' => 'timestamp'],
'date_specialed' => ['type' => 'datetime'],
'timestamp_str' => ['type' => 'string'],
];
$table = new Table(['schema' => $schema]);

Expand Down

0 comments on commit 606717f

Please sign in to comment.