Skip to content

Commit c6bd807

Browse files
committed
merged branch jmikola/2.1-mongoclient (PR #6338)
This PR was merged into the 2.1 branch. Commits ------- f24e3d7 [HttpKernel] Revise MongoDbProfilerStorage::write() return value 78c5273 [Session] Document Mongo|MongoClient argument type instead of "object" de19a81 [HttpKernel] Support MongoClient and Mongo connection classes b28af77 [Session] Support MongoClient and Mongo connection classes 20e93bf [Session] Utilize MongoDB::selectCollection() Discussion ---------- [2.1] Support PHP MongoDB driver 1.3.0+ in profiler/session classes > Bug fix: yes > Feature addition: yes > Backwards compatibility break: no > Symfony2 tests pass: yes > Fixes the following tickets: #6130 > License of the code: MIT I don't believe this is a BC break, but just to confirm: the MongoDbSessionHandler constructor signature changed, as I removed the `Mongo` type hint. Bug fix entails allowing MongoClient to be used by default if the new driver is available, since the original Mongo class is deprecated. I also removed the assumption about `MongoCollection::update()`'s return value, since it may be a status array now. --------------------------------------------------------------------------- by jmikola at 2012-12-13T22:04:47Z Lots of test failures, but they appear to be unrelated.
2 parents 422ad83 + f24e3d7 commit c6bd807

File tree

3 files changed

+42
-50
lines changed

3 files changed

+42
-50
lines changed

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

Lines changed: 9 additions & 4 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
40-
* @param array $options An associative array of field options
39+
* @param \Mongo|\MongoClient $mongo A MongoClient or Mongo instance
40+
* @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
}
@@ -137,7 +142,7 @@ public function read($sessionId)
137142
private function getCollection()
138143
{
139144
if (null === $this->collection) {
140-
$this->collection = $this->mongo->selectDB($this->options['database'])->selectCollection($this->options['collection']);
145+
$this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
141146
}
142147

143148
return $this->collection;

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

Lines changed: 25 additions & 43 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');
@@ -58,22 +76,13 @@ public function testCloseMethodAlwaysReturnTrue()
5876

5977
public function testWrite()
6078
{
61-
$database = $this->getMockBuilder('MongoDB')
62-
->disableOriginalConstructor()
63-
->getMock();
64-
6579
$collection = $this->getMockBuilder('MongoCollection')
6680
->disableOriginalConstructor()
6781
->getMock();
6882

6983
$this->mongo->expects($this->once())
70-
->method('selectDB')
71-
->with($this->options['database'])
72-
->will($this->returnValue($database));
73-
74-
$database->expects($this->once())
7584
->method('selectCollection')
76-
->with($this->options['collection'])
85+
->with($this->options['database'], $this->options['collection'])
7786
->will($this->returnValue($collection));
7887

7988
$that = $this;
@@ -96,22 +105,13 @@ public function testWrite()
96105

97106
public function testReplaceSessionData()
98107
{
99-
$database = $this->getMockBuilder('MongoDB')
100-
->disableOriginalConstructor()
101-
->getMock();
102-
103108
$collection = $this->getMockBuilder('MongoCollection')
104109
->disableOriginalConstructor()
105110
->getMock();
106111

107112
$this->mongo->expects($this->once())
108-
->method('selectDB')
109-
->with($this->options['database'])
110-
->will($this->returnValue($database));
111-
112-
$database->expects($this->once())
113113
->method('selectCollection')
114-
->with($this->options['collection'])
114+
->with($this->options['database'], $this->options['collection'])
115115
->will($this->returnValue($collection));
116116

117117
$data = array();
@@ -130,22 +130,13 @@ public function testReplaceSessionData()
130130

131131
public function testDestroy()
132132
{
133-
$database = $this->getMockBuilder('MongoDB')
134-
->disableOriginalConstructor()
135-
->getMock();
136-
137133
$collection = $this->getMockBuilder('MongoCollection')
138134
->disableOriginalConstructor()
139135
->getMock();
140136

141137
$this->mongo->expects($this->once())
142-
->method('selectDB')
143-
->with($this->options['database'])
144-
->will($this->returnValue($database));
145-
146-
$database->expects($this->once())
147138
->method('selectCollection')
148-
->with($this->options['collection'])
139+
->with($this->options['database'], $this->options['collection'])
149140
->will($this->returnValue($collection));
150141

151142
$collection->expects($this->once())
@@ -160,22 +151,13 @@ public function testDestroy()
160151

161152
public function testGc()
162153
{
163-
$database = $this->getMockBuilder('MongoDB')
164-
->disableOriginalConstructor()
165-
->getMock();
166-
167154
$collection = $this->getMockBuilder('MongoCollection')
168155
->disableOriginalConstructor()
169156
->getMock();
170157

171158
$this->mongo->expects($this->once())
172-
->method('selectDB')
173-
->with($this->options['database'])
174-
->will($this->returnValue($database));
175-
176-
$database->expects($this->once())
177159
->method('selectCollection')
178-
->with($this->options['collection'])
160+
->with($this->options['database'], $this->options['collection'])
179161
->will($this->returnValue($collection));
180162

181163
$that = $this;

src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public function write(Profile $profile)
102102
'time' => $profile->getTime()
103103
);
104104

105-
return $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
105+
$result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
106+
107+
return (boolean) (isset($result['ok']) ? $result['ok'] : $result);
106108
}
107109

108110
/**
@@ -114,12 +116,15 @@ protected function getMongo()
114116
{
115117
if ($this->mongo === null) {
116118
if (preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $this->dsn, $matches)) {
117-
$mongo = new \Mongo($matches[1] . (!empty($matches[2]) ? '/' . $matches[2] : ''));
119+
$server = $matches[1] . (!empty($matches[2]) ? '/' . $matches[2] : '');
118120
$database = $matches[2];
119121
$collection = $matches[3];
122+
123+
$mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient';
124+
$mongo = new $mongoClass($server);
120125
$this->mongo = $mongo->selectCollection($database, $collection);
121126
} else {
122-
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://user:pass@location/database/collection"', $this->dsn));
127+
throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://[user:pass@]host/database/collection"', $this->dsn));
123128
}
124129
}
125130

0 commit comments

Comments
 (0)