Browse Source

Fixed boolean casting and adding test for disabling casting

Jose Lorenzo Rodriguez 8 years ago
parent
commit
e5b4c65b21

+ 4 - 3
src/Database/Type/BoolType.php

@@ -80,9 +80,10 @@ class BoolType extends Type implements TypeInterface, BatchCastingInterface
      */
     public function toPHP($value, Driver $driver)
     {
-        if ($value === null) {
-            return null;
+        if ($value === null || $value === true || $value === false) {
+            return $value;
         }
+
         if (!is_numeric($value)) {
             return strtolower($value) === 'true';
         }
@@ -98,7 +99,7 @@ class BoolType extends Type implements TypeInterface, BatchCastingInterface
     public function manyToPHP(array $values, array $fields, Driver $driver)
     {
         foreach ($fields as $field) {
-            if (!isset($values[$field])) {
+            if (!isset($values[$field]) || $values[$field] === true || $values[$field] === false) {
                 continue;
             }
 

+ 1 - 1
src/Database/Type/DateTimeType.php

@@ -174,7 +174,7 @@ class DateTimeType extends Type implements TypeInterface, BatchCastingInterface
             }
 
             $instance = clone $this->_datetimeInstance;
-            $instance = $instance->modify($value);
+            $instance = $instance->modify($values[$field]);
 
             if ($this->setToDateStart) {
                 $instance = $instance->setTime(0, 0, 0);

+ 23 - 0
tests/TestCase/Database/QueryTest.php

@@ -4483,6 +4483,29 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Test disabling type casting
+     *
+     * @return void
+     */
+    public function testCastResultsDisable()
+    {
+        $query = new Query($this->connection);
+        $typeMap = new TypeMap([
+            'one' => 'integer',
+            'two' => 'integer',
+            'three' => 'integer',
+            'true' => 'boolean'
+        ]);
+        $results = $query
+            ->select(['one' => '1 * 1', 'two' => '1 * 2', 'true' => '1 * 1', 'three' => '1 + 2'])
+            ->setSelectTypeMap($typeMap)
+            ->disableResultsCasting()
+            ->execute()
+            ->fetchAll('assoc');
+        $this->assertSame([['one' => '1', 'two' => '2', 'true' => '1', 'three' => '3']], $results);
+    }
+
+    /**
      * Test that reading an undefined clause does not emit an error.
      *
      * @return void

+ 7 - 1
tests/TestCase/Database/Type/BoolTypeTest.php

@@ -95,11 +95,13 @@ class BoolTypeTest extends TestCase
         $this->assertTrue($this->type->toPHP('1', $this->driver));
         $this->assertTrue($this->type->toPHP('TRUE', $this->driver));
         $this->assertTrue($this->type->toPHP('true', $this->driver));
+        $this->assertTrue($this->type->toPHP(true, $this->driver));
 
         $this->assertFalse($this->type->toPHP(0, $this->driver));
         $this->assertFalse($this->type->toPHP('0', $this->driver));
         $this->assertFalse($this->type->toPHP('FALSE', $this->driver));
         $this->assertFalse($this->type->toPHP('false', $this->driver));
+        $this->assertFalse($this->type->toPHP(false, $this->driver));
     }
 
     /**
@@ -116,7 +118,9 @@ class BoolTypeTest extends TestCase
             'd' => 'false',
             'e' => 'FALSE',
             'f' => '0',
-            'g' => '1'
+            'g' => '1',
+            'h' => true,
+            'i' => false,
         ];
         $expected = [
             'a' => null,
@@ -126,6 +130,8 @@ class BoolTypeTest extends TestCase
             'e' => false,
             'f' => false,
             'g' => true,
+            'h' => true,
+            'i' => false,
         ];
         $this->assertEquals(
             $expected,

+ 5 - 0
tests/TestCase/Database/Type/IntegerTypeTest.php

@@ -62,6 +62,9 @@ class IntegerTypeTest extends TestCase
 
         $result = $this->type->toPHP('-2', $this->driver);
         $this->assertSame(-2, $result);
+
+        $result = $this->type->toPHP(10, $this->driver);
+        $this->assertSame(10, $result);
     }
 
     /**
@@ -76,12 +79,14 @@ class IntegerTypeTest extends TestCase
             'b' => '2.3',
             'c' => '15',
             'c' => '0.0',
+            'd' => 10
         ];
         $expected = [
             'a' => null,
             'b' => 2,
             'c' => 15,
             'c' => 0,
+            'd' => 10
         ];
         $this->assertEquals(
             $expected,