Browse Source

Merge pull request #11847 from jeremyharris/timestamp-behavior

Fixed TimestampBehavior failing when type isn't DateTimeType
Mark Story 8 years ago
parent
commit
060a9ebe3a

+ 8 - 0
src/ORM/Behavior/TimestampBehavior.php

@@ -204,6 +204,14 @@ class TimestampBehavior extends Behavior
 
         /** @var \Cake\Database\Type\DateTimeType $type */
         $type = Type::build($columnType);
+
+        if (!$type instanceof Type\DateTimeType) {
+            deprecationWarning('TimestampBehavior support for column types other than DateTimeType will be removed in 4.0.');
+            $entity->set($field, (string)$ts);
+
+            return;
+        }
+
         $class = $type->getDateTimeClassName();
 
         $entity->set($field, new $class($ts));

+ 27 - 0
tests/TestCase/ORM/Behavior/TimestampBehaviorTest.php

@@ -21,6 +21,7 @@ use Cake\ORM\Behavior\TimestampBehavior;
 use Cake\ORM\Entity;
 use Cake\ORM\Table;
 use Cake\TestSuite\TestCase;
+use PHPUnit\Framework\Error\Deprecated;
 
 /**
  * Behavior test case
@@ -242,6 +243,31 @@ class TimestampBehaviorTest extends TestCase
     }
 
     /**
+     * tests using non-DateTimeType throws deprecation warning
+     *
+     * @return void
+     */
+    public function testNonDateTimeTypeDeprecated()
+    {
+        $this->expectException(Deprecated::class);
+        $this->expectExceptionMessage('TimestampBehavior support for column types other than DateTimeType will be removed in 4.0.');
+
+        $table = $this->getTable();
+        $this->Behavior = new TimestampBehavior($table, [
+            'events' => [
+                'Model.beforeSave' => [
+                    'timestamp_str' => 'always',
+                ]
+            ],
+        ]);
+
+        $entity = new Entity();
+        $event = new Event('Model.beforeSave');
+        $this->Behavior->handleEvent($event, $entity);
+        $this->assertInternalType('string', $entity->timestamp_str);
+    }
+
+    /**
      * testInvalidEventConfig
      *
      * @return void
@@ -457,6 +483,7 @@ class TimestampBehaviorTest extends TestCase
             'created' => ['type' => 'datetime'],
             'modified' => ['type' => 'timestamp'],
             'date_specialed' => ['type' => 'datetime'],
+            'timestamp_str' => ['type' => 'string'],
         ];
         $table = new Table(['schema' => $schema]);