1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-09 07:37:08 +02:00

Result: normalize always converts type TEXT to string

This commit is contained in:
David Grudl
2015-10-13 14:26:41 +02:00
parent 6f273ef601
commit 91c2be2a84
2 changed files with 61 additions and 1 deletions

View File

@@ -490,7 +490,8 @@ class Result implements IDataSource
continue;
}
$value = $row[$key];
if ($value === FALSE || $type === Type::TEXT) {
if ($type === Type::TEXT) {
$row[$key] = (string) $value;
} elseif ($type === Type::INTEGER) {
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;

View File

@@ -0,0 +1,59 @@
<?php
use Tester\Assert;
use Dibi\Type;
require __DIR__ . '/bootstrap.php';
class MockResult extends Dibi\Result
{
function __construct()
{}
function test($row)
{
$normalize = new ReflectionMethod('Dibi\Result', 'normalize');
$normalize->setAccessible(TRUE);
$normalize->invokeArgs($this, [& $row]);
return $row;
}
}
test(function () {
$result = new MockResult;
$result->setType('col', Type::BOOL);
Assert::same(['col' => NULL], $result->test(['col' => NULL]));
Assert::same(['col' => TRUE], $result->test(['col' => TRUE]));
Assert::same(['col' => FALSE], $result->test(['col' => FALSE]));
Assert::same(['col' => FALSE], $result->test(['col' => '']));
Assert::same(['col' => FALSE], $result->test(['col' => '0']));
Assert::same(['col' => TRUE], $result->test(['col' => '1']));
Assert::same(['col' => TRUE], $result->test(['col' => 't']));
Assert::same(['col' => FALSE], $result->test(['col' => 'f']));
Assert::same(['col' => TRUE], $result->test(['col' => 'T']));
Assert::same(['col' => FALSE], $result->test(['col' => 'F']));
Assert::same(['col' => FALSE], $result->test(['col' => 0]));
Assert::same(['col' => FALSE], $result->test(['col' => 0.0]));
Assert::same(['col' => TRUE], $result->test(['col' => 1]));
Assert::same(['col' => TRUE], $result->test(['col' => 1.0]));
});
test(function () {
$result = new MockResult;
$result->setType('col', Type::TEXT);
Assert::same(['col' => NULL], $result->test(['col' => NULL]));
Assert::same(['col' => '1'], $result->test(['col' => TRUE]));
Assert::same(['col' => ''], $result->test(['col' => FALSE]));
Assert::same(['col' => ''], $result->test(['col' => '']));
Assert::same(['col' => '0'], $result->test(['col' => '0']));
Assert::same(['col' => '1'], $result->test(['col' => '1']));
Assert::same(['col' => '0'], $result->test(['col' => 0]));
Assert::same(['col' => '1'], $result->test(['col' => 1]));
});