Browse Source

Merge pull request #6476 from cakephp/issue-6474

Add Type::set()
José Lorenzo Rodríguez 11 years ago
parent
commit
52feab2b26
2 changed files with 24 additions and 0 deletions
  1. 12 0
      src/Database/Type.php
  2. 12 0
      tests/TestCase/Database/TypeTest.php

+ 12 - 0
src/Database/Type.php

@@ -106,6 +106,18 @@ class Type
     }
 
     /**
+     * Returns a Type object capable of converting a type identified by $name
+     *
+     * @param string $name The type identifier you want to set.
+     * @param \Cake\Databse\Type $instance The type instance you want to set.
+     * @return void
+     */
+    public static function set($name, Type $instance)
+    {
+        static::$_builtTypes[$name] = $instance;
+    }
+
+    /**
      * Registers a new type identifier and maps it to a fully namespaced classname,
      * If called with no arguments it will return current types map array
      * If $className is omitted it will return mapped class for $type

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

@@ -380,4 +380,16 @@ class TypeTest extends TestCase
         $driver = $this->getMock('\Cake\Database\Driver');
         $this->assertEquals(PDO::PARAM_STR, $type->toStatement($string, $driver));
     }
+
+    /**
+     * Test setting instances into the factory/registry.
+     *
+     * @return void
+     */
+    public function testSet()
+    {
+        $instance = $this->getMock('Cake\Database\Type');
+        Type::set('random', $instance);
+        $this->assertSame($instance, Type::build('random'));
+    }
 }