18
18
*/
19
19
class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
20
20
{
21
- private static $ mongo ;
21
+ /**
22
+ * @var \PHPUnit_Framework_MockObject_MockObject
23
+ */
24
+ private $ mongo ;
22
25
23
- public static function setUpBeforeClass ()
24
- {
25
- if (class_exists ('\Mongo ' )) {
26
- try {
27
- self ::$ mongo = new \Mongo ();
28
- } catch (\Exception $ e ) {
29
- }
30
- }
31
- }
26
+ /**
27
+ * @var array
28
+ */
29
+ private $ options ;
32
30
33
31
protected function setUp ()
34
32
{
35
- if (null === self :: $ mongo ) {
36
- $ this ->markTestSkipped ('MongoDbSessionHandler requires the php "mongo" extension and a mongodb server on localhost ' );
33
+ if (! class_exists ( ' \Mongo ' ) ) {
34
+ $ this ->markTestSkipped ('MongoDbSessionHandler requires the php "mongo" extension ' );
37
35
}
38
36
39
- $ this ->options = array ('database ' => 'sf2-test ' , 'collection ' => 'session-test ' );
40
- $ this ->options = array ('database ' => 'sf2-test ' , 'collection ' => 'session-test ' );
37
+ $ this ->mongo = $ this ->getMockBuilder ('Mongo ' )
38
+ ->disableOriginalConstructor ()
39
+ ->getMock ();
41
40
42
- $ this ->storage = new MongoDbSessionHandler (self ::$ mongo , $ this ->options );
43
- }
41
+ $ this ->options = array (
42
+ 'id_field ' => 'sess_id ' ,
43
+ 'data_field ' => 'sess_data ' ,
44
+ 'time_field ' => 'sess_time ' ,
45
+ 'database ' => 'sf2-test ' ,
46
+ 'collection ' => 'session-test '
47
+ );
44
48
45
- protected function tearDown ()
46
- {
47
- if (null !== self ::$ mongo ) {
48
- self ::$ mongo ->dropDB ($ this ->options ['database ' ]);
49
- }
49
+ $ this ->storage = new MongoDbSessionHandler ($ this ->mongo , $ this ->options );
50
50
}
51
51
52
52
public function testOpenMethodAlwaysReturnTrue ()
@@ -61,39 +61,136 @@ public function testCloseMethodAlwaysReturnTrue()
61
61
62
62
public function testWrite ()
63
63
{
64
+ $ database = $ this ->getMockBuilder ('MongoDB ' )
65
+ ->disableOriginalConstructor ()
66
+ ->getMock ();
67
+
68
+ $ collection = $ this ->getMockBuilder ('MongoCollection ' )
69
+ ->disableOriginalConstructor ()
70
+ ->getMock ();
71
+
72
+ $ this ->mongo ->expects ($ this ->once ())
73
+ ->method ('selectDB ' )
74
+ ->with ($ this ->options ['database ' ])
75
+ ->will ($ this ->returnValue ($ database ));
76
+
77
+ $ database ->expects ($ this ->once ())
78
+ ->method ('selectCollection ' )
79
+ ->with ($ this ->options ['collection ' ])
80
+ ->will ($ this ->returnValue ($ collection ));
81
+
82
+ $ that = $ this ;
83
+ $ data = array ();
84
+
85
+ $ collection ->expects ($ this ->once ())
86
+ ->method ('update ' )
87
+ ->will ($ this ->returnCallback (function ($ citeria , $ updateData , $ options ) use ($ that , &$ data ) {
88
+ $ that ->assertEquals (array ($ that ->options ['id_field ' ] => 'foo ' ), $ citeria );
89
+ $ that ->assertEquals (array ('upsert ' => true ), $ options );
90
+
91
+ $ data = $ updateData ['$set ' ];
92
+ }));
93
+
64
94
$ this ->assertTrue ($ this ->storage ->write ('foo ' , 'bar ' ));
65
- $ this ->assertEquals ('bar ' , $ this ->storage ->read ('foo ' ));
95
+
96
+ $ this ->assertEquals ('foo ' , $ data [$ this ->options ['id_field ' ]]);
97
+ $ this ->assertEquals ('bar ' , $ data [$ this ->options ['data_field ' ]]->bin );
66
98
}
67
99
68
100
public function testReplaceSessionData ()
69
101
{
102
+ $ database = $ this ->getMockBuilder ('MongoDB ' )
103
+ ->disableOriginalConstructor ()
104
+ ->getMock ();
105
+
106
+ $ collection = $ this ->getMockBuilder ('MongoCollection ' )
107
+ ->disableOriginalConstructor ()
108
+ ->getMock ();
109
+
110
+ $ this ->mongo ->expects ($ this ->once ())
111
+ ->method ('selectDB ' )
112
+ ->with ($ this ->options ['database ' ])
113
+ ->will ($ this ->returnValue ($ database ));
114
+
115
+ $ database ->expects ($ this ->once ())
116
+ ->method ('selectCollection ' )
117
+ ->with ($ this ->options ['collection ' ])
118
+ ->will ($ this ->returnValue ($ collection ));
119
+
120
+ $ data = array ();
121
+
122
+ $ collection ->expects ($ this ->exactly (2 ))
123
+ ->method ('update ' )
124
+ ->will ($ this ->returnCallback (function ($ citeria , $ updateData , $ options ) use (&$ data ) {
125
+ $ data = $ updateData ;
126
+ }));
127
+
70
128
$ this ->storage ->write ('foo ' , 'bar ' );
71
129
$ this ->storage ->write ('foo ' , 'foobar ' );
72
130
73
- $ coll = self ::$ mongo ->selectDB ($ this ->options ['database ' ])->selectCollection ($ this ->options ['collection ' ]);
74
-
75
- $ this ->assertEquals ('foobar ' , $ this ->storage ->read ('foo ' ));
76
- $ this ->assertEquals (1 , $ coll ->find (array ('sess_id ' => 'foo ' ))->count ());
131
+ $ this ->assertEquals ('foobar ' , $ data ['$set ' ][$ this ->options ['data_field ' ]]->bin );
77
132
}
78
133
79
134
public function testDestroy ()
80
135
{
81
- $ this ->storage ->write ('foo ' , 'bar ' );
82
- $ this ->storage ->destroy ('foo ' );
136
+ $ database = $ this ->getMockBuilder ('MongoDB ' )
137
+ ->disableOriginalConstructor ()
138
+ ->getMock ();
139
+
140
+ $ collection = $ this ->getMockBuilder ('MongoCollection ' )
141
+ ->disableOriginalConstructor ()
142
+ ->getMock ();
143
+
144
+ $ this ->mongo ->expects ($ this ->once ())
145
+ ->method ('selectDB ' )
146
+ ->with ($ this ->options ['database ' ])
147
+ ->will ($ this ->returnValue ($ database ));
148
+
149
+ $ database ->expects ($ this ->once ())
150
+ ->method ('selectCollection ' )
151
+ ->with ($ this ->options ['collection ' ])
152
+ ->will ($ this ->returnValue ($ collection ));
153
+
154
+ $ collection ->expects ($ this ->once ())
155
+ ->method ('remove ' )
156
+ ->with (
157
+ array ($ this ->options ['id_field ' ] => 'foo ' ),
158
+ array ('justOne ' => true )
159
+ );
83
160
84
- $ this ->assertEquals ('' , $ this ->storage ->read ('foo ' ));
161
+
162
+ $ this ->storage ->destroy ('foo ' );
85
163
}
86
164
87
165
public function testGc ()
88
166
{
89
- $ this ->storage ->write ('foo ' , 'bar ' );
90
- $ this ->storage ->write ('bar ' , 'foo ' );
167
+ $ database = $ this ->getMockBuilder ('MongoDB ' )
168
+ ->disableOriginalConstructor ()
169
+ ->getMock ();
91
170
92
- $ coll = self ::$ mongo ->selectDB ($ this ->options ['database ' ])->selectCollection ($ this ->options ['collection ' ]);
171
+ $ collection = $ this ->getMockBuilder ('MongoCollection ' )
172
+ ->disableOriginalConstructor ()
173
+ ->getMock ();
93
174
94
- $ this ->assertEquals (2 , $ coll ->count ());
95
- $ this ->storage ->gc (-1 );
96
- $ this ->assertEquals (0 , $ coll ->count ());
175
+ $ this ->mongo ->expects ($ this ->once ())
176
+ ->method ('selectDB ' )
177
+ ->with ($ this ->options ['database ' ])
178
+ ->will ($ this ->returnValue ($ database ));
97
179
180
+ $ database ->expects ($ this ->once ())
181
+ ->method ('selectCollection ' )
182
+ ->with ($ this ->options ['collection ' ])
183
+ ->will ($ this ->returnValue ($ collection ));
184
+
185
+ $ that = $ this ;
186
+
187
+ $ collection ->expects ($ this ->once ())
188
+ ->method ('remove ' )
189
+ ->will ($ this ->returnCallback (function ($ citeria ) use ($ that ) {
190
+ $ that ->assertInstanceOf ('MongoTimestamp ' , $ citeria [$ that ->options ['time_field ' ]]['$lt ' ]);
191
+ $ that ->assertGreaterThanOrEqual (time () - -1 , $ citeria [$ that ->options ['time_field ' ]]['$lt ' ]->sec );
192
+ }));
193
+
194
+ $ this ->storage ->gc (-1 );
98
195
}
99
196
}
0 commit comments