8000 Refactor the unit test for the "MongoDbSessionHandler" · symfony/symfony@f2d8a8a · GitHub
[go: up one dir, main page]

Skip to content

Commit f2d8a8a

Browse files
committed
Refactor the unit test for the "MongoDbSessionHandler"
1 parent ecab04c commit f2d8a8a

File tree

1 file changed

+132
-35
lines changed

1 file changed

+132
-35
lines changed

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

Lines changed: 132 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,35 @@
1818
*/
1919
class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
2020
{
21-
private static $mongo;
21+
/**
22+
* @var \PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
private $mongo;
2225

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;
3230

3331
protected function setUp()
3432
{
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');
3735
}
3836

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();
4140

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+
);
4448

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);
5050
}
5151

5252
public function testOpenMethodAlwaysReturnTrue()
@@ -61,39 +61,136 @@ public function testCloseMethodAlwaysReturnTrue()
6161

6262
public function testWrite()
6363
{
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+
6494
$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);
6698
}
6799

68100
public function testReplaceSessionData()
69101
{
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+
70128
$this->storage->write('foo', 'bar');
71129
$this->storage->write('foo', 'foobar');
72130

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);
77132
}
78133

79134
public function testDestroy()
80135
{
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+
);
83160

84-
$this->assertEquals('', $this->storage->read('foo'));
161+
162+
$this->storage->destroy('foo');
85163
}
86164

87165
public function testGc()
88166
{
89-
$this->storage->write('foo', 'bar');
90-
$this->storage->write('bar', 'foo');
167+
$database = $this->getMockBuilder('MongoDB')
168+
->disableOriginalConstructor()
169+
->getMock();
91170

92-
$coll = self::$mongo->selectDB($this->options['database'])->selectCollection($this->options['collection']);
171+
$collection = $this->getMockBuilder('MongoCollection')
172+
->disableOriginalConstructor()
173+
->getMock();
93174

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));
97179

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);
98195
}
99196
}

0 commit comments

Comments
 (0)
0