Browse Source

Fix Type::buildAll() not returning type instance set using Type::set().

Closes #13358
ADmad 6 years ago
parent
commit
a3c5faba44

+ 1 - 0
src/Database/Type.php

@@ -138,6 +138,7 @@ class Type implements TypeInterface
     public static function set($name, Type $instance)
     {
         static::$_builtTypes[$name] = $instance;
+        static::$_types[$name] = get_class($instance);
     }
 
     /**

+ 13 - 2
tests/TestCase/Database/QueryTest.php

@@ -20,9 +20,11 @@ use Cake\Database\Expression\QueryExpression;
 use Cake\Database\Query;
 use Cake\Database\StatementInterface;
 use Cake\Database\Statement\StatementDecorator;
+use Cake\Database\Type;
 use Cake\Database\TypeMap;
 use Cake\Datasource\ConnectionManager;
 use Cake\TestSuite\TestCase;
+use TestApp\Database\Type\BarType;
 
 /**
  * Tests Query class
@@ -4481,16 +4483,25 @@ class QueryTest extends TestCase
      */
     public function testSelectTypeConversion()
     {
+        Type::set('custom_datetime', new BarType('custom_datetime'));
         $this->loadFixtures('Comments');
+
         $query = new Query($this->connection);
         $query
-            ->select(['id', 'comment', 'the_date' => 'created'])
+            ->select(['id', 'comment', 'the_date' => 'created', 'updated'])
             ->from('comments')
             ->limit(1)
-            ->getSelectTypeMap()->setTypes(['id' => 'integer', 'the_date' => 'datetime']);
+            ->getSelectTypeMap()
+                ->setTypes([
+                    'id' => 'integer',
+                    'the_date' => 'datetime',
+                    'updated' => 'custom_datetime'
+                ]);
+
         $result = $query->execute()->fetchAll('assoc');
         $this->assertInternalType('integer', $result[0]['id']);
         $this->assertInstanceOf('DateTime', $result[0]['the_date']);
+        $this->assertInstanceOf('DateTime', $result[0]['updated']);
     }
 
     /**

+ 15 - 0
tests/TestCase/Database/TypeTest.php

@@ -147,6 +147,21 @@ class TypeTest extends TestCase
     }
 
     /**
+     * Tests new types set with set() are returned by buildAll()
+     *
+     * @return void
+     */
+    public function testSetAndBuild()
+    {
+        $types = Type::buildAll();
+        $this->assertFalse(isset($types['foo']));
+
+        Type::set('foo', new FooType());
+        $types = Type::buildAll();
+        $this->assertTrue(isset($types['foo']));
+    }
+
+    /**
      * Tests overwriting type map works for building
      *
      * @return void

+ 3 - 3
tests/test_app/TestApp/Database/Type/BarType.php

@@ -14,12 +14,12 @@
  */
 namespace TestApp\Database\Type;
 
-use Cake\Database\Type;
+use Cake\Database\Type\DateTimeType;
 
-class BarType extends Type
+class BarType extends DateTimeType
 {
     public function getBaseType()
     {
-        return 'text';
+        return 'datetimetype';
     }
 }