Skip to content

Instantly share code, notes, and snippets.

@madapaja
Created October 20, 2011 07:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madapaja/1300603 to your computer and use it in GitHub Desktop.
Save madapaja/1300603 to your computer and use it in GitHub Desktop.
Base testcase class for Doctrine MongoDB ODM testcases
<?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;
}
}
@madapaja
Copy link
Author

Doctrine MongoDB ODM 向けベーステストケースクラスです。

http://symfony.com/doc/current/cookbook/testing/doctrine.html#unit-testing の、
OrmTestCase と同様にODMリポジトリのテストを行う事が可能です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment