Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #127 from doctrine/database-methods
Browse files Browse the repository at this point in the history
Improve events/logging for Database methods
  • Loading branch information
jmikola committed Aug 12, 2013
2 parents 028f058 + c76cdbc commit ea5b7d0
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 36 deletions.
48 changes: 34 additions & 14 deletions lib/Doctrine/MongoDB/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ public function command(array $data, array $options = array())
/**
* Wrapper method for MongoDB::createCollection().
*
* @see http://php.net/manual/en/mongodb.command.php
* This method will dispatch preCreateCollection and postCreateCollection
* events.
*
* @see http://php.net/manual/en/mongodb.createcollection.php
* @param string $name Collection name
* @param boolean|array $cappedOrOptions Capped collection indicator or an
* options array (for driver 1.4+)
Expand All @@ -139,16 +142,10 @@ public function createCollection($name, $cappedOrOptions = false, $size = 0, $ma
$this->eventManager->dispatchEvent(Events::preCreateCollection, new CreateCollectionEventArgs($this, $name, $options));
}

if (version_compare(phpversion('mongo'), '1.4.0', '>=')) {
$this->getMongoDB()->createCollection($name, $options);
} else {
$this->getMongoDB()->createCollection($name, $options['capped'], $options['size'], $options['max']);
}

$result = $this->selectCollection($name);
$result = $this->doCreateCollection($name, $options);

if ($this->eventManager->hasListeners(Events::postCreateCollection)) {
$this->eventManager->dispatchEvent(Events::postCreateCollection, new EventArgs($this, $prefix));
$this->eventManager->dispatchEvent(Events::postCreateCollection, new EventArgs($this, $result));
}

return $result;
Expand All @@ -170,6 +167,8 @@ public function createDBRef($collection, $a)
/**
* Wrapper method for MongoDB::drop().
*
* This method will dispatch preDropDatabase and postDropDatabase events.
*
* @see http://php.net/manual/en/mongodb.drop.php
* @return array
*/
Expand Down Expand Up @@ -251,6 +250,8 @@ public function getDBRef(array $reference)
/**
* Wrapper method for MongoDB::getGridFS().
*
* This method will dispatch preGetGridFS and postGetGridFS events.
*
* @see http://php.net/manual/en/mongodb.getgridfs.php
* @param string $prefix
* @return GridFS
Expand All @@ -261,13 +262,13 @@ public function getGridFS($prefix = 'fs')
$this->eventManager->dispatchEvent(Events::preGetGridFS, new EventArgs($this, $prefix));
}

$gridFS = $this->doGetGridFs($prefix);
$gridfs = $this->doGetGridFS($prefix);

if ($this->eventManager->hasListeners(Events::preGetGridFS)) {
$this->eventManager->dispatchEvent(Events::preGetGridFS, new EventArgs($this, $gridFS));
if ($this->eventManager->hasListeners(Events::postGetGridFS)) {
$this->eventManager->dispatchEvent(Events::postGetGridFS, new EventArgs($this, $gridfs));
}

return $gridFS;
return $gridfs;
}

/**
Expand Down Expand Up @@ -519,14 +520,33 @@ protected function doGetDBRef(array $reference)
});
}

/**
* Creates a collection.
*
* @see Database::createCollection()
* @param string $name
* @param array $options
* @return Collection
*/
protected function doCreateCollection($name, array $options)
{
if (version_compare(phpversion('mongo'), '1.4.0', '>=')) {
$this->getMongoDB()->createCollection($name, $options);
} else {
$this->getMongoDB()->createCollection($name, $options['capped'], $options['size'], $options['max']);
}

return $this->doSelectCollection($name);
}

/**
* Return a new GridFS instance.
*
* @see Database::getGridFS()
* @param string $prefix
* @return GridFS
*/
protected function doGetGridFs($prefix)
protected function doGetGridFS($prefix)
{
return new GridFS($this->connection, $prefix, $this, $this->eventManager, $this->cmd);
}
Expand Down
17 changes: 12 additions & 5 deletions lib/Doctrine/MongoDB/LoggableDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,23 @@ public function command(array $data, array $options = array())
/**
* @see Database::createCollection()
*/
public function createCollection($name, $capped = false, $size = 0, $max = 0)
public function createCollection($name, $cappedOrOptions = false, $size = 0, $max = 0)
{
$options = is_array($cappedOrOptions)
? array_merge(array('capped' => false, 'size' => 0, 'max' => 0), $cappedOrOptions)
: array('capped' => $cappedOrOptions, 'size' => $size, 'max' => $max);

$this->log(array(
'createCollection' => true,
'capped' => $capped,
'size' => $size,
'max' => $max,
'name' => $name,
'options' => $options,
/* @deprecated 1.1 Replaced by options; will be removed for 2.0 */
'capped' => $options['capped'],
'size' => $options['size'],
'max' => $options['max'],
));

return parent::createCollection($name, $capped, $size, $max);
return parent::createCollection($name, $options);
}

/**
Expand Down
91 changes: 91 additions & 0 deletions tests/Doctrine/MongoDB/Tests/DatabaseEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,51 @@

use Doctrine\Common\EventManager;
use Doctrine\MongoDB\Events;
use Doctrine\MongoDB\Event\CreateCollectionEventArgs;
use Doctrine\MongoDB\Event\EventArgs;
use Doctrine\MongoDB\Event\MutableEventArgs;

class DatabaseEventsTest extends \PHPUnit_Framework_TestCase
{
const databaseName = 'database';

public function testCreateCollection()
{
$name = 'collection';
$options = array('capped' => false, 'size' => 0, 'max' => 0);
$result = $this->getMockCollection();

$eventManager = $this->getMockEventManager();
$db = $this->getMockDatabase($eventManager, array('doCreateCollection' => $result));

$this->expectEvents($eventManager, array(
array(Events::preCreateCollection, new CreateCollectionEventArgs($db, $name, $options)),
array(Events::postCreateCollection, new EventArgs($db, $result)),
));

$this->assertSame($result, $db->createCollection($name, $options));
}

public function testDrop()
{
$result = array('dropped' => self::databaseName, 'ok' => 1);

$mongoDB = $this->getMockMongoDB();
$mongoDB->expects($this->once())
->method('drop')
->will($this->returnValue($result));

$eventManager = $this->getMockEventManager();
$db = $this->getMockDatabase($eventManager, array('getMongoDB' => $mongoDB));

$this->expectEvents($eventManager, array(
array(Events::preDropDatabase, new EventArgs($db)),
array(Events::postDropDatabase, new EventArgs($db)),
));

$this->assertSame($result, $db->drop());
}

public function testGetDBRef()
{
$reference = array('$ref' => 'collection', '$id' => 1);
Expand All @@ -27,6 +65,38 @@ public function testGetDBRef()
$this->assertSame($result, $db->getDBRef($reference));
}

public function testGetGridFS()
{
$prefix = 'fs';
$result = $this->getMockGridFS();

$eventManager = $this->getMockEventManager();
$db = $this->getMockDatabase($eventManager, array('doGetGridFS' => $result));

$this->expectEvents($eventManager, array(
array(Events::preGetGridFS, new EventArgs($db, $prefix)),
array(Events::postGetGridFS, new EventArgs($db, $result)),
));

$this->assertSame($result, $db->getGridFS());
}

public function testSelectCollection()
{
$name = 'collection';
$result = $this->getMockCollection();

$eventManager = $this->getMockEventManager();
$db = $this->getMockDatabase($eventManager, array('doSelectCollection' => $result));

$this->expectEvents($eventManager, array(
array(Events::preSelectCollection, new EventArgs($db, $name)),
array(Events::postSelectCollection, new EventArgs($db, $result)),
));

$this->assertSame($result, $db->selectCollection($name));
}

/**
* Expect events to be dispatched by the event manager in the given order.
*
Expand Down Expand Up @@ -54,6 +124,13 @@ private function expectEvents(EventManager $em, array $events)
}
}

private function getMockCollection()
{
return $this->getMockBuilder('Doctrine\MongoDB\Collection')
->disableOriginalConstructor()
->getMock();
}

private function getMockDatabase(EventManager $em, array $methods)
{
$c = $this->getMockBuilder('Doctrine\MongoDB\Connection')
Expand All @@ -80,4 +157,18 @@ private function getMockEventManager()
->disableOriginalConstructor()
->getMock();
}

private function getMockGridFS()
{
return $this->getMockBuilder('Doctrine\MongoDB\GridFS')
->disableOriginalConstructor()
->getMock();
}

private function getMockMongoDB()
{
return $this->getMockBuilder('MongoDB')
->disableOriginalConstructor()
->getMock();
}
}
2 changes: 1 addition & 1 deletion tests/Doctrine/MongoDB/Tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testCreateCollectionWithOptionsArgument()
if (version_compare(phpversion('mongo'), '1.4.0', '>=')) {
$this->mongodb->expects($this->once())
->method('createCollection')
->with('foo', array('capped' => true, 'size' => 10485760, 'autoIndexId' => false, 'max' => 0));
->with('foo', array('capped' => true, 'size' => 10485760, 'max' => 0, 'autoIndexId' => false,));
} else {
$this->mongodb->expects($this->once())
->method('createCollection')
Expand Down
35 changes: 19 additions & 16 deletions tests/Doctrine/MongoDB/Tests/LoggableCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Doctrine\MongoDB\Tests;

use Doctrine\MongoDB\LoggableCollection;
use Doctrine\Common\EventManager;

class LoggableCollectionTest extends \PHPUnit_Framework_TestCase
{
const collectionName = 'collection';
const databaseName = 'database';

public function testLog()
{
$called = false;
Expand All @@ -15,29 +17,30 @@ public function testLog()
$called = $msg;
};

$database = $this->getMockDatabase();

$database->expects($this->once())
->method('getName')
->will($this->returnValue('test'));

$coll = new LoggableCollection($this->getMockConnection(), 'foo', $database, new EventManager(), '$', $loggerCallable);
$coll->log(array('test' => 'test'));
$collection = $this->getTestLoggableDatabase($loggerCallable);
$collection->log(array('test' => 'test'));

$this->assertEquals(array('collection' => 'foo', 'db' => 'test', 'test' => 'test'), $called);
$this->assertEquals(array('collection' => self::collectionName, 'db' => self::databaseName, 'test' => 'test'), $called);
}

private function getMockDatabase()
private function getTestLoggableDatabase($loggerCallable)
{
return $this->getMockBuilder('Doctrine\MongoDB\Database')
$c = $this->getMockBuilder('Doctrine\MongoDB\Connection')
->disableOriginalConstructor()
->getMock();
}

private function getMockConnection()
{
return $this->getMockBuilder('Doctrine\MongoDB\Connection')
$db = $this->getMockBuilder('Doctrine\MongoDB\Database')
->disableOriginalConstructor()
->getMock();

$db->expects($this->any())
->method('getName')
->will($this->returnValue(self::databaseName));

$em = $this->getMockBuilder('Doctrine\Common\EventManager')
->disableOriginalConstructor()
->getMock();

return new LoggableCollection($c, self::collectionName, $db, $em, '$', $loggerCallable);
}
}
Loading

0 comments on commit ea5b7d0

Please sign in to comment.