This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make LoggableDatabase::createCollection() API consistent with Database
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
Showing
2 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |