From 91c2be2a843cf2d932da6505a1be576676af5d1f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 13 Oct 2015 14:26:41 +0200 Subject: [PATCH] Result: normalize always converts type TEXT to string --- src/Dibi/Result.php | 3 +- tests/dibi/Result.normalize.phpt | 59 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 tests/dibi/Result.normalize.phpt diff --git a/src/Dibi/Result.php b/src/Dibi/Result.php index c60ae832..66357f2c 100644 --- a/src/Dibi/Result.php +++ b/src/Dibi/Result.php @@ -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; diff --git a/tests/dibi/Result.normalize.phpt b/tests/dibi/Result.normalize.phpt new file mode 100644 index 00000000..3b71b193 --- /dev/null +++ b/tests/dibi/Result.normalize.phpt @@ -0,0 +1,59 @@ +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])); +});