Skip to content
Permalink
Browse files

Fixed boolean casting and adding test for disabling casting

  • Loading branch information...
lorenzo committed Feb 24, 2018
1 parent 029cd96 commit e5b4c65b2128bfca3d16aa05302a216868f10f83
@@ -80,9 +80,10 @@ public function toDatabase($value, Driver $driver)
*/
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 @@ public function toPHP($value, Driver $driver)
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;
}
@@ -174,7 +174,7 @@ public function manyToPHP(array $values, array $fields, Driver $driver)
}
$instance = clone $this->_datetimeInstance;
$instance = $instance->modify($value);
$instance = $instance->modify($values[$field]);
if ($this->setToDateStart) {
$instance = $instance->setTime(0, 0, 0);
@@ -4482,6 +4482,29 @@ public function testCastResults()
$this->assertSame([['one' => 1, 'two' => 2, 'true' => true, 'three' => 3]], $results);
}
/**
* 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.
*
@@ -95,11 +95,13 @@ public function testToPHP()
$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 @@ public function testManyToPHP()
'd' => 'false',
'e' => 'FALSE',
'f' => '0',
'g' => '1'
'g' => '1',
'h' => true,
'i' => false,
];
$expected = [
'a' => null,
@@ -126,6 +130,8 @@ public function testManyToPHP()
'e' => false,
'f' => false,
'g' => true,
'h' => true,
'i' => false,
];
$this->assertEquals(
$expected,
@@ -62,6 +62,9 @@ public function testToPHP()
$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 @@ public function testManyToPHP()
'b' => '2.3',
'c' => '15',
'c' => '0.0',
'd' => 10
];
$expected = [
'a' => null,
'b' => 2,
'c' => 15,
'c' => 0,
'd' => 10
];
$this->assertEquals(
$expected,

0 comments on commit e5b4c65

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