Skip to content
Permalink
Browse files

More fixes

  • Loading branch information...
lorenzo committed Feb 24, 2018
1 parent e5b4c65 commit 721bbff353a6d80d808264842240466178aaac36
Showing with 45 additions and 38 deletions.
  1. +1 −0 src/Database/Query.php
  2. +44 −17 tests/TestCase/Database/QueryTest.php
  3. +0 −21 tests/TestCase/ORM/QueryTest.php
@@ -2015,6 +2015,7 @@ public function bufferResults($enable = null)
public function setSelectTypeMap(TypeMap $typeMap)
{
$this->_selectTypeMap = $typeMap;
$this->_dirty();
return $this;
}
@@ -28,7 +28,7 @@
class QueryTest extends TestCase
{
public $fixtures = ['core.articles', 'core.authors', 'core.comments'];
public $fixtures = ['core.articles', 'core.authors', 'core.comments', 'core.profiles'];
public $autoFixtures = false;
@@ -4467,19 +4467,23 @@ public function testGetValueBinder()
*/
public function testCastResults()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$typeMap = new TypeMap([
'one' => 'integer',
'two' => 'integer',
'three' => 'integer',
'true' => 'boolean'
]);
$fields = [
'id' => 'integer',
'user_id' => 'integer',
'is_active' => 'boolean'
];
$typeMap = new TypeMap($fields + ['a' => 'integer']);
$results = $query
->select(['one' => '1 * 1', 'two' => '1 * 2', 'true' => '1', 'three' => '1 + 2'])
->select(array_keys($fields))
->select(['a' => 'id'])
->from('profiles')
->setSelectTypeMap($typeMap)
->limit(1)
->execute()
->fetchAll('assoc');
$this->assertSame([['one' => 1, 'two' => 2, 'true' => true, 'three' => 3]], $results);
$this->assertSame([['id' => 1, 'user_id' => 1, 'is_active' => true, 'a' => 1]], $results);
}
/**
@@ -4489,20 +4493,43 @@ public function testCastResults()
*/
public function testCastResultsDisable()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$typeMap = new TypeMap([
'one' => 'integer',
'two' => 'integer',
'three' => 'integer',
'true' => 'boolean'
]);
$typeMap = new TypeMap(['a' => 'datetime']);
$results = $query
->select(['one' => '1 * 1', 'two' => '1 * 2', 'true' => '1 * 1', 'three' => '1 + 2'])
->select(['a' => 'id'])
->from('profiles')
->setSelectTypeMap($typeMap)
->limit(1)
->disableResultsCasting()
->execute()
->fetchAll('assoc');
$this->assertSame([['one' => '1', 'two' => '2', 'true' => '1', 'three' => '3']], $results);
$this->assertEquals([['a' => '1']], $results);
}
/**
* Test that type conversion is only applied once.
*
* @return void
*/
public function testAllNoDuplicateTypeCasting()
{
$query = new Query($this->connection);
$query
->select('1.5 AS a')
->setSelectTypeMap(new TypeMap(['a' => 'integer']));
// Convert to an array and make the query dirty again.
$result = $query->execute()->fetchAll('assoc');
$this->assertEquals([['a' => 1]], $result);
$query->setSelectTypeMap(new TypeMap(['a' => 'float']));
// Get results a second time.
$result = $query->execute()->fetchAll('assoc');
// Had the type casting being rememberd from the first time,
// The valuewould be a truncated float (1.0)
$this->assertEquals([['a' => 1.5]], $result);
}
/**
@@ -3548,25 +3548,4 @@ public function testNotMatchingNested()
];
$this->assertSame($expected, $results->first());
}
/**
* Test that type conversion is only applied once.
*
* @return void
*/
public function testAllNoDuplicateTypeCasting()
{
$table = $this->getTableLocator()->get('Comments');
$query = $table->find()
->select(['id', 'comment', 'created']);
// Convert to an array and make the query dirty again.
$result = $query->all()->toArray();
$query->limit(99);
// Get results a second time.
$result2 = $query->all()->toArray();
$this->assertEquals(1, $query->__debugInfo()['decorators'], 'Only one typecaster should exist');
}
}

0 comments on commit 721bbff

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