diff --git a/src/Database/Type.php b/src/Database/Type.php index f00b6b7fff8..456bfcfd960 100644 --- a/src/Database/Type.php +++ b/src/Database/Type.php @@ -106,7 +106,8 @@ public static function build($name) throw new InvalidArgumentException(sprintf('Unknown type "%s"', $name)); } if (is_string(static::$_types[$name])) { - return static::$_builtTypes[$name] = new static::$_types[$name]($name); + static::$_types[$name] = new static::$_types[$name]($name); + return static::$_builtTypes[$name] = static::$_types[$name]; } return static::$_builtTypes[$name] = static::$_types[$name]; @@ -144,9 +145,9 @@ public static function set($name, Type $instance) * If called with no arguments it will return current types map array * If $className is omitted it will return mapped class for $type * - * @param string|array|\Cake\Database\Type|null $type if string name of type to map, if array list of arrays to be mapped - * @param string|null $className The classname to register. - * @return array|string|null if $type is null then array with current map, if $className is null string + * @param string|string[]|\Cake\Database\Type[]|null $type if string name of type to map, if array list of arrays to be mapped + * @param string|\Cake\Database\Type|null $className The classname or object instance of it to register. + * @return array|string|null If $type is null then array with current map, if $className is null string * configured class name for give $type, null otherwise */ public static function map($type = null, $className = null) @@ -300,4 +301,19 @@ public function marshal($value) { return $this->_basicTypeCast($value); } + + /** + * Returns an array that can be used to describe the internal state of this + * object. + * + * @return array + */ + public function __debugInfo() + { + return [ + 'name' => $this->_name, + 'types' => static::$_types, + 'builtTypes' => static::$_builtTypes, + ]; + } } diff --git a/tests/TestCase/Database/TypeTest.php b/tests/TestCase/Database/TypeTest.php index db919dccbfe..1eadd87ab38 100644 --- a/tests/TestCase/Database/TypeTest.php +++ b/tests/TestCase/Database/TypeTest.php @@ -15,6 +15,9 @@ namespace Cake\Test\TestCase\Database; use Cake\Database\Type; +use Cake\Database\Type\BoolType; +use Cake\Database\Type\IntegerType; +use Cake\Database\Type\UuidType; use Cake\TestSuite\TestCase; use PDO; use TestApp\Database\Type\BarType; @@ -251,4 +254,33 @@ public function testSet() Type::set('random', $instance); $this->assertSame($instance, Type::build('random')); } + + /** + * @return void + */ + public function testDebugInfo() + { + $type = new Type('foo'); + Type::clear(); + Type::map('bool', BoolType::class); + Type::map('int', IntegerType::class); + $uuidType = new UuidType('uuid'); + Type::map('uuid', $uuidType); + Type::build('bool'); + + $result = $type->__debugInfo(); + $boolType = new BoolType('bool'); + $expected = [ + 'name' => 'foo', + 'types' => [ + 'bool' => $boolType, + 'int' => IntegerType::class, + 'uuid' => $uuidType, + ], + 'builtTypes' => [ + 'bool' => $boolType, + ], + ]; + $this->assertEquals($expected, $result); + } }