Skip to content
Permalink
Browse files

Add DebugInfo and small performance cache.

  • Loading branch information...
dereuromark committed Dec 2, 2017
1 parent 1785901 commit 5a5d2b78d07d67e0034678fe3729b06d0ec95d16
Showing with 52 additions and 4 deletions.
  1. +20 −4 src/Database/Type.php
  2. +32 −0 tests/TestCase/Database/TypeTest.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,
];
}
}
@@ -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);
}
}

0 comments on commit 5a5d2b7

Please sign in to comment.
You can’t perform that action at this time.