Andreas Braun 10 лет назад
Родитель
Сommit
90e25e6976
2 измененных файлов с 26 добавлено и 39 удалено
  1. 9 18
      lib/Mongo/MongoGridFSCursor.php
  2. 17 21
      tests/Alcaeus/MongoDbAdapter/MongoGridFSCursorTest.php

+ 9 - 18
lib/Mongo/MongoGridFSCursor.php

@@ -13,7 +13,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-class MongoGridFSCursor extends MongoCursor implements Traversable, Iterator
+class MongoGridFSCursor extends MongoCursor
 {
     /**
      * @static
@@ -29,51 +29,42 @@ class MongoGridFSCursor extends MongoCursor implements Traversable, Iterator
 
     /**
      * Create a new cursor
+     *
      * @link http://php.net/manual/en/mongogridfscursor.construct.php
      * @param MongoGridFS $gridfs Related GridFS collection
-     * @param resource $connection Database connection
+     * @param MongoClient $connection Database connection
      * @param string $ns Full name of database and collection
      * @param array $query Database query
      * @param array $fields Fields to return
      * @return MongoGridFSCursor Returns the new cursor
      */
-    public function __construct(MongoGridFS $gridfs, $connection, $ns, array $query = array(), array $fields = array())
+    public function __construct(MongoGridFS $gridfs, MongoClient $connection, $ns, array $query = array(), array $fields = array())
     {
         $this->gridfs = $gridfs;
         parent::__construct($connection, $ns, $query, $fields);
     }
 
     /**
-     * Return the next file to which this cursor points, and advance the cursor
-     * @link http://php.net/manual/en/mongogridfscursor.getnext.php
-     * @return MongoGridFSFile Returns the next file
-     */
-    public function getNext()
-    {
-        $file = parent::next();
-        return new MongoGridFSFile($this->gridfs, $file);
-    }
-
-    /**
      * Returns the current file
+     *
      * @link http://php.net/manual/en/mongogridfscursor.current.php
      * @return MongoGridFSFile The current file
      */
     public function current()
     {
         $file = parent::current();
-        return new MongoGridFSFile($this->gridfs, $file);
+        return ($file !== null) ? new MongoGridFSFile($this->gridfs, $file) : null;
     }
 
     /**
      * Returns the current result's filename
+     *
      * @link http://php.net/manual/en/mongogridfscursor.key.php
      * @return string The current results filename
      */
     public function key()
     {
-        $file = parent::current();
-        return isset($file['filename']) ? $file['filename'] : null;
+        $file = $this->current();
+        return ($file !== null) ? $file->getFilename() : null;
     }
-
 }

+ 17 - 21
tests/Alcaeus/MongoDbAdapter/MongoGridFSCursorTest.php

@@ -4,29 +4,25 @@ namespace Alcaeus\MongoDbAdapter\Tests;
 
 class MongoGridFSCursorTest extends TestCase
 {
-    public function testCursor()
+    public function testCursorItems()
     {
-        $cursor = $this->getCursor();
-        $array = iterator_to_array($cursor);
+        $gridfs = $this->getGridFS();
+        $gridfs->storeBytes('foo', ['filename' => 'foo.txt']);
+        $gridfs->storeBytes('bar', ['filename' => 'bar.txt']);
 
+        $cursor = $gridfs->find(['filename' => 'foo.txt']);
+        $this->assertCount(1, $cursor);
+        foreach ($cursor as $key => $value) {
+            $this->assertSame('foo.txt', $key);
+            $this->assertInstanceOf('MongoGridFSFile', $value);
+            $this->assertSame('foo', $value->getBytes());
 
-        $this->assertCount(2, $array);
-        $this->assertArrayHasKey('One.txt', $array);
-        $this->assertArrayHasKey('Two.txt', $array);
-        $firstFile = $array['One.txt'];
-        $this->assertInstanceOf('MongoGridFSFile', $firstFile);
-        $this->assertArraySubset(['length' => 3, 'filename' => 'One.txt'], $firstFile->file);
-        $secondFile = $array['Two.txt'];
-        $this->assertInstanceOf('MongoGridFSFile', $secondFile);
-        $this->assertArraySubset(['length' => 3, 'filename' => 'Two.txt'], $secondFile->file);
-    }
-
-    private function getCursor()
-    {
-        $gridFS = $this->getGridFS();
-        $gridFS->storeBytes('One', ['filename' => 'One.txt']);
-        $gridFS->storeBytes('Two', ['filename' => 'Two.txt']);
-
-        return $gridFS->find();
+            $this->assertArraySubset([
+                'filename' => 'foo.txt',
+                'chunkSize' => \MongoGridFS::DEFAULT_CHUNK_SIZE,
+                'length' => 3,
+                'md5' => 'acbd18db4cc2f85cedef654fccc4a4d8'
+            ], $value->file);
+        }
     }
 }