Permalink
Browse files

Rewrite DocumentManagerMock to use reflection to expose privates

ClassMetadataFactoryTest and SchemaManagerTest required refactoring for these changes.
  • Loading branch information...
1 parent 3f4c371 commit 0c1429f0aa6262fb051ab60e416d0031914cdb7a @jmikola jmikola committed Jan 21, 2014
@@ -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;
}
}
@@ -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);
}
}
Oops, something went wrong.

0 comments on commit 0c1429f

Please sign in to comment.