8000 [Session] Support MongoClient and Mongo connection classes · symfony/symfony@b28af77 · GitHub
[go: up one dir, main page]

Skip to content

Commit b28af77

Browse files
committed
[Session] Support MongoClient and Mongo connection classes
This provides compatibility with pre-1.3.0 and newer PHP MongoDB drivers.
1 parent 20e93bf commit b28af77

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
3636
/**
3737
* Constructor.
3838
*
39-
* @param \Mongo $mongo A "Mongo" instance
39+
* @param object $mongo A MongoClient or Mongo instance
4040
* @param array $options An associative array of field options
4141
*
42+
* @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
4243
* @throws \InvalidArgumentException When "database" or "collection" not provided
4344
*/
44-
public function __construct(\Mongo $mongo, array $options)
45+
public function __construct($mongo, array $options)
4546
{
47+
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
48+
throw new \InvalidArgumentException('MongoClient or Mongo instance required');
49+
}
50+
4651
if (!isset($options['database']) || !isset($options['collection'])) {
4752
throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
4853
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
2727

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

34-
$this->mongo = $this->getMockBuilder('Mongo')
34+
$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? 'Mongo' : 'MongoClient';
35+
36+
$this->mongo = $this->getMockBuilder($mongoClass)
3537
->disableOriginalConstructor()
3638
->getMock();
3739

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

51+
/**
52+
* @expectedException InvalidArgumentException
53+
*/
54+
public function testConstructorShouldThrowExceptionForInvalidMongo()
55+
{
56+
new MongoDbSessionHandler(new \stdClass(), $this->options);
57+
}
58+
59+
/**
60+
* @expectedException InvalidArgumentException
61+
*/
62+
public function testConstructorShouldThrowExceptionForMissingOptions()
63+
{
64+
new MongoDbSessionHandler($this->mongo, array());
65+
}
66+
4967
public function testOpenMethodAlwaysReturnTrue()
5068
{
5169
$this->assertTrue($this->storage->open('test', 'test'), 'The "open" method should always return true');

0 commit comments

Comments
 (0)
0