Skip to content

Commit

Permalink
Merge pull request #10127 from cakephp/issue-10111
Browse files Browse the repository at this point in the history
Adds test for caching result set with file engine
  • Loading branch information
lorenzo committed Jan 31, 2017
2 parents 5705b58 + aee51a1 commit b2b7f76
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/ORM/ResultSet.php
Expand Up @@ -313,10 +313,19 @@ public function first()
*/
public function serialize()
{
if (!$this->_useBuffering) {
$msg = 'You cannot serialize an un-buffered ResultSet. Use Query::bufferResults() to get a buffered ResultSet.';
throw new Exception($msg);
}

while ($this->valid()) {
$this->next();
}

if ($this->_results instanceof SplFixedArray) {
return serialize($this->_results->toArray());
}

return serialize($this->_results);
}

Expand All @@ -330,9 +339,10 @@ public function serialize()
*/
public function unserialize($serialized)
{
$this->_results = unserialize($serialized);
$results = (array)(unserialize($serialized) ?: []);
$this->_results = SplFixedArray::fromArray($results);
$this->_useBuffering = true;
$this->_count = count($this->_results);
$this->_count = $this->_results->count();
}

/**
Expand All @@ -351,7 +361,13 @@ public function count()
return $this->_count = $this->_statement->rowCount();
}

return $this->_count = count($this->_results);
if ($this->_results instanceof SplFixedArray) {
$this->_count = $this->_results->count();
} else {
$this->_count = count($this->_results);
}

return $this->_count;
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -712,6 +712,20 @@ public function testJsonSerialize()
$this->assertEquals(json_encode($data), json_encode($entity));
}

/**
* Tests serializing an entity as PHP
*
* @return void
*/
public function testPhpSerialize()
{
$data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
$entity = new Entity($data);
$copy = unserialize(serialize($entity));
$this->assertInstanceOf(Entity::class, $copy);
$this->assertEquals($data, $copy->toArray());
}

/**
* Tests that jsonSerialize is called recursively for contained entities
*
Expand Down
1 change: 0 additions & 1 deletion tests/TestCase/ORM/ResultSetTest.php
Expand Up @@ -14,7 +14,6 @@
*/
namespace Cake\Test\TestCase\ORM;

use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\Entity;
Expand Down

0 comments on commit b2b7f76

Please sign in to comment.