15
15
16
16
/**
17
17
* @author Markus Bachmann <markus.bachmann@bachi.biz>
18
- * @requires extension mongo
19
18
* @group time-sensitive
20
19
*/
21
20
class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
@@ -31,7 +30,15 @@ protected function setUp()
31
30
{
32
31
parent ::setUp ();
33
32
34
- $ mongoClass = version_compare (phpversion ('mongo ' ), '1.3.0 ' , '< ' ) ? 'Mongo ' : 'MongoClient ' ;
33
+ if (!extension_loaded ('mongo ' ) && !extension_loaded ('mongodb ' )) {
34
+ $ this ->markTestSkipped ('The Mongo or MongoDB extension is required. ' );
35
+ }
36
+
37
+ if (phpversion ('mongodb ' )) {
38
+ $ mongoClass = 'MongoDB\Client ' ;
39
+ } else {
40
+ $ mongoClass = version_compare (phpversion ('mongo ' ), '1.3.0 ' , '< ' ) ? 'Mongo ' : 'MongoClient ' ;
41
+ }
35
42
36
43
$ this ->mongo = $ this ->getMockBuilder ($ mongoClass )
37
44
->disableOriginalConstructor ()
@@ -98,14 +105,28 @@ public function testRead()
98
105
99
106
$ that ->assertArrayHasKey ($ that ->options ['expiry_field ' ], $ criteria );
100
107
$ that ->assertArrayHasKey ('$gte ' , $ criteria [$ that ->options ['expiry_field ' ]]);
101
- $ that ->assertInstanceOf ('MongoDate ' , $ criteria [$ that ->options ['expiry_field ' ]]['$gte ' ]);
102
- $ that ->assertGreaterThanOrEqual ($ criteria [$ that ->options ['expiry_field ' ]]['$gte ' ]->sec , $ testTimeout );
103
108
104
- return array (
109
+ if (phpversion ('mongodb ' )) {
110
+ $ that ->assertInstanceOf ('MongoDB\BSON\UTCDateTime ' , $ criteria [$ that ->options ['expiry_field ' ]]['$gte ' ]);
111
+ $ that ->assertGreaterThanOrEqual (round (intval ((string ) $ criteria [$ that ->options ['expiry_field ' ]]['$gte ' ]) / 1000 ), $ testTimeout );
112
+ } else {
113
+ $ that ->assertInstanceOf ('MongoDate ' , $ criteria [$ that ->options ['expiry_field ' ]]['$gte ' ]);
114
+ $ that ->assertGreaterThanOrEqual ($ criteria [$ that ->options ['expiry_field ' ]]['$gte ' ]->sec , $ testTimeout );
115
+ }
116
+
117
+ $ fields = array (
105
118
$ that ->options ['id_field ' ] => 'foo ' ,
106
- $ that ->options ['data_field ' ] => new \MongoBinData ('bar ' , \MongoBinData::BYTE_ARRAY ),
107
- $ that ->options ['id_field ' ] => new \MongoDate (),
108
119
);
120
+
121
+ if (phpversion ('mongodb ' )) {
122
+ $ fields [$ that ->options ['data_field ' ]] = new \MongoDB \BSON \Binary ('bar ' , \MongoDB \BSON \Binary::TYPE_OLD_BINARY );
123
+ $ fields [$ that ->options ['id_field ' ]] = new \MongoDB \BSON \UTCDateTime (time () * 1000 );
124
+ } else {
125
+ $ fields [$ that ->options ['data_field ' ]] = new \MongoBinData ('bar ' , \MongoBinData::BYTE_ARRAY );
126
+ $ fields [$ that ->options ['id_field ' ]] = new \MongoDate ();
127
+ }
128
+
129
+ return $ fields ;
109
130
}));
110
131
111
132
$ this ->assertEquals ('bar ' , $ this ->storage ->read ('foo ' ));
@@ -123,22 +144,36 @@ public function testWrite()
123
144
$ that = $ this ;
124
145
$ data = array ();
125
146
147
+ $ methodName = phpversion ('mongodb ' ) ? 'updateOne ' : 'update ' ;
148
+
126
149
$ collection ->expects ($ this ->once ())
127
- ->method (' update ' )
150
+ ->method ($ methodName )
128
151
->will ($ this ->returnCallback (function ($ criteria , $ updateData , $ options ) use ($ that , &$ data ) {
129
152
$ that ->assertEquals (array ($ that ->options ['id_field ' ] => 'foo ' ), $ criteria );
130
- $ that ->assertEquals (array ('upsert ' => true , 'multiple ' => false ), $ options );
153
+
154
+ if (phpversion ('mongodb ' )) {
155
+ $ that ->assertEquals (array ('upsert ' => true ), $ options );
156
+ } else {
157
+ $ that ->assertEquals (array ('upsert ' => true , 'multiple ' => false ), $ options );
158
+ }
131
159
132
160
$ data = $ updateData ['$set ' ];
133
161
}));
134
162
135
163
$ expectedExpiry = time () + (int ) ini_get ('session.gc_maxlifetime ' );
136
164
$ this ->assertTrue ($ this ->storage ->write ('foo ' , 'bar ' ));
137
165
138
- $ this ->assertEquals ('bar ' , $ data [$ this ->options ['data_field ' ]]->bin );
139
- $ that ->assertInstanceOf ('MongoDate ' , $ data [$ this ->options ['time_field ' ]]);
140
- $ this ->assertInstanceOf ('MongoDate ' , $ data [$ this ->options ['expiry_field ' ]]);
141
- $ this ->assertGreaterThanOrEqual ($ expectedExpiry , $ data [$ this ->options ['expiry_field ' ]]->sec );
166
+ if (phpversion ('mongodb ' )) {
167
+ $ that ->assertEquals ('bar ' , $ data [$ that ->options ['data_field ' ]]->getData ());
168
+ $ that ->assertInstanceOf ('MongoDB\BSON\UTCDateTime ' , $ data [$ that ->options ['time_field ' ]]);
169
+ $ that ->assertInstanceOf ('MongoDB\BSON\UTCDateTime ' , $ data [$ that ->options ['expiry_field ' ]]);
170
+ $ that ->assertGreaterThanOrEqual ($ expectedExpiry , round (intval ((string ) $ data [$ that ->options ['expiry_field ' ]]) / 1000 ));
171
+ } else {
172
+ $ that ->assertEquals ('bar ' , $ data [$ that ->options ['data_field ' ]]->bin );
173
+ $ that ->assertInstanceOf ('MongoDate ' , $ data [$ that ->options ['time_field ' ]]);
174
+ $ that ->assertInstanceOf ('MongoDate ' , $ data [$ that ->options ['expiry_field ' ]]);
175
+ $ that ->assertGreaterThanOrEqual ($ expectedExpiry , $ data [$ that ->options ['expiry_field ' ]]->sec );
176
+ }
142
177
}
143
178
144
179
public function testWriteWhenUsingExpiresField ()
@@ -164,20 +199,33 @@ public function testWriteWhenUsingExpiresField()
164
199
$ that = $ this ;
165
200
$ data = array ();
166
201
202
+ $ methodName = phpversion ('mongodb ' ) ? 'updateOne ' : 'update ' ;
203
+
167
204
$ collection ->expects ($ this ->once ())
168
- ->method (' update ' )
205
+ ->method ($ methodName )
169
206
->will ($ this ->returnCallback (function ($ criteria , $ updateData , $ options ) use ($ that , &$ data ) {
170
207
$ that ->assertEquals (array ($ that ->options ['id_field ' ] => 'foo ' ), $ criteria );
171
- $ that ->assertEquals (array ('upsert ' => true , 'multiple ' => false ), $ options );
208
+
209
+ if (phpversion ('mongodb ' )) {
210
+ $ that ->assertEquals (array ('upsert ' => true ), $ options );
211
+ } else {
212
+ $ that ->assertEquals (array ('upsert ' => true , 'multiple ' => false ), $ options );
213
+ }
172
214
173
215
$ data = $ updateData ['$set ' ];
174
216
}));
175
217
176
218
$ this ->assertTrue ($ this ->storage ->write ('foo ' , 'bar ' ));
177
219
178
- $ this ->assertEquals ('bar ' , $ data [$ this ->options ['data_field ' ]]->bin );
179
- $ that ->assertInstanceOf ('MongoDate ' , $ data [$ this ->options ['time_field ' ]]);
180
- $ that ->assertInstanceOf ('MongoDate ' , $ data [$ this ->options ['expiry_field ' ]]);
220
+ if (phpversion ('mongodb ' )) {
221
+ $ that ->assertEquals ('bar ' , $ data [$ that ->options ['data_field ' ]]->getData ());
222
+ $ that ->assertInstanceOf ('MongoDB\BSON\UTCDateTime ' , $ data [$ that ->options ['time_field ' ]]);
223
+ $ that ->assertInstanceOf ('MongoDB\BSON\UTCDateTime ' , $ data [$ that ->options ['expiry_field ' ]]);
224
+ } else {
225
+ $ that ->assertEquals ('bar ' , $ data [$ that ->options ['data_field ' ]]->bin );
226
+ $ that ->assertInstanceOf ('MongoDate ' , $ data [$ that ->options ['time_field ' ]]);
227
+ $ that ->assertInstanceOf ('MongoDate ' , $ data [$ that ->options ['expiry_field ' ]]);
228
+ }
181
229
}
182
230
183
231
public function testReplaceSessionData ()
@@ -191,16 +239,22 @@ public function testReplaceSessionData()
191
239
192
240
$ data = array ();
193
241
242
+ $ methodName = phpversion ('mongodb ' ) ? 'updateOne ' : 'update ' ;
243
+
194
244
$ collection ->expects ($ this ->exactly (2 ))
195
- ->method (' update ' )
245
+ ->method ($ methodName )
196
246
->will ($ this ->returnCallback (function ($ criteria , $ updateData , $ options ) use (&$ data ) {
197
247
$ data = $ updateData ;
198
248
}));
199
249
200
250
$ this ->storage ->write ('foo ' , 'bar ' );
201
251
$ this ->storage ->write ('foo ' , 'foobar ' );
202
252
203
- $ this ->assertEquals ('foobar ' , $ data ['$set ' ][$ this ->options ['data_field ' ]]->bin );
253
+ if (phpversion ('mongodb ' )) {
254
+ $ this ->assertEquals ('foobar ' , $ data ['$set ' ][$ this ->options ['data_field ' ]]->getData ());
255
+ } else {
256
+ $ this ->assertEquals ('foobar ' , $ data ['$set ' ][$ this ->options ['data_field ' ]]->bin );
257
+ }
204
258
}
205
259
206
260
public function testDestroy ()
@@ -212,8 +266,10 @@ public function testDestroy()
212
266
->with ($ this ->options ['database ' ], $ this ->options ['collection ' ])
213
267
->will ($ this ->returnValue ($ collection ));
214
268
269
+ $ methodName = phpversion ('mongodb ' ) ? 'deleteOne ' : 'remove ' ;
270
+
215
271
$ collection ->expects ($ this ->once ())
216
- ->method (' remove ' )
272
+ ->method ($ methodName )
217
273
->with (array ($ this ->options ['id_field ' ] => 'foo ' ));
218
274
219
275
$ this ->assertTrue ($ this ->storage ->destroy ('foo ' ));
@@ -230,19 +286,45 @@ public function testGc()
230
286
231
287
$ that = $ this ;
232
288
289
+ $ methodName = phpversion ('mongodb ' ) ? 'deleteOne ' : 'remove ' ;
290
+
233
291
$ collection ->expects ($ this ->once ())
234
- ->method (' remove ' )
292
+ ->method ($ methodName )
235
293
->will ($ this ->returnCallback (function ($ criteria ) use ($ that ) {
236
- $ that ->assertInstanceOf ('MongoDate ' , $ criteria [$ that ->options ['expiry_field ' ]]['$lt ' ]);
237
- $ that ->assertGreaterThanOrEqual (time () - 1 , $ criteria [$ that ->options ['expiry_field ' ]]['$lt ' ]->sec );
294
+ if (phpversion ('mongodb ' )) {
295
+ $ that ->assertInstanceOf ('MongoDB\BSON\UTCDateTime ' , $ criteria [$ that ->options ['expiry_field ' ]]['$lt ' ]);
296
+ $ that ->assertGreaterThanOrEqual (time () - 1 , round (intval ((string ) $ criteria [$ that ->options ['expiry_field ' ]]['$lt ' ]) / 1000 ));
297
+ } else {
298
+ $ that ->assertInstanceOf ('MongoDate ' , $ criteria [$ that ->options ['expiry_field ' ]]['$lt ' ]);
299
+ $ that ->assertGreaterThanOrEqual (time () - 1 , $ criteria [$ that ->options ['expiry_field ' ]]['$lt ' ]->sec );
300
+ }
238
301
}));
239
302
240
303
$ this ->assertTrue ($ this ->storage ->gc (1 ));
241
304
}
242
305
306
+ public function testGetConnection ()
307
+ {
308
+ $ method = new \ReflectionMethod ($ this ->storage , 'getMongo ' );
309
+ $ method ->setAccessible (true );
310
+
311
+ if (phpversion ('mongodb ' )) {
312
+ $ mongoClass = 'MongoDB\Client ' ;
313
+ } else {
314
+ $ mongoClass = version_compare (phpversion ('mongo ' ), '1.3.0 ' , '< ' ) ? 'Mongo ' : 'MongoClient ' ;
315
+ }
316
+
317
+ $ this ->assertInstanceOf ($ mongoClass , $ method ->invoke ($ this ->storage ));
318
+ }
319
+
243
320
private function createMongoCollectionMock ()
244
321
{
245
- $ collection = $ this ->getMockBuilder ('MongoCollection ' )
322
+ $ collectionClass = 'MongoCollection ' ;
323
+ if (phpversion ('mongodb ' )) {
324
+ $ collectionClass = 'MongoDB\Collection ' ;
325
+ }
326
+
327
+ $ collection = $ this ->getMockBuilder ($ collectionClass )
246
328
->disableOriginalConstructor ()
247
329
->getMock ();
248
330
0 commit comments