Skip to content

Commit

Permalink
Ensure no MongoDB driver classes are serialized
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Feb 14, 2016
1 parent 601aa3a commit d4a6583
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ counterparts in `ext-mongo`. Do not rely on exception messages being the same.
Methods that return a result array containing a `connectionId` field will always
return `0` as connection ID.

## Serialization of objects
Serialization of any Mongo* objects (e.g. MongoGridFSFile, MongoCursor, etc.)
will not work properly. The objects can be serialized but are not usable after
unserializing them.

## Mongo

- The Mongo class is deprecated and was not implemented in this library. If you
Expand Down
8 changes: 8 additions & 0 deletions lib/Alcaeus/MongoDbAdapter/AbstractCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,12 @@ protected function reset()
$this->cursor = null;
$this->iterator = null;
}

/**
* @return array
*/
public function __sleep()
{
return ['batchSize', 'connection', 'iterator', 'ns', 'optionNames', 'position', 'startedIterating'];
}
}
10 changes: 10 additions & 0 deletions lib/Mongo/MongoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,15 @@ private function notImplemented()
{
throw new \Exception('Not implemented');
}

/**
* @return array
*/
function __sleep()
{
return [
'connected', 'status', 'server', 'persistent'
];
}
}

8 changes: 8 additions & 0 deletions lib/Mongo/MongoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -963,5 +963,13 @@ private function checkCollectionName($name)
throw new Exception('Collection name cannot contain null bytes');
}
}

/**
* @return array
*/
public function __sleep()
{
return ['db', 'name'];
}
}

8 changes: 8 additions & 0 deletions lib/Mongo/MongoCommandCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,12 @@ protected function getIterationInfo()

return $iterationInfo;
}

/**
* @return array
*/
public function __sleep()
{
return ['command'] + parent::__sleep();
}
}
24 changes: 24 additions & 0 deletions lib/Mongo/MongoCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,28 @@ protected function getCursorInfo()
'fields' => $this->projection,
];
}

/**
* @return array
*/
public function __sleep()
{
return [
'allowPartialResults',
'awaitData',
'flags',
'hint',
'limit',
'maxTimeMS',
'noCursorTimeout',
'optionNames',
'options',
'projection',
'query',
'skip',
'snapshot',
'sort',
'tailable',
] + parent::__sleep();
}
}
8 changes: 8 additions & 0 deletions lib/Mongo/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,12 @@ private function getSystemCollectionFilterClosure($includeSystemCollections = fa
return $includeSystemCollections || ! preg_match('#^system\.#', $collectionInfo->getName());
};
}

/**
* @return array
*/
public function __sleep()
{
return ['connection', 'name'];
}
}
8 changes: 8 additions & 0 deletions lib/Mongo/MongoGridFS.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,12 @@ private function isOKResult($result)
return (is_array($result) && $result['ok'] == 1.0) ||
(is_bool($result) && $result);
}

/**
* @return array
*/
public function __sleep()
{
return ['chunks', 'chunksName', 'database', 'defaultChunkSize', 'filesName', 'prefix'] + parent::__sleep();
}
}
5 changes: 5 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
class MongoClientTest extends TestCase
{
public function testSerialize()
{
$this->assertInternalType('string', serialize($this->getClient()));
}

public function testGetDb()
{
$client = $this->getClient();
Expand Down
5 changes: 5 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
class MongoCollectionTest extends TestCase
{
public function testSerialize()
{
$this->assertInternalType('string', serialize($this->getCollection()));
}

public function testGetNestedCollections()
{
$collection = $this->getCollection()->foo->bar;
Expand Down
7 changes: 7 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCommandCursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
*/
class MongoCommandCursorTest extends TestCase
{
public function testSerialize()
{
$this->prepareData();
$cursor = $this->getCollection()->aggregateCursor([['$match' => ['foo' => 'bar']]]);
$this->assertInternalType('string', serialize($cursor));
}

public function testInfo()
{
$this->prepareData();
Expand Down
7 changes: 7 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoCursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class MongoCursorTest extends TestCase
{
public function testSerialize()
{
$this->prepareData();
$cursor = $this->getCollection()->find(['foo' => 'bar']);
$this->assertInternalType('string', serialize($cursor));
}

public function testCursorConvertsTypes()
{
$this->prepareData();
Expand Down
5 changes: 5 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
class MongoDBTest extends TestCase
{
public function testSerialize()
{
$this->assertInternalType('string', serialize($this->getDatabase()));
}

public function testEmptyDatabaseName()
{
$this->setExpectedException('Exception', 'Database name cannot be empty');
Expand Down
6 changes: 6 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoDeleteBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

class MongoDeleteBatchTest extends TestCase
{
public function testSerialize()
{
$batch = new \MongoDeleteBatch($this->getCollection());
$this->assertInternalType('string', serialize($batch));
}

public function testDeleteOne()
{
$collection = $this->getCollection();
Expand Down
10 changes: 10 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSCursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class MongoGridFSCursorTest extends TestCase
{
public function testSerialize()
{
$gridfs = $this->getGridFS();
$gridfs->storeBytes('foo', ['filename' => 'foo.txt']);
$gridfs->storeBytes('bar', ['filename' => 'bar.txt']);
$cursor = $gridfs->find(['filename' => 'foo.txt']);

$this->assertInternalType('string', serialize($cursor));
}

public function testCursorItems()
{
$gridfs = $this->getGridFS();
Expand Down
9 changes: 9 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

class MongoGridFSFileTest extends TestCase
{
public function testSerialize()
{
$this->prepareFile('abcd', ['filename' => 'foo']);
$file = $this->getGridFS()->findOne(['filename' => 'foo']);
$this->assertInstanceOf(\MongoGridFSFile::class, $file);

$this->assertInternalType('string', serialize($file));
}

public function testFileProperty()
{
$file = $this->getFile();
Expand Down
5 changes: 5 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoGridFSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class MongoGridFSTest extends TestCase
{
public function testSerialize()
{
$this->assertInternalType('string', serialize($this->getGridFS()));
}

public function testChunkProperty()
{
$collection = $this->getGridFS();
Expand Down
6 changes: 6 additions & 0 deletions tests/Alcaeus/MongoDbAdapter/Mongo/MongoInsertBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

class MongoInsertBatchTest extends TestCase
{
public function testSerialize()
{
$batch = new \MongoInsertBatch($this->getCollection());
$this->assertInternalType('string', serialize($batch));
}

public function testInsertBatch()
{
$batch = new \MongoInsertBatch($this->getCollection());
Expand Down
6 changes: 5 additions & 1 deletion tests/Alcaeus/MongoDbAdapter/Mongo/MongoUpdateBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

class MongoUpdateBatchTest extends TestCase
{

public function testSerialize()
{
$batch = new \MongoUpdateBatch($this->getCollection());
$this->assertInternalType('string', serialize($batch));
}

public function testUpdateOne()
{
Expand Down

0 comments on commit d4a6583

Please sign in to comment.