1515
1616/**
1717 * @author Markus Bachmann <markus.bachmann@bachi.biz>
18- * @requires extension mongo
1918 * @group time-sensitive
2019 */
2120class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
@@ -31,7 +30,15 @@ protected function setUp()
3130 {
3231 parent ::setUp ();
3332
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+ }
3542
3643 $ this ->mongo = $ this ->getMockBuilder ($ mongoClass )
3744 ->disableOriginalConstructor ()
@@ -98,14 +105,28 @@ public function testRead()
98105
99106 $ that ->assertArrayHasKey ($ that ->options ['expiry_field ' ], $ criteria );
100107 $ 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 );
103108
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 (
105118 $ that ->options ['id_field ' ] => 'foo ' ,
106- $ that ->options ['data_field ' ] => new \MongoBinData ('bar ' , \MongoBinData::BYTE_ARRAY ),
107- $ that ->options ['id_field ' ] => new \MongoDate (),
108119 );
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 ;
109130 }));
110131
111132 $ this ->assertEquals ('bar ' , $ this ->storage ->read ('foo ' ));
@@ -123,22 +144,36 @@ public function testWrite()
123144 $ that = $ this ;
124145 $ data = array ();
125146
147+ $ methodName = phpversion ('mongodb ' ) ? 'updateOne ' : 'update ' ;
148+
126149 $ collection ->expects ($ this ->once ())
127- ->method (' update ' )
150+ ->method ($ methodName )
128151 ->will ($ this ->returnCallback (function ($ criteria , $ updateData , $ options ) use ($ that , &$ data ) {
129152 $ 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+ }
131159
132160 $ data = $ updateData ['$set ' ];
133161 }));
134162
135163 $ expectedExpiry = time () + (int ) ini_get ('session.gc_maxlifetime ' );
136164 $ this ->assertTrue ($ this ->storage ->write ('foo ' , 'bar ' ));
137165
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+ }
142177 }
143178
144179 public function testWriteWhenUsingExpiresField ()
@@ -164,20 +199,33 @@ public function testWriteWhenUsingExpiresField()
164199 $ that = $ this ;
165200 $ data = array ();
166201
202+ $ methodName = phpversion ('mongodb ' ) ? 'updateOne ' : 'update ' ;
203+
167204 $ collection ->expects ($ this ->once ())
168- ->method (' update ' )
205+ ->method ($ methodName )
169206 ->will ($ this ->returnCallback (function ($ criteria , $ updateData , $ options ) use ($ that , &$ data ) {
170207 $ 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+ }
172214
173215 $ data = $ updateData ['$set ' ];
174216 }));
175217
176218 $ this ->assertTrue ($ this ->storage ->write ('foo ' , 'bar ' ));
177219
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+ }
181229 }
182230
183231 public function testReplaceSessionData ()
@@ -191,16 +239,22 @@ public function testReplaceSessionData()
191239
192240 $ data = array ();
193241
242+ $ methodName = phpversion ('mongodb ' ) ? 'updateOne ' : 'update ' ;
243+
194244 $ collection ->expects ($ this ->exactly (2 ))
195- ->method (' update ' )
245+ ->method ($ methodName )
196246 ->will ($ this ->returnCallback (function ($ criteria , $ updateData , $ options ) use (&$ data ) {
197247 $ data = $ updateData ;
198248 }));
199249
200250 $ this ->storage ->write ('foo ' , 'bar ' );
201251 $ this ->storage ->write ('foo ' , 'foobar ' );
202252
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+ }
204258 }
205259
206260 public function testDestroy ()
@@ -212,8 +266,10 @@ public function testDestroy()
212266 ->with ($ this ->options ['database ' ], $ this ->options ['collection ' ])
213267 ->will ($ this ->returnValue ($ collection ));
214268
269+ $ methodName = phpversion ('mongodb ' ) ? 'deleteOne ' : 'remove ' ;
270+
215271 $ collection ->expects ($ this ->once ())
216- ->method (' remove ' )
272+ ->method ($ methodName )
217273 ->with (array ($ this ->options ['id_field ' ] => 'foo ' ));
218274
219275 $ this ->assertTrue ($ this ->storage ->destroy ('foo ' ));
@@ -230,19 +286,45 @@ public function testGc()
230286
231287 $ that = $ this ;
232288
289+ $ methodName = phpversion ('mongodb ' ) ? 'deleteOne ' : 'remove ' ;
290+
233291 $ collection ->expects ($ this ->once ())
234- ->method (' remove ' )
292+ ->method ($ methodName )
235293 ->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+ }
238301 }));
239302
240303 $ this ->assertTrue ($ this ->storage ->gc (1 ));
241304 }
242305
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+
243320 private function createMongoCollectionMock ()
244321 {
245- $ collection = $ this ->getMockBuilder ('MongoCollection ' )
322+ $ collectionClass = 'MongoCollection ' ;
323+ if (phpversion ('mongodb ' )) {
324+ $ collectionClass = 'MongoDB\Collection ' ;
325+ }
326+
327+ $ collection = $ this ->getMockBuilder ($ collectionClass )
246328 ->disableOriginalConstructor ()
247329 ->getMock ();
248330
0 commit comments