[Session] Support MongoClient and Mongo connection classes

This provides compatibility with pre-1.3.0 and newer PHP MongoDB drivers.
This commit is contained in:
Jeremy Mikola 2012-12-13 15:48:37 -05:00
parent 20e93bf1a4
commit b28af77101
2 changed files with 28 additions and 5 deletions

View File

@ -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');
}

View File

@ -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();
@ -46,6 +48,22 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
$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');