Skip to content

Commit

Permalink
[Session] Support MongoClient and Mongo connection classes
Browse files Browse the repository at this point in the history
This provides compatibility with pre-1.3.0 and newer PHP MongoDB drivers.
  • Loading branch information
jmikola committed Dec 13, 2012
1 parent 20e93bf commit b28af77
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Expand Up @@ -36,13 +36,18 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
/**
* Constructor.
*
* @param \Mongo $mongo A "Mongo" instance
* @param object $mongo A MongoClient or Mongo instance
* @param array $options An associative array of field options
*
* @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
* @throws \InvalidArgumentException When "database" or "collection" not provided
*/
public function __construct(\Mongo $mongo, array $options)
public function __construct($mongo, array $options)
{
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
throw new \InvalidArgumentException('MongoClient or Mongo instance required');
}

if (!isset($options['database']) || !isset($options['collection'])) {
throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
}
Expand Down
Expand Up @@ -27,11 +27,13 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
if (!class_exists('\Mongo')) {
$this->markTestSkipped('MongoDbSessionHandler requires the php "mongo" extension');
if (!extension_loaded('mongo')) {
$this->markTestSkipped('MongoDbSessionHandler requires the PHP "mongo" extension.');
}

$this->mongo = $this->getMockBuilder('Mongo')
$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? 'Mongo' : 'MongoClient';

$this->mongo = $this->getMockBuilder($mongoClass)
->disableOriginalConstructor()
->getMock();

Expand All @@ -46,6 +48,22 @@ protected function setUp()
$this->storage = new MongoDbSessionHandler($this->mongo, $this->options);
}

/**
* @expectedException InvalidArgumentException
*/
public function testConstructorShouldThrowExceptionForInvalidMongo()
{
new MongoDbSessionHandler(new \stdClass(), $this->options);
}

/**
* @expectedException InvalidArgumentException
*/
public function testConstructorShouldThrowExceptionForMissingOptions()
{
new MongoDbSessionHandler($this->mongo, array());
}

public function testOpenMethodAlwaysReturnTrue()
{
$this->assertTrue($this->storage->open('test', 'test'), 'The "open" method should always return true');
Expand Down

0 comments on commit b28af77

Please sign in to comment.