Skip to content
Browse files

Merge pull request #781 from doctrine/fix/gh759

Use null default value for ClassMetadataInfo slaveOkay
  • Loading branch information...
2 parents 58117b9 + bd6bcc0 commit 661a3b3a00a2f6e3c8502f4d9c5f93b7730fff03 @jmikola jmikola committed Jan 21, 2014
View
29 lib/Doctrine/ODM/MongoDB/DocumentManager.php
@@ -93,6 +93,13 @@ class DocumentManager implements ObjectManager
private $hydratorFactory;
/**
+ * The Proxy factory instance.
+ *
+ * @var ProxyFactory
+ */
+ private $proxyFactory;
+
+ /**
* SchemaManager instance
*
* @var SchemaManager
@@ -291,11 +298,13 @@ public function getDocumentDatabase($className)
if (isset($this->documentDatabases[$className])) {
return $this->documentDatabases[$className];
}
+
$metadata = $this->metadataFactory->getMetadataFor($className);
$db = $metadata->getDatabase();
$db = $db ? $db : $this->config->getDefaultDB();
$db = $db ? $db : 'doctrine';
$this->documentDatabases[$className] = $this->connection->selectDatabase($db);
+
return $this->documentDatabases[$className];
}
@@ -321,24 +330,26 @@ public function getDocumentCollection($className)
$className = ltrim($className, '\\');
$metadata = $this->metadataFactory->getMetadataFor($className);
- $collection = $metadata->getCollection();
+ $collectionName = $metadata->getCollection();
- if ( ! $collection) {
+ if ( ! $collectionName) {
throw MongoDBException::documentNotMappedToCollection($className);
}
- $db = $this->getDocumentDatabase($className);
if ( ! isset($this->documentCollections[$className])) {
- if ($metadata->isFile()) {
- $this->documentCollections[$className] = $db->getGridFS($collection);
- } else {
- $this->documentCollections[$className] = $db->selectCollection($collection);
- }
+ $db = $this->getDocumentDatabase($className);
+
+ $this->documentCollections[$className] = $metadata->isFile()
+ ? $db->getGridFS($collectionName)
+ : $db->selectCollection($collectionName);
}
+
$collection = $this->documentCollections[$className];
- if (isset($metadata->slaveOkay)) {
+
+ if ($metadata->slaveOkay !== null) {
$collection->setSlaveOkay($metadata->slaveOkay);
}
+
return $this->documentCollections[$className];
}
View
8 lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php
@@ -186,7 +186,7 @@ class ClassMetadataInfo implements \Doctrine\Common\Persistence\Mapping\ClassMet
/**
* READ-ONLY: Whether or not reads for this class are okay to read from a slave.
*/
- public $slaveOkay = false;
+ public $slaveOkay;
/**
* READ-ONLY: The array of indexes for the document collection.
@@ -697,13 +697,13 @@ public function setDiscriminatorValue($value)
}
/**
- * Sets whether or not reads for this class are okay to read from a slave.
+ * Sets the slaveOkay option applied to collections for this class.
*
- * @param bool $slaveOkay
+ * @param boolean|null $slaveOkay
*/
public function setSlaveOkay($slaveOkay)
{
- $this->slaveOkay = $slaveOkay;
+ $this->slaveOkay = $slaveOkay === null ? null : (boolean) $slaveOkay;
}
/**
View
68 tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php
@@ -2,6 +2,9 @@
namespace Doctrine\ODM\MongoDB\Tests;
+use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
+use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock;
+
/**
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
@@ -120,4 +123,69 @@ public function testAffectedByErrorIfClosedException($methodName)
$this->dm->$methodName(new \stdClass());
}
}
+
+ public function testGetDocumentCollectionAppliesClassMetadataSlaveOkay()
+ {
+ $cm1 = new ClassMetadataInfo('a');
+ $cm1->collection = 'a';
+
+ $cm2 = new ClassMetadataInfo('b');
+ $cm2->collection = 'b';
+ $cm2->slaveOkay = true;
+
+ $cm3 = new ClassMetadataInfo('c');
+ $cm3->collection = 'c';
+ $cm3->slaveOkay = false;
+
+ $map = array(
+ array('a', $cm1),
+ array('b', $cm2),
+ array('c', $cm3),
+ );
+
+ $metadataFactory = $this->getMockClassMetadataFactory();
+ $metadataFactory->expects($this->any())
+ ->method('getMetadataFor')
+ ->will($this->returnValueMap($map));
+
+ $coll1 = $this->getMockCollection();
+ $coll1->expects($this->never())
+ ->method('setSlaveOkay');
+
+ $coll2 = $this->getMockCollection();
+ $coll2->expects($this->once())
+ ->method('setSlaveOkay')
+ ->with(true);
+
+ $coll3 = $this->getMockCollection();
+ $coll3->expects($this->once())
+ ->method('setSlaveOkay')
+ ->with(false);
+
+ $dm = new DocumentManagerMock();
+ $dm->metadataFactory = $metadataFactory;
+ $dm->documentCollections = array(
+ 'a' => $coll1,
+ 'b' => $coll2,
+ 'c' => $coll3,
+ );
+
+ $dm->getDocumentCollection('a');
+ $dm->getDocumentCollection('b');
+ $dm->getDocumentCollection('c');
+ }
+
+ private function getMockClassMetadataFactory()
+ {
+ return $this->getMockBuilder('Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ private function getMockCollection()
+ {
+ return $this->getMockBuilder('Doctrine\MongoDB\Collection')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
}
View
38 tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php
@@ -4,17 +4,14 @@
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
+use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock;
-use Doctrine\ODM\MongoDB\Tests\Mocks\MetadataDriverMock;
class ClassMetadataFactoryTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
public function testGetMetadataForSingleClass()
{
- $driver = new MetadataDriverMock();
- $documentManager = $this->getMockDocumentManager($driver);
-
// Self-made metadata
$cm1 = new ClassMetadata('Doctrine\ODM\MongoDB\Tests\Mapping\TestDocument1');
$cm1->setCollection('group');
@@ -28,7 +25,6 @@ public function testGetMetadataForSingleClass()
// SUT
$cmf = new ClassMetadataFactoryTestSubject();
- $cmf->setDocumentManager($documentManager);
$cmf->setMetadataFor('Doctrine\ODM\MongoDB\Tests\Mapping\TestDocument1', $cm1);
// Prechecks
@@ -50,13 +46,17 @@ public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized()
require_once __DIR__."/Documents/GlobalNamespaceDocument.php";
$driver = AnnotationDriver::create(__DIR__ . '/Documents');
- $documentManager = $this->getMockDocumentManager($driver);
- $mf = $documentManager->getMetadataFactory();
- $m1 = $mf->getMetadataFor("DoctrineGlobal_Article");
- $h1 = $mf->hasMetadataFor("DoctrineGlobal_Article");
- $h2 = $mf->hasMetadataFor("\DoctrineGlobal_Article");
- $m2 = $mf->getMetadataFor("\DoctrineGlobal_Article");
+ $dm = $this->getMockDocumentManager($driver);
+
+ $cmf = new ClassMetadataFactory();
+ $cmf->setConfiguration($dm->getConfiguration());
+ $cmf->setDocumentManager($dm);
+
+ $m1 = $cmf->getMetadataFor("DoctrineGlobal_Article");
+ $h1 = $cmf->hasMetadataFor("DoctrineGlobal_Article");
+ $h2 = $cmf->hasMetadataFor("\DoctrineGlobal_Article");
+ $m2 = $cmf->getMetadataFor("\DoctrineGlobal_Article");
$this->assertNotSame($m1, $m2);
$this->assertFalse($h2);
@@ -66,23 +66,17 @@ public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized()
protected function getMockDocumentManager($driver)
{
$config = new Configuration();
-
- $config->setProxyDir(__DIR__ . '/../../../../../Proxies');
- $config->setProxyNamespace('Proxies');
- $config->setHydratorDir(__DIR__ . '/../../../../../Hydrators');
- $config->setHydratorNamespace('Hydrators');
- $config->setDefaultDB(DOCTRINE_MONGODB_DATABASE);
$config->setMetadataDriverImpl($driver);
- $conn = $this->getMockBuilder('Doctrine\MongoDB\Connection')
- ->disableOriginalConstructor()
- ->getMock();
-
$em = $this->getMockBuilder('Doctrine\Common\EventManager')
->disableOriginalConstructor()
->getMock();
- return DocumentManagerMock::create($conn, $config, $em);
+ $dm = new DocumentManagerMock();
+ $dm->config = $config;
+ $dm->eventManager = $em;
+
+ return $dm;
}
}
View
7 tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataInfoTest.php
@@ -12,6 +12,13 @@
class ClassMetadataInfoTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
+ public function testSlaveOkayDefault()
+ {
+ $cm = new ClassMetadataInfo('stdClass');
+
+ $this->assertNull($cm->slaveOkay);
+ }
+
public function testDefaultDiscriminatorField()
{
$cm = new ClassMetadataInfo('stdClass');
View
106 tests/Doctrine/ODM/MongoDB/Tests/Mocks/DocumentManagerMock.php
@@ -2,95 +2,43 @@
namespace Doctrine\ODM\MongoDB\Tests\Mocks;
-use Doctrine\ODM\MongoDB\Proxy\ProxyFactory;
-use Doctrine\MongoDB\Connection;
-use Doctrine\ODM\MongoDB\Configuration;
-use Doctrine\Common\EventManager;
-use Doctrine\ODM\MongoDB\UnitOfWork;
-use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
-use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
-use Doctrine\ODM\MongoDB\SchemaManager;
-use Doctrine\MongoDB\Collection;
-use Doctrine\MongoDB\Database;
+use Doctrine\ODM\MongoDB\DocumentManager;
-class DocumentManagerMock extends \Doctrine\ODM\MongoDB\DocumentManager
+class DocumentManagerMock extends DocumentManager
{
- private $uowMock;
- private $proxyFactoryMock;
- private $metadataFactory;
- private $schemaManager;
- private $documentCollections = array();
- private $documentDatabases = array();
- private $documentMetadatas = array();
+ private $reflectionClass;
- public function getUnitOfWork()
+ public function __construct()
{
- return isset($this->uowMock) ? $this->uowMock : parent::getUnitOfWork();
+ $this->reflectionClass = new \ReflectionClass(get_parent_class($this));
}
- public function setUnitOfWork(UnitOfWork $uow)
+ /**
+ * Set private properties declared in the DocumentManager class.
+ *
+ * @param string $name
+ * @param mixed $value
+ * @throws \ReflectionException if the property does not exist
+ */
+ public function __set($name, $value)
{
- $this->uowMock = $uow;
+ $property = $this->reflectionClass->getProperty($name);
+ $property->setAccessible(true);
+ $property->setValue($this, $value);
}
- public function setProxyFactory(ProxyFactory $proxyFactory)
+ /**
+ * Get private properties declared in the DocumentManager class.
+ *
+ * @param string $name
+ * @return mixed
+ * @throws \ReflectionException if the property does not exist
+ */
+ public function __get($name)
{
- $this->proxyFactoryMock = $proxyFactory;
- }
+ $property = $this->reflectionClass->getProperty($name);
+ $property->setAccessible(true);
- public function getProxyFactory()
- {
- return isset($this->proxyFactoryMock) ? $this->proxyFactoryMock : parent::getProxyFactory();
- }
-
- public function setMetadataFactory(ClassMetadataFactory $metadataFactory)
- {
- $this->metadataFactory = $metadataFactory;
- }
-
- public function getMetadataFactory()
- {
- return isset($this->metadataFactory) ? $this->metadataFactory : parent::getMetadataFactory();
- }
-
- public function setSchemaManager(SchemaManager $schemaManager)
- {
- $this->schemaManager = $schemaManager;
- }
-
- public function getSchemaManager()
- {
- return isset($this->schemaManager) ? $this->schemaManager : parent::getSchemaManager();
- }
-
- public function setDocumentCollection($documentName, Collection $collection)
- {
- $this->documentCollections[$documentName] = $collection;
- }
-
- public function getDocumentCollection($documentName)
- {
- return isset($this->documentCollections[$documentName]) ? $this->documentCollections[$documentName] : parent::getDocumentCollection($documentName);
- }
-
- public function setDocumentDatabase($documentName, Database $database)
- {
- $this->documentDatabases[$documentName] = $database;
- }
-
- public function getDocumentDatabase($documentName)
- {
- return isset($this->documentDatabases[$documentName]) ? $this->documentDatabases[$documentName] : parent::getDocumentDatabase($documentName);
- }
-
-
- public function setClassMetadata($documentName, ClassMetadata $metadata)
- {
- $this->documentMetadatas[$documentName] = $metadata;
- }
-
- public function getClassMetadata($documentName)
- {
- return isset($this->documentMetadatas[$documentName]) ? $this->documentMetadatas[$documentName] : parent::getClassMetadata($documentName);
+ return $property->getValue($this);
}
}
View
428 tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
@@ -2,89 +2,125 @@
namespace Doctrine\ODM\MongoDB\Tests;
-use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\SchemaManager;
+use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock;
/**
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
-class SchemaManagerTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
+class SchemaManagerTest extends \PHPUnit_Framework_TestCase
{
+ private $indexedClasses = array(
+ 'Documents\CmsAddress',
+ 'Documents\CmsArticle',
+ 'Documents\CmsComment',
+ 'Documents\CmsProduct',
+ 'Documents\Comment',
+ 'Documents\SimpleReferenceUser',
+ );
+
+ private $someNonIndexedClasses = array(
+ 'Documents\CmsGroup',
+ 'Documents\CmsPhonenumber',
+ 'Documents\CmsUser',
+ );
+
+ private $someMappedSuperclassAndEmbeddedClasses = array(
+ 'Documents/CmsContent',
+ 'Documents/CmsPage',
+ 'Documents/Issue',
+ 'Documents/Message',
+ 'Documents/Phonenumber',
+ 'Documents/Song',
+ 'Documents/SubCategory',
+ );
+
+ private $classMetadatas = array();
+ private $documentCollections = array();
+ private $documentDatabases = array();
+ private $schemaManager;
+
public function setUp()
{
- $this->uow = $this->getMockBuilder('Doctrine\ODM\MongoDB\UnitOfWork')
- ->disableOriginalConstructor()
- ->getMock();
+ $this->dm = $this->getMockDocumentManager();
- $this->uow->expects($this->any())
- ->method('getDocumentPersister')
- ->will($this->returnValue($this->getMockDocumentPersister()));
+ $cmf = new ClassMetadataFactory();
+ $cmf->setConfiguration($this->dm->getConfiguration());
+ $cmf->setDocumentManager($this->dm);
- $this->dm = $this->getMockDocumentManager();
- $this->dm->setSchemaManager(new SchemaManager($this->dm, $this->dm->getMetadataFactory()));
- $this->dm->setUnitOfWork($this->uow);
- }
+ $map = array();
- public function tearDown()
- {
- unset($this->dm, $this->uow);
+ foreach ($cmf->getAllMetadata() as $cm) {
+ $this->documentCollections[$cm->name] = $this->getMockCollection();
+ $this->documentDatabases[$cm->name] = $this->getMockDatabase();
+ $this->classMetadatas[$cm->name] = $cm;
+ }
+
+ $this->dm->unitOfWork = $this->getMockUnitOfWork();
+ $this->dm->metadataFactory = $cmf;
+ $this->dm->documentCollections = $this->documentCollections;
+ $this->dm->documentDatabases = $this->documentDatabases;
+
+ $this->schemaManager = new SchemaManager($this->dm, $cmf);
+ $this->dm->schemaManager = $this->schemaManager;
}
- /**
- * @dataProvider provideIndexedClasses
- */
- public function testEnsureIndexes(array $classes)
+ public function testEnsureIndexes()
{
- foreach ($classes as $className) {
- $collection = $this->getMockCollection();
- $collection->expects($this->once())
- ->method('ensureIndex');
- $this->dm->setDocumentCollection($className, $collection);
+ foreach ($this->documentCollections as $class => $collection) {
+ if (in_array($class, $this->indexedClasses)) {
+ $collection->expects($this->once())->method('ensureIndex');
+ } else {
+ $collection->expects($this->never())->method('ensureIndex');
+ }
}
- $this->dm->getSchemaManager()->ensureIndexes();
+ $this->schemaManager->ensureIndexes();
}
public function testEnsureDocumentIndexes()
{
- $collection = $this->getMockCollection();
- $collection->expects($this->once())
- ->method('ensureIndex');
- $this->dm->setDocumentCollection('Documents\CmsArticle', $collection);
- $this->dm->getSchemaManager()->ensureDocumentIndexes('Documents\CmsArticle');
+ foreach ($this->documentCollections as $class => $collection) {
+ if ($class === 'Documents\CmsArticle') {
+ $collection->expects($this->once())->method('ensureIndex');
+ } else {
+ $collection->expects($this->never())->method('ensureIndex');
+ }
+ }
+
+ $this->schemaManager->ensureDocumentIndexes('Documents\CmsArticle');
}
public function testEnsureDocumentIndexesWithTwoLevelInheritance()
{
- $collection = $this->getMockCollection();
- $collection->expects($this->once())
- ->method('ensureIndex');
- $this->dm->setDocumentCollection('Documents\CmsProduct', $collection);
- $this->dm->getSchemaManager()->ensureDocumentIndexes('Documents\CmsProduct');
+ $collection = $this->documentCollections['Documents\CmsProduct'];
+ $collection->expects($this->once())->method('ensureIndex');
+
+ $this->schemaManager->ensureDocumentIndexes('Documents\CmsProduct');
}
public function testEnsureDocumentIndexesWithTimeout()
{
- $collection = $this->getMockCollection();
+ $collection = $this->documentCollections['Documents\CmsArticle'];
$collection->expects($this->once())
->method('ensureIndex')
->with($this->anything(), $this->callback(function($o) {
return isset($o['timeout']) && $o['timeout'] === 10000;
}));
- $this->dm->setDocumentCollection('Documents\CmsArticle', $collection);
- $this->dm->getSchemaManager()->ensureDocumentIndexes('Documents\CmsArticle', 10000);
+
+ $this->schemaManager->ensureDocumentIndexes('Documents\CmsArticle', 10000);
}
public function testUpdateDocumentIndexesShouldCreateMappedIndexes()
{
- $database = $this->getMockDatabase();
+ $database = $this->documentDatabases['Documents\CmsArticle'];
$database->expects($this->never())
->method('command');
- $collection = $this->getMockCollection();
+ $collection = $this->documentCollections['Documents\CmsArticle'];
$collection->expects($this->once())
->method('getIndexInfo')
->will($this->returnValue(array()));
@@ -94,20 +130,19 @@ public function testUpdateDocumentIndexesShouldCreateMappedIndexes()
->method('getDatabase')
->will($this->returnValue($database));
- $this->dm->setDocumentCollection('Documents\CmsArticle', $collection);
- $this->dm->getSchemaManager()->updateDocumentIndexes('Documents\CmsArticle');
+ $this->schemaManager->updateDocumentIndexes('Documents\CmsArticle');
}
public function testUpdateDocumentIndexesShouldDeleteUnmappedIndexesBeforeCreatingMappedIndexes()
{
- $database = $this->getMockDatabase();
+ $database = $this->documentDatabases['Documents\CmsArticle'];
$database->expects($this->once())
->method('command')
->with($this->callback(function($c) {
return array_key_exists('deleteIndexes', $c);
}));
- $collection = $this->getMockCollection();
+ $collection = $this->documentCollections['Documents\CmsArticle'];
$collection->expects($this->once())
->method('getIndexInfo')
->will($this->returnValue(array(array(
@@ -121,245 +156,143 @@ public function testUpdateDocumentIndexesShouldDeleteUnmappedIndexesBeforeCreati
->method('getDatabase')
->will($this->returnValue($database));
- $this->dm->setDocumentCollection('Documents\CmsArticle', $collection);
- $this->dm->getSchemaManager()->updateDocumentIndexes('Documents\CmsArticle');
+ $this->schemaManager->updateDocumentIndexes('Documents\CmsArticle');
}
- /**
- * @dataProvider provideIndexedClasses
- */
- public function testDeleteIndexes(array $classes)
+ public function testDeleteIndexes()
{
- $metadatas = array();
- foreach ($classes as $className) {
- $collection = $this->getMockCollection();
- $collection->expects($this->once())
- ->method('deleteIndexes');
- $metadatas[] = (object) array('name' => $className, 'isMappedSuperclass' => false, 'isEmbeddedDocument' => false);
-
- $this->dm->setDocumentCollection($className, $collection);
+ foreach ($this->documentCollections as $class => $collection) {
+ if (in_array($class, $this->indexedClasses)) {
+ $collection->expects($this->once())->method('deleteIndexes');
+ } elseif (in_array($class, $this->someMappedSuperclassAndEmbeddedClasses)) {
+ $collection->expects($this->never())->method('deleteIndexes');
+ }
}
- $metadataFactory = $this->getMockMetadataFactory();
- $metadataFactory->expects($this->once())
- ->method('getAllMetadata')
- ->will($this->returnValue($metadatas));
-
- $this->dm->setMetadataFactory($metadataFactory);
- $this->dm->setSchemaManager(new SchemaManager($this->dm, $metadataFactory));
-
- $this->dm->getSchemaManager()->deleteIndexes();
+ $this->schemaManager->deleteIndexes();
}
public function testDeleteDocumentIndexes()
{
- $collection = $this->getMockCollection();
- $collection->expects($this->once())
- ->method('deleteIndexes');
- $this->dm->setDocumentCollection('Documents\CmsArticle', $collection);
+ foreach ($this->documentCollections as $class => $collection) {
+ if ($class === 'Documents\CmsArticle') {
+ $collection->expects($this->once())->method('deleteIndexes');
+ } else {
+ $collection->expects($this->never())->method('deleteIndexes');
+ }
+ }
- $this->dm->getSchemaManager()->deleteDocumentIndexes('Documents\CmsArticle');
+ $this->schemaManager->deleteDocumentIndexes('Documents\CmsArticle');
}
public function testCreateDocumentCollection()
{
- $className = 'Documents\CmsArticle';
-
- $classMetadata = $this->getMockClassMetadata($className);
- $classMetadata->expects($this->any())
- ->method('isFile')
- ->will($this->returnValue(false));
- $classMetadata->expects($this->once())
- ->method('getCollection')
- ->will($this->returnValue('cms_articles'));
- $classMetadata->expects($this->once())
- ->method('getCollectionCapped')
- ->will($this->returnValue(true));
- $classMetadata->expects($this->once())
- ->method('getCollectionSize')
- ->will($this->returnValue(1048576));
- $classMetadata->expects($this->once())
- ->method('getCollectionMax')
- ->will($this->returnValue(32));
-
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->once())
+ $cm = $this->classMetadatas['Documents\CmsArticle'];
+ $cm->collectionCapped = true;
+ $cm->collectionSize = 1048576;
+ $cm->collectionMax = 32;
+
+ $database = $this->documentDatabases['Documents\CmsArticle'];
+ $database->expects($this->once())
->method('createCollection')
- ->with('cms_articles', true, 1048576, 32);
+ ->with('CmsArticle', true, 1048576, 32);
- $this->dm->setDocumentDatabase($className, $documentDatabase);
- $this->dm->setClassMetadata($className, $classMetadata);
- $this->dm->getSchemaManager()->createDocumentCollection($className);
+ $this->schemaManager->createDocumentCollection('Documents\CmsArticle');
}
public function testCreateGridFSCollection()
{
- $className = 'Documents\File';
-
- $classMetadata = $this->getMockClassMetadata($className);
- $classMetadata->expects($this->any())
- ->method('isFile')
- ->will($this->returnValue(true));
- $classMetadata->expects($this->any())
- ->method('getCollection')
- ->will($this->returnValue('fs'));
-
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->at(0))
+ $database = $this->documentDatabases['Documents\File'];
+ $database->expects($this->at(0))
->method('createCollection')
- ->with('fs.files');
- $documentDatabase->expects($this->at(1))
+ ->with('File.files');
+ $database->expects($this->at(1))
->method('createCollection')
- ->with('fs.chunks');
+ ->with('File.chunks');
- $this->dm->setDocumentDatabase($className, $documentDatabase);
- $this->dm->setClassMetadata($className, $classMetadata);
- $this->dm->getSchemaManager()->createDocumentCollection($className);
+ $this->schemaManager->createDocumentCollection('Documents\File');
}
- /**
- * @dataProvider provideIndexedClasses
- */
- public function testCreateCollections(array $classes)
+ public function testCreateCollections()
{
- $metadatas = array();
- foreach ($classes as $className) {
- $metadatas[] = (object) array('name' => $className, 'isMappedSuperclass' => false, 'isEmbeddedDocument' => false);
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->once())
- ->method('createCollection');
- $this->dm->setDocumentDatabase($className, $documentDatabase);
+ foreach ($this->documentDatabases as $class => $database) {
+ if (in_array($class, $this->indexedClasses + $this->someNonIndexedClasses)) {
+ $database->expects($this->once())->method('createCollection');
+ } elseif (in_array($class, $this->someMappedSuperclassAndEmbeddedClasses)) {
+ $database->expects($this->never())->method('createCollection');
+ }
}
- $metadataFactory = $this->getMockMetadataFactory();
- $metadataFactory->expects($this->once())
- ->method('getAllMetadata')
- ->will($this->returnValue($metadatas));
-
- $this->dm->setMetadataFactory($metadataFactory);
- $this->dm->setSchemaManager(new \Doctrine\ODM\MongoDB\SchemaManager($this->dm, $metadataFactory));
-
- $this->dm->getSchemaManager()->createCollections();
+ $this->schemaManager->createCollections();
}
- /**
- * @dataProvider provideIndexedClasses
- */
- public function testDropCollections(array $classes)
+ public function testDropCollections()
{
- $metadatas = array();
- foreach ($classes as $className) {
- $metadata = $this->getMockClassMetadata($className);
- $metadata->expects($this->once())
- ->method('getCollection')
- ->will($this->returnValue($className));
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->once())
- ->method('dropCollection')
- ->with($className);
- $this->dm->setDocumentDatabase($className, $documentDatabase);
- $this->dm->setClassMetadata($className, $metadata);
- $metadatas[] = $metadata;
+ foreach ($this->documentDatabases as $class => $database) {
+ if (in_array($class, $this->indexedClasses + $this->someNonIndexedClasses)) {
+ $database->expects($this->once())
+ ->method('dropCollection')
+ ->with($this->classMetadatas[$class]->collection);
+ } elseif (in_array($class, $this->someMappedSuperclassAndEmbeddedClasses)) {
+ $database->expects($this->never())->method('dropCollection');
+ }
}
- $metadataFactory = $this->getMockMetadataFactory();
- $metadataFactory->expects($this->once())
- ->method('getAllMetadata')
- ->will($this->returnValue($metadatas));
-
- $this->dm->setMetadataFactory($metadataFactory);
- $this->dm->setSchemaManager(new \Doctrine\ODM\MongoDB\SchemaManager($this->dm, $metadataFactory));
-
- $this->dm->getSchemaManager()->dropCollections();
+ $this->schemaManager->dropCollections();
}
public function testDropDocumentCollection()
{
- $className = 'Documents\CmsArticle';
- $collectionName = 'cms_articles';
- $classMetadata = $this->getMockClassMetadata($className);
- $classMetadata->expects($this->once())
- ->method('getCollection')
- ->will($this->returnValue($collectionName));
-
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->once())
- ->method('dropCollection')
- ->with($collectionName);
- $this->dm->setDocumentDatabase($className, $documentDatabase);
-
- $this->dm->setClassMetadata($className, $classMetadata);
-
- $this->dm->getSchemaManager()->dropDocumentCollection($className);
+ foreach ($this->documentDatabases as $class => $database) {
+ if ($class === 'Documents\CmsArticle') {
+ $database->expects($this->once())
+ ->method('dropCollection')
+ ->with($this->classMetadatas[$class]->collection);
+ } else {
+ $database->expects($this->never())->method('dropCollection');
+ }
+ }
+
+ $this->schemaManager->dropDocumentCollection('Documents\CmsArticle');
}
public function testCreateDocumentDatabase()
{
- $className = 'Documents\CmsArticle';
- $dbName = 'test_db';
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->once())
- ->method('execute');
- $this->dm->setDocumentDatabase($className, $documentDatabase);
-
- $this->dm->getSchemaManager()->createDocumentDatabase($className);
+ foreach ($this->documentDatabases as $class => $database) {
+ if ($class === 'Documents\CmsArticle') {
+ $database->expects($this->once())->method('execute');
+ } else {
+ $database->expects($this->never())->method('execute');
+ }
+ }
+
+ $this->schemaManager->createDocumentDatabase('Documents\CmsArticle');
}
public function testDropDocumentDatabase()
{
- $className = 'Documents\CmsArticle';
- $dbName = 'test_db';
-
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->once())
- ->method('drop');
- $this->dm->setDocumentDatabase($className, $documentDatabase);
+ foreach ($this->documentDatabases as $class => $database) {
+ if ($class === 'Documents\CmsArticle') {
+ $database->expects($this->once())->method('drop');
+ } else {
+ $database->expects($this->never())->method('drop');
+ }
+ }
- $this->dm->getSchemaManager()->dropDocumentDatabase($className);
+ $this->dm->getSchemaManager()->dropDocumentDatabase('Documents\CmsArticle');
}
- /**
- * @dataProvider provideIndexedClasses
- */
- public function testDropDatabases(array $classes)
+ public function testDropDatabases()
{
- $metadatas = array();
- foreach ($classes as $className) {
- $documentDatabase = $this->getMockDatabase();
- $documentDatabase->expects($this->once())
- ->method('drop');
- $this->dm->setDocumentDatabase($className, $documentDatabase);
- $metadatas[] = (object) array('name' => $className, 'isMappedSuperclass' => false, 'isEmbeddedDocument' => false);
+ foreach ($this->documentDatabases as $class => $database) {
+ if (in_array($class, $this->indexedClasses + $this->someNonIndexedClasses)) {
+ $database->expects($this->once())->method('drop');
+ } elseif (in_array($class, $this->someMappedSuperclassAndEmbeddedClasses)) {
+ $database->expects($this->never())->method('drop');
+ }
}
- $metadataFactory = $this->getMockMetadataFactory();
- $metadataFactory->expects($this->once())
- ->method('getAllMetadata')
- ->will($this->returnValue($metadatas));
-
- $this->dm->setMetadataFactory($metadataFactory);
- $this->dm->setSchemaManager(new \Doctrine\ODM\MongoDB\SchemaManager($this->dm, $metadataFactory));
-
- $this->dm->getSchemaManager()->dropDatabases();
- }
-
- public function provideIndexedClasses()
- {
- return array(array(array(
- 'Documents\SimpleReferenceUser',
- 'Documents\Comment',
- 'Documents\CmsArticle',
- 'Documents\CmsAddress',
- 'Documents\CmsComment',
- 'Documents\CmsProduct'
- )));
- }
-
- private function getMockClassMetadata($className)
- {
- return $this->getMockBuilder('Doctrine\ODM\MongoDB\Mapping\ClassMetadata')
- ->setConstructorArgs(array($className))
- ->getMock();
+ $this->schemaManager->dropDatabases();
}
private function getMockCollection()
@@ -379,26 +312,20 @@ private function getMockDatabase()
private function getMockDocumentManager()
{
$config = new Configuration();
-
- $config->setProxyDir(__DIR__ . '/../../../../../Proxies');
- $config->setProxyNamespace('Proxies');
- $config->setHydratorDir(__DIR__ . '/../../../../../Hydrators');
- $config->setHydratorNamespace('Hydrators');
- $config->setDefaultDB(DOCTRINE_MONGODB_DATABASE);
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/../../../../Documents'));
- $conn = $this->getMockBuilder('Doctrine\MongoDB\Connection')
- ->disableOriginalConstructor()
- ->getMock();
-
$em = $this->getMockBuilder('Doctrine\Common\EventManager')
->disableOriginalConstructor()
->getMock();
- return DocumentManagerMock::create($conn, $config, $em);
+ $dm = new DocumentManagerMock();
+ $dm->eventManager = $em;
+ $dm->config = $config;
+
+ return $dm;
}
- private function getMockDocumentPersister()
+ private function getMockUnitOfWork()
{
$documentPersister = $this->getMockBuilder('Doctrine\ODM\MongoDB\Persisters\DocumentPersister')
->disableOriginalConstructor()
@@ -408,13 +335,14 @@ private function getMockDocumentPersister()
->method('prepareFieldName')
->will($this->returnArgument(0));
- return $documentPersister;
- }
-
- private function getMockMetadataFactory()
- {
- return $this->getMockBuilder('Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory')
+ $uow = $this->getMockBuilder('Doctrine\ODM\MongoDB\UnitOfWork')
->disableOriginalConstructor()
->getMock();
+
+ $uow->expects($this->any())
+ ->method('getDocumentPersister')
+ ->will($this->returnValue($documentPersister));
+
+ return $uow;
}
}
View
1 tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php
@@ -9,7 +9,6 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder;
-use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock;
use Doctrine\ODM\MongoDB\Tests\Mocks\ConnectionMock;
use Doctrine\ODM\MongoDB\Tests\Mocks\UnitOfWorkMock;
use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentPersisterMock;

0 comments on commit 661a3b3

Please sign in to comment.
Something went wrong with that request. Please try again.