Skip to content

Commit

Permalink
Fixing various faling tests. The problem was not closing the statemen…
Browse files Browse the repository at this point in the history
…ts, among other things
  • Loading branch information
lorenzo committed Feb 12, 2014
1 parent 645fd49 commit 70e3ef0
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Database/Expression/TupleComparison.php
Expand Up @@ -88,7 +88,8 @@ protected function _stringifyValues($generator) {
$type = $this->_type; $type = $this->_type;
$multiType = is_array($type); $multiType = is_array($type);
$isMulti = $this->isMulti($i, $type); $isMulti = $this->isMulti($i, $type);
$type = $isMulti ? str_replace('[]', '', $type) : $type; $type = $multiType ? $type : str_replace('[]', '', $type);
$type = $type ?: null;


if ($isMulti) { if ($isMulti) {
$bound = []; $bound = [];
Expand Down
1 change: 1 addition & 0 deletions src/ORM/Association/ExternalAssociationTrait.php
Expand Up @@ -313,6 +313,7 @@ protected function _linkField($options) {
*/ */
protected function _buildSubquery($query, $foreignKey) { protected function _buildSubquery($query, $foreignKey) {
$filterQuery = clone $query; $filterQuery = clone $query;
$filterQuery->limit(null);
$filterQuery->contain([], true); $filterQuery->contain([], true);
$joins = $filterQuery->join(); $joins = $filterQuery->join();
foreach ($joins as $i => $join) { foreach ($joins as $i => $join) {
Expand Down
3 changes: 1 addition & 2 deletions src/ORM/Query.php
Expand Up @@ -780,8 +780,7 @@ public function first() {
if ($this->_dirty) { if ($this->_dirty) {
$this->limit(1); $this->limit(1);
} }
$this->_results = $this->all(); return $this->all()->first();
return $this->_results->first();
} }


/** /**
Expand Down
21 changes: 18 additions & 3 deletions src/ORM/ResultSet.php
Expand Up @@ -113,6 +113,13 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
*/ */
protected $_useBuffering = true; protected $_useBuffering = true;


/**
* Holds the count of records in this result set
*
* @var integer
*/
protected $_count;

/** /**
* Constructor * Constructor
* *
Expand All @@ -128,6 +135,10 @@ public function __construct($query, $statement) {
$this->_hydrate = $this->_query->hydrate(); $this->_hydrate = $this->_query->hydrate();
$this->_entityClass = $query->repository()->entityClass(); $this->_entityClass = $query->repository()->entityClass();
$this->_useBuffering = $query->bufferResults(); $this->_useBuffering = $query->bufferResults();

if ($statement) {
$this->count();
}
} }


/** /**
Expand Down Expand Up @@ -199,8 +210,9 @@ public function valid() {


$this->_current = $this->_fetchResult(); $this->_current = $this->_fetchResult();
$valid = $this->_current !== false; $valid = $this->_current !== false;
$hasNext = $this->_index < $this->_count;


if (!$valid && $this->_statement) { if ($this->_statement && !($valid && $hasNext)) {
$this->_statement->closeCursor(); $this->_statement->closeCursor();
} }


Expand Down Expand Up @@ -271,10 +283,13 @@ public function first() {
* @return integer * @return integer
*/ */
public function count() { public function count() {
if ($this->_count !== null) {
return $this->_count;
}
if ($this->_statement) { if ($this->_statement) {
return $this->_statement->rowCount(); return $this->_count = $this->_statement->rowCount();
} }
return count($this->_results); return $this->_count = count($this->_results);

This comment has been minimized.

Copy link
@markstory

markstory Feb 13, 2014

Member

I'm guessing these are workarounds for PDO being sad?

This comment has been minimized.

Copy link
@lorenzo

lorenzo Feb 13, 2014

Author Member

This was part of the changes for fixing TranslateBehavior, and yes, it is just to please PDO :P

This comment has been minimized.

Copy link
@markstory

markstory Feb 13, 2014

Member

Oh PDO u so crazy.

} }


/** /**
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -426,6 +426,7 @@ public function testFindSingleLocaleBelongsto() {


$results = $table->find() $results = $table->find()
->select(['title', 'body']) ->select(['title', 'body'])
->order(['title' => 'asc'])
->contain(['Authors' => function($q) { ->contain(['Authors' => function($q) {
return $q->select(['id', 'name']); return $q->select(['id', 'name']);
}]); }]);
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/ORM/QueryTest.php
Expand Up @@ -885,7 +885,10 @@ public function testResultsAreWrappedInMapReduce() {
$params = [$this->connection, $this->table]; $params = [$this->connection, $this->table];
$query = $this->getMock('\Cake\ORM\Query', ['execute'], $params); $query = $this->getMock('\Cake\ORM\Query', ['execute'], $params);


$statement = $this->getMock('\Database\StatementInterface', ['fetch', 'closeCursor']); $statement = $this->getMock(
'\Database\StatementInterface',
['fetch', 'closeCursor', 'rowCount']
);
$statement->expects($this->exactly(3)) $statement->expects($this->exactly(3))
->method('fetch') ->method('fetch')
->will($this->onConsecutiveCalls(['a' => 1], ['a' => 2], false)); ->will($this->onConsecutiveCalls(['a' => 1], ['a' => 2], false));
Expand Down

0 comments on commit 70e3ef0

Please sign in to comment.