Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 24, 2018
1 parent e5b4c65 commit 721bbff
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/Database/Query.php
Expand Up @@ -2015,6 +2015,7 @@ public function bufferResults($enable = null)
public function setSelectTypeMap(TypeMap $typeMap)
{
$this->_selectTypeMap = $typeMap;
$this->_dirty();

return $this;
}
Expand Down
61 changes: 44 additions & 17 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
21 changes: 0 additions & 21 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -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.