Browse Source

Merge pull request #10127 from cakephp/issue-10111

Adds test for caching result set with file engine
José Lorenzo Rodríguez 9 years ago
parent
commit
b2b7f76cd0
3 changed files with 33 additions and 4 deletions
  1. 19 3
      src/ORM/ResultSet.php
  2. 14 0
      tests/TestCase/ORM/EntityTest.php
  3. 0 1
      tests/TestCase/ORM/ResultSetTest.php

+ 19 - 3
src/ORM/ResultSet.php

@@ -313,10 +313,19 @@ class ResultSet implements ResultSetInterface
      */
     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);
     }
 
@@ -330,9 +339,10 @@ class ResultSet implements ResultSetInterface
      */
     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();
     }
 
     /**
@@ -351,7 +361,13 @@ class ResultSet implements ResultSetInterface
             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;
     }
 
     /**

+ 14 - 0
tests/TestCase/ORM/EntityTest.php

@@ -713,6 +713,20 @@ class EntityTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return void

+ 0 - 1
tests/TestCase/ORM/ResultSetTest.php

@@ -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;