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

Commit

Permalink
Make LoggableDatabase::createCollection() API consistent with Database
Browse files Browse the repository at this point in the history
LoggableDatabase must support the same arguments as the base method, which means duplicate the logic.

Ensure the created collection's name is logged, as well as the new options array. Deprecate the older log format (akin to what was done in CreateCollectionEventArgs).
  • Loading branch information
jmikola committed Aug 12, 2013
1 parent 8f3f95d commit 839493d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 5 deletions.
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
105 changes: 105 additions & 0 deletions tests/Doctrine/MongoDB/Tests/LoggableDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Doctrine\MongoDB\Tests;

use Doctrine\MongoDB\LoggableDatabase;

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

public function testLog()
{
$called = false;

$loggerCallable = function($msg) use (&$called) {
$called = $msg;
};

$db = $this->getTestLoggableDatabase($loggerCallable);
$db->log(array('test' => 'test'));

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

public function testCreateCollectionWithMultipleArguments()
{
$called = false;

$loggerCallable = function($msg) use (&$called) {
$called = $msg;
};

$db = $this->getTestLoggableDatabase($loggerCallable);
$db->createCollection('foo', true, 10485760, 0);

$expected = array(
'createCollection' => true,
'name' => 'foo',
'options' => array('capped' => true, 'size' => 10485760, 'max' => 0),
'capped' => true,
'size' => 10485760,
'max' => 0,
'db' => self::databaseName,
);

$this->assertEquals($expected, $called);
}

public function testCreateCollectionWithOptionsArgument()
{
$called = false;

$loggerCallable = function($msg) use (&$called) {
$called = $msg;
};

$db = $this->getTestLoggableDatabase($loggerCallable);
$db->createCollection('foo', array('capped' => true, 'size' => 10485760, 'autoIndexId' => false));

$expected = array(
'createCollection' => true,
'name' => 'foo',
'options' => array('capped' => true, 'size' => 10485760, 'max' => 0, 'autoIndexId' => false),
'capped' => true,
'size' => 10485760,
'max' => 0,
'db' => self::databaseName,
);

$this->assertEquals($expected, $called);
}

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

$mdb = $this->getMockBuilder('MongoDB')
->disableOriginalConstructor()
->getMock();

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

$db = new TestLoggableDatabaseStub($c, self::databaseName, $em, '$', 0, $loggerCallable);
$db->setMongoDB($mdb);

return $db;
}
}

class TestLoggableDatabaseStub extends LoggableDatabase
{
public function setMongoDB($mongoDB)
{
$this->mongoDB = $mongoDB;
}

public function getMongoDB()
{
return $this->mongoDB;
}
}

0 comments on commit 839493d

Please sign in to comment.