diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php index c9eb0b38d6..979880d0a3 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php @@ -18,35 +18,35 @@ use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandl */ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase { - private static $mongo; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $mongo; - public static function setUpBeforeClass() - { - if (class_exists('\Mongo')) { - try { - self::$mongo = new \Mongo(); - } catch (\Exception $e) { - } - } - } + /** + * @var array + */ + private $options; protected function setUp() { - if (null === self::$mongo) { - $this->markTestSkipped('MongoDbSessionHandler requires the php "mongo" extension and a mongodb server on localhost'); + if (!class_exists('\Mongo')) { + $this->markTestSkipped('MongoDbSessionHandler requires the php "mongo" extension'); } - $this->options = array('database' => 'sf2-test', 'collection' => 'session-test'); - $this->options = array('database' => 'sf2-test', 'collection' => 'session-test'); + $this->mongo = $this->getMockBuilder('Mongo') + ->disableOriginalConstructor() + ->getMock(); - $this->storage = new MongoDbSessionHandler(self::$mongo, $this->options); - } + $this->options = array( + 'id_field' => 'sess_id', + 'data_field' => 'sess_data', + 'time_field' => 'sess_time', + 'database' => 'sf2-test', + 'collection' => 'session-test' + ); - protected function tearDown() - { - if (null !== self::$mongo) { - self::$mongo->dropDB($this->options['database']); - } + $this->storage = new MongoDbSessionHandler($this->mongo, $this->options); } public function testOpenMethodAlwaysReturnTrue() @@ -61,39 +61,136 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase public function testWrite() { + $database = $this->getMockBuilder('MongoDB') + ->disableOriginalConstructor() + ->getMock(); + + $collection = $this->getMockBuilder('MongoCollection') + ->disableOriginalConstructor() + ->getMock(); + + $this->mongo->expects($this->once()) + ->method('selectDB') + ->with($this->options['database']) + ->will($this->returnValue($database)); + + $database->expects($this->once()) + ->method('selectCollection') + ->with($this->options['collection']) + ->will($this->returnValue($collection)); + + $that = $this; + $data = array(); + + $collection->expects($this->once()) + ->method('update') + ->will($this->returnCallback(function($citeria, $updateData, $options) use ($that, &$data) { + $that->assertEquals(array($that->options['id_field'] => 'foo'), $citeria); + $that->assertEquals(array('upsert' => true), $options); + + $data = $updateData['$set']; + })); + $this->assertTrue($this->storage->write('foo', 'bar')); - $this->assertEquals('bar', $this->storage->read('foo')); + + $this->assertEquals('foo', $data[$this->options['id_field']]); + $this->assertEquals('bar', $data[$this->options['data_field']]->bin); } public function testReplaceSessionData() { + $database = $this->getMockBuilder('MongoDB') + ->disableOriginalConstructor() + ->getMock(); + + $collection = $this->getMockBuilder('MongoCollection') + ->disableOriginalConstructor() + ->getMock(); + + $this->mongo->expects($this->once()) + ->method('selectDB') + ->with($this->options['database']) + ->will($this->returnValue($database)); + + $database->expects($this->once()) + ->method('selectCollection') + ->with($this->options['collection']) + ->will($this->returnValue($collection)); + + $data = array(); + + $collection->expects($this->exactly(2)) + ->method('update') + ->will($this->returnCallback(function($citeria, $updateData, $options) use (&$data) { + $data = $updateData; + })); + $this->storage->write('foo', 'bar'); $this->storage->write('foo', 'foobar'); - $coll = self::$mongo->selectDB($this->options['database'])->selectCollection($this->options['collection']); - - $this->assertEquals('foobar', $this->storage->read('foo')); - $this->assertEquals(1, $coll->find(array('sess_id' => 'foo'))->count()); + $this->assertEquals('foobar', $data['$set'][$this->options['data_field']]->bin); } public function testDestroy() { - $this->storage->write('foo', 'bar'); - $this->storage->destroy('foo'); + $database = $this->getMockBuilder('MongoDB') + ->disableOriginalConstructor() + ->getMock(); - $this->assertEquals('', $this->storage->read('foo')); + $collection = $this->getMockBuilder('MongoCollection') + ->disableOriginalConstructor() + ->getMock(); + + $this->mongo->expects($this->once()) + ->method('selectDB') + ->with($this->options['database']) + ->will($this->returnValue($database)); + + $database->expects($this->once()) + ->method('selectCollection') + ->with($this->options['collection']) + ->will($this->returnValue($collection)); + + $collection->expects($this->once()) + ->method('remove') + ->with( + array($this->options['id_field'] => 'foo'), + array('justOne' => true) + ); + + + $this->storage->destroy('foo'); } public function testGc() { - $this->storage->write('foo', 'bar'); - $this->storage->write('bar', 'foo'); + $database = $this->getMockBuilder('MongoDB') + ->disableOriginalConstructor() + ->getMock(); - $coll = self::$mongo->selectDB($this->options['database'])->selectCollection($this->options['collection']); + $collection = $this->getMockBuilder('MongoCollection') + ->disableOriginalConstructor() + ->getMock(); + + $this->mongo->expects($this->once()) + ->method('selectDB') + ->with($this->options['database']) + ->will($this->returnValue($database)); + + $database->expects($this->once()) + ->method('selectCollection') + ->with($this->options['collection']) + ->will($this->returnValue($collection)); + + $that = $this; + + $collection->expects($this->once()) + ->method('remove') + ->will($this->returnCallback(function($citeria) use($that) { + $that->assertInstanceOf('MongoTimestamp', $citeria[$that->options['time_field']]['$lt']); + $that->assertGreaterThanOrEqual(time() - -1, $citeria[$that->options['time_field']]['$lt']->sec); + })); - $this->assertEquals(2, $coll->count()); $this->storage->gc(-1); - $this->assertEquals(0, $coll->count()); - } }