Created
October 20, 2011 07:23
-
-
Save madapaja/1300603 to your computer and use it in GitHub Desktop.
Base testcase class for Doctrine MongoDB ODM testcases
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
<?php | |
namespace Madapaja\ODM\MongoDB\Tests; | |
use Doctrine\ODM\MongoDB\Configuration; | |
/** | |
* Base testcase class for MongoDB ODM testcases. | |
* | |
* Usage: | |
* | |
* namespace Acme\ExampleBundle\Tests\Repository; | |
* | |
* use Madapaja\ODM\MongoDB\Tests\OdmTestCase; | |
* use Doctrine\Common\Annotations\AnnotationReader; | |
* use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; | |
* | |
* class BlogRepositoryTest extends OdmTestCase | |
* { | |
* private $_dm; | |
* | |
* protected function setUp() | |
* { | |
* $reader = new AnnotationReader(); | |
* $reader->setIgnoreNotImportedAnnotations(true); | |
* $reader->setEnableParsePhpImports(true); | |
* | |
* $metadataDriver = new AnnotationDriver( | |
* $reader, | |
* // provide the namespace of the entities you want to tests | |
* 'Acme\\ExampleBundle\\Document' | |
* ); | |
* | |
* $this->_dm = $this->_getTestDocumentManager(); | |
* | |
* $this->_dm->getConfiguration() | |
* ->setMetadataDriverImpl($metadataDriver); | |
* | |
* // allows you to use the <Bundle:Name>:<DocumentName> syntax | |
* $this->_dm->getConfiguration()->setDocumentNamespaces(array( | |
* 'AcmeExampleBundle' => 'Acme\\ExampleBundle\\Document' | |
* )); | |
* } | |
* | |
* public function testCreateSearchByNameQueryBuilder() | |
* { | |
* $queryBuilder = $this->_dm->getRepository('AcmeExampleBundle:Blog') | |
* ->createSearchByNameQueryBuilder('foo'); | |
* | |
* $this->assertEquals(\Doctrine\MongoDB\Query\Query::TYPE_FIND, $queryBuilder->getQuery()->getType()); | |
* $this->assertEquals(1, count($queryBuilder->getQuery()->debug())); | |
* $this->assertEquals(array('name' => new \MongoRegex('/foo/i')), $queryBuilder->getQuery()->debug()); | |
* } | |
* } | |
*/ | |
class OdmTestCase extends \PHPUnit_Framework_TestCase | |
{ | |
/** The metadata cache that is shared between all ORM tests (except functional tests). */ | |
private static $_metadataCacheImpl = null; | |
/** | |
* Creates an DocumentManager for testing purposes. | |
* | |
* @return Doctrine\ODM\MongoDB\DocumentManager | |
*/ | |
protected function _getTestDocumentManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true) | |
{ | |
$config = new Configuration(); | |
if ($withSharedMetadata) { | |
$config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl()); | |
} else { | |
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); | |
} | |
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver()); | |
$config->setProxyDir(__DIR__ . '/Proxies'); | |
$config->setProxyNamespace('Proxies'); | |
$config->setHydratorDir(__DIR__ . '/Hydrators'); | |
$config->setHydratorNamespace('Hydrators'); | |
$eventManager = new \Doctrine\Common\EventManager(); | |
$mongoMock = new \Doctrine\ODM\MongoDB\Tests\Mocks\ConnectionMock(); | |
return \Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock::create($mongoMock, $config, $eventManager); | |
} | |
private static function getSharedMetadataCacheImpl() | |
{ | |
if (self::$_metadataCacheImpl === null) { | |
self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache; | |
} | |
return self::$_metadataCacheImpl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doctrine MongoDB ODM 向けベーステストケースクラスです。
http://symfony.com/doc/current/cookbook/testing/doctrine.html#unit-testing の、
OrmTestCase と同様にODMリポジトリのテストを行う事が可能です。