General work on DoctrineMongoDBBundle to bring it more up to speed with DoctrineBundle. Added missing console commands, proxy cache warmer and hydrator cache warmer.

This commit is contained in:
Jonathan H. Wage 2011-02-12 21:42:05 -06:00 committed by Fabien Potencier
parent eafd391c17
commit b716b707ba
13 changed files with 777 additions and 3 deletions

View File

@ -0,0 +1,76 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
/**
* The hydrator generator cache warmer generates all document hydrators.
*
* In the process of generating hydrators the cache for all the metadata is primed also,
* since this information is necessary to build the hydrators in the first place.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class HydratorCacheWarmer implements CacheWarmerInterface
{
/**
* @var Container
*/
private $container;
/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* This cache warmer is not optional, without hydrators fatal error occour!
*
* @return false
*/
public function isOptional()
{
return false;
}
public function warmUp($cacheDir)
{
// we need the directory no matter the hydrator cache generation strategy.
$hydratorCacheDir = $this->container->getParameter('doctrine.odm.mongodb.hydrator_dir');
if (!file_exists($hydratorCacheDir)) {
if (false === @mkdir($hydratorCacheDir, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create the Doctrine Hydrator directory (%s)', dirname($hydratorCacheDir)));
}
} else if (!is_writable($hydratorCacheDir)) {
throw new \RuntimeException(sprintf('Doctrine Hydrator directory (%s) is not writeable for the current system user.', $hydratorCacheDir));
}
// if hydrators are autogenerated we don't need to generate them in the cache warmer.
if ($this->container->getParameter('doctrine.odm.mongodb.auto_generate_hydrator_classes') === true) {
return;
}
$documentManagers = $this->container->getParameter('doctrine.odm.mongodb.document_managers');
foreach ($documentManagers AS $documentManagerName) {
$dm = $this->container->get(sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManagerName));
/* @var $dm Doctrine\ODM\MongoDB\DocumentManager */
$classes = $dm->getMetadataFactory()->getAllMetadata();
$dm->getHydratorFactory()->generateHydratorClasses($classes);
}
}
}

View File

@ -0,0 +1,76 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
/**
* The proxy generator cache warmer generates all document proxies.
*
* In the process of generating proxies the cache for all the metadata is primed also,
* since this information is necessary to build the proxies in the first place.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class ProxyCacheWarmer implements CacheWarmerInterface
{
/**
* @var Container
*/
private $container;
/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* This cache warmer is not optional, without proxies fatal error occour!
*
* @return false
*/
public function isOptional()
{
return false;
}
public function warmUp($cacheDir)
{
// we need the directory no matter the proxy cache generation strategy.
$proxyCacheDir = $this->container->getParameter('doctrine.odm.mongodb.proxy_dir');
if (!file_exists($proxyCacheDir)) {
if (false === @mkdir($proxyCacheDir, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create the Doctrine Proxy directory (%s)', dirname($proxyCacheDir)));
}
} else if (!is_writable($proxyCacheDir)) {
throw new \RuntimeException(sprintf('Doctrine Proxy directory (%s) is not writeable for the current system user.', $proxyCacheDir));
}
// if proxys are autogenerated we don't need to generate them in the cache warmer.
if ($this->container->getParameter('doctrine.odm.mongodb.auto_generate_proxy_classes') === true) {
return;
}
$documentManagers = $this->container->getParameter('doctrine.odm.mongodb.document_managers');
foreach ($documentManagers AS $documentManagerName) {
$dm = $this->container->get(sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManagerName));
/* @var $dm Doctrine\ODM\MongoDB\DocumentManager */
$classes = $dm->getMetadataFactory()->getAllMetadata();
$dm->getProxyFactory()->generateProxyClasses($classes);
}
}
}

View File

@ -14,6 +14,9 @@ namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\ODM\MongoDB\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ODM\MongoDB\Tools\DocumentGenerator;
/**
* Base class for Doctrine ODM console commands to extend.
@ -35,4 +38,83 @@ abstract class DoctrineODMCommand extends Command
$helperSet = $application->getHelperSet();
$helperSet->set(new DocumentManagerHelper($dm), 'dm');
}
}
protected function getDocumentGenerator()
{
$documentGenerator = new DocumentGenerator();
$documentGenerator->setAnnotationPrefix('mongodb:');
$documentGenerator->setGenerateAnnotations(false);
$documentGenerator->setGenerateStubMethods(true);
$documentGenerator->setRegenerateDocumentIfExists(false);
$documentGenerator->setUpdateDocumentIfExists(true);
$documentGenerator->setNumSpaces(4);
return $documentGenerator;
}
protected function getDoctrineDocumentManagers()
{
$documentManagerNames = $this->container->getParameter('doctrine.odm.mongodb.document_managers');
$documentManagers = array();
foreach ($documentManagerNames AS $documentManagerName) {
$dm = $this->container->get(sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManagerName));
$documentManagers[] = $dm;
}
return $documentManagers;
}
protected function getBundleMetadatas(Bundle $bundle)
{
$namespace = $bundle->getNamespace();
$bundleMetadatas = array();
$documentManagers = $this->getDoctrineDocumentManagers();
foreach ($documentManagers as $key => $dm) {
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setDocumentManager($dm);
$cmf->setConfiguration($dm->getConfiguration());
$metadatas = $cmf->getAllMetadata();
foreach ($metadatas as $metadata) {
if (strpos($metadata->name, $namespace) === 0) {
$bundleMetadatas[$metadata->name] = $metadata;
}
}
}
return $bundleMetadatas;
}
protected function findBundle($bundleName)
{
$foundBundle = false;
foreach ($this->application->getKernel()->getBundles() as $bundle) {
/* @var $bundle Bundle */
if (strtolower($bundleName) == strtolower($bundle->getName())) {
$foundBundle = $bundle;
break;
}
}
if (!$foundBundle) {
throw new \InvalidArgumentException("No bundle " . $bundleName . " was found.");
}
return $foundBundle;
}
/**
* Transform classname to a path $foundBundle substract it to get the destination
*
* @param Bundle $bundle
* @return string
*/
protected function findBasePathForBundle($bundle)
{
$path = str_replace('\\', '/', $bundle->getNamespace());
$destination = str_replace('/'.$path, "", $bundle->getPath(), $c);
if ($c != 1) {
throw new \RuntimeException("Something went terribly wrong.");
}
return $destination;
}
}

View File

@ -0,0 +1,80 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
/**
* Generate document classes from mapping information
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateDocumentsDoctrineODMCommand extends DoctrineODMCommand
{
protected function configure()
{
$this
->setName('doctrine:mongodb:generate:documents')
->setDescription('Generate document classes and method stubs from your mapping information.')
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the document or documents in.')
->addOption('document', null, InputOption::VALUE_OPTIONAL, 'The document class to initialize (shortname without namespace).')
->setHelp(<<<EOT
The <info>doctrine:generate:documents</info> command generates document classes and method stubs from your mapping information:
You have to limit generation of documents to an individual bundle:
<info>./app/console doctrine:generate:documents MyCustomBundle</info>
Alternatively, you can limit generation to a single document within a bundle:
<info>./app/console doctrine:generate:documents "MyCustomBundle" --document="User"</info>
You have to specifiy the shortname (without namespace) of the document you want to filter for.
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$bundleName = $input->getArgument('bundle');
$filterDocument = $input->getOption('document');
$foundBundle = $this->findBundle($bundleName);
if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
$output->writeln(sprintf('Generating documents for "<info>%s</info>"', $foundBundle->getName()));
$documentGenerator = $this->getDocumentGenerator();
foreach ($metadatas as $metadata) {
if ($filterDocument && $metadata->reflClass->getShortName() == $filterDocument) {
continue;
}
if (strpos($metadata->name, $foundBundle->getNamespace()) === false) {
throw new \RuntimeException(
"Document " . $metadata->name . " and bundle don't have a commont namespace, ".
"generation failed because the target directory cannot be detected.");
}
$output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name));
$documentGenerator->generate(array($metadata), $this->findBasePathForBundle($foundBundle));
}
} else {
throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped documents.");
}
}
}

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand;
/**
* Generate the Doctrine ORM document hydrators to your cache directory.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateHydratorsDoctrineODMCommand extends GenerateHydratorsCommand
{
protected function configure()
{
parent::configure();
$this
->setName('doctrine:mongodb:generate:hydrators')
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.')
->setHelp(<<<EOT
The <info>doctrine:generate:hydrators</info> command generates hydrator classes for your documents:
<info>./app/console doctrine:generate:hydrators</info>
You can specify the document manager you want to generate the hydrators for:
<info>./app/console doctrine:generate:hydrators --dm=name</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineODMCommand::setApplicationDocumentManager($this->application, $input->getOption('dm'));
return parent::execute($input, $output);
}
}

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand;
/**
* Generate the Doctrine ORM document proxies to your cache directory.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateProxiesDoctrineODMCommand extends GenerateProxiesCommand
{
protected function configure()
{
parent::configure();
$this
->setName('doctrine:mongodb:generate:proxies')
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.')
->setHelp(<<<EOT
The <info>doctrine:generate:proxies</info> command generates proxy classes for your default document manager:
<info>./app/console doctrine:generate:proxies</info>
You can specify the document manager you want to generate the proxies for:
<info>./app/console doctrine:generate:proxies --dm=name</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineODMCommand::setApplicationDocumentManager($this->application, $input->getOption('dm'));
return parent::execute($input, $output);
}
}

View File

@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Doctrine\ODM\MongoDB\Tools\DocumentRepositoryGenerator;
/**
* Command to generate repository classes for mapping information.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateRepositoriesDoctrineODMCommand extends DoctrineODMCommand
{
protected function configure()
{
$this
->setName('doctrine:mongodb:generate:repositories')
->setDescription('Generate repository classes from your mapping information.')
->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to initialize the repositories in.')
->addOption('document', null, InputOption::VALUE_OPTIONAL, 'The document class to generate the repository for (shortname without namespace).')
->setHelp(<<<EOT
The <info>doctrine:generate:repositories</info> command generates the configured document repository classes from your mapping information:
<info>./app/console doctrine:generate:repositories</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$bundleName = $input->getArgument('bundle');
$filterDocument = $input->getOption('document');
$foundBundle = $this->findBundle($bundleName);
if ($metadatas = $this->getBundleMetadatas($foundBundle)) {
$output->writeln(sprintf('Generating document repositories for "<info>%s</info>"', $foundBundle->getName()));
$generator = new DocumentRepositoryGenerator();
foreach ($metadatas as $metadata) {
if ($filterDocument && $filterDocument !== $metadata->reflClass->getShortname()) {
continue;
}
if ($metadata->customRepositoryClassName) {
if (strpos($metadata->customRepositoryClassName, $foundBundle->getName()) === false) {
throw new \RuntimeException(
"Repository " . $metadata->customRepositoryClassName . " and bundle don't have a commont namespace, ".
"generation failed because the target directory cannot be detected.");
}
$output->writeln(sprintf(' > <info>OK</info> generating <comment>%s</comment>', $metadata->customRepositoryClassName));
$generator->writeDocumentRepositoryClass($metadata->customRepositoryClassName, $this->findBasePathForBundle($foundBundle));
} else {
$output->writeln(sprintf(' > <error>SKIP</error> no custom repository for <comment>%s</comment>', $metadata->name));
}
}
} else {
throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped documents.");
}
}
}

View File

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand;
/**
* Execute a Doctrine MongoDB ODM query and output the results.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class QueryDoctrineODMCommand extends QueryCommand
{
protected function configure()
{
parent::configure();
$this
->setName('doctrine:mongodb:query')
->addOption('dm', null, InputOption::VALUE_OPTIONAL, 'The document manager to use for this command.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineODMCommand::setApplicationDocumentManager($this->application, $input->getOption('dm'));
return parent::execute($input, $output);
}
}

View File

@ -105,6 +105,7 @@ class DoctrineMongoDBExtension extends AbstractDoctrineExtension
$documentManager['name'] = $name;
$this->loadDocumentManager($documentManager, $container);
}
$container->setParameter('doctrine.odm.mongodb.document_managers', array_keys($documentManagers));
}
/**

View File

@ -50,7 +50,13 @@
<!-- security/user -->
<parameter key="security.user.provider.document.class">Symfony\Bundle\DoctrineMongoDBBundle\Security\DocumentUserProvider</parameter>
<!-- proxy cache warmer -->
<parameter key="doctrine.odm.mongodb.proxy_cache_warmer.class">Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer\ProxyCacheWarmer</parameter>
<!-- hydrator cache warmer -->
<parameter key="doctrine.odm.mongodb.hydrator_cache_warmer.class">Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer\HydratorCacheWarmer</parameter>
<!-- validator -->
<parameter key="doctrine_odm.mongodb.validator.unique.class">Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints\UniqueValidator</parameter>
</parameters>
@ -88,7 +94,17 @@
</service>
<service id="security.user.document_manager" alias="doctrine.odm.mongodb.default_document_manager" />
<service id="doctrine.odm.mongodb.proxy_cache_warmer" class="%doctrine.odm.mongodb.proxy_cache_warmer.class%" public="false">
<tag name="kernel.cache_warmer" />
<argument type="service" id="service_container" />
</service>
<service id="doctrine.odm.mongodb.hydrator_cache_warmer" class="%doctrine.odm.mongodb.hydrator_cache_warmer.class%" public="false">
<tag name="kernel.cache_warmer" />
<argument type="service" id="service_container" />
</service>
<!-- validator -->
<service id="doctrine_odm.mongodb.validator.unique" class="%doctrine_odm.mongodb.validator.unique.class%">
<tag name="validator.constraint_validator" alias="doctrine_odm.mongodb.unique" />

View File

@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Tests\CacheWarmer;
use Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer\HydratorCacheWarmer;
class HydratorCacheWarmerTest extends \Symfony\Bundle\DoctrineMongoDBBundle\Tests\TestCase
{
/**
* This is not necessarily a good test, it doesnt generate any hydrators
* because there are none in the AnnotationsBundle. However that is
* rather a task of doctrine to test. We touch the lines here and
* verify that the container is called correctly for the relevant information.
*
* @group DoctrineODMMongoDBHydrator
*/
public function testWarmCache()
{
$testManager = $this->createTestDocumentManager(array(
__DIR__ . "/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Document")
);
$container = $this->getMock('Symfony\Component\DependencyInjection\Container');
$container->expects($this->at(0))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.hydrator_dir'))
->will($this->returnValue(sys_get_temp_dir()));
$container->expects($this->at(1))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.auto_generate_hydrator_classes'))
->will($this->returnValue( false ));
$container->expects($this->at(2))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.document_managers'))
->will($this->returnValue(array('default', 'foo')));
$container->expects($this->at(3))
->method('get')
->with($this->equalTo('doctrine.odm.mongodb.default_document_manager'))
->will($this->returnValue($testManager));
$container->expects($this->at(4))
->method('get')
->with($this->equalTo('doctrine.odm.mongodb.foo_document_manager'))
->will($this->returnValue($testManager));
$cacheWarmer = new HydratorCacheWarmer($container);
$cacheWarmer->warmUp(sys_get_temp_dir());
}
/**
* @group DoctrineODMMongoDBHydrator
*/
public function testSkipWhenHydratorsAreAutoGenerated()
{
$testManager = $this->createTestDocumentManager(array(
__DIR__ . "/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Document")
);
$container = $this->getMock('Symfony\Component\DependencyInjection\Container');
$container->expects($this->at(0))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.hydrator_dir'))
->will($this->returnValue(sys_get_temp_dir()));
$container->expects($this->at(1))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.auto_generate_hydrator_classes'))
->will($this->returnValue( true ));
$container->expects($this->at(2))
->method('getParameter')
->with($this->equalTo('assertion'))
->will($this->returnValue( true ));
$cacheWarmer = new HydratorCacheWarmer($container);
$cacheWarmer->warmUp(sys_get_temp_dir());
$container->getParameter('assertion'); // check that the assertion is really the third call.
}
/**
* @group DoctrineODMMongoDBHydrator
*/
public function testHydratorCacheWarmingIsNotOptional()
{
$container = $this->getMock('Symfony\Component\DependencyInjection\Container');
$cacheWarmer = new HydratorCacheWarmer($container);
$this->assertFalse($cacheWarmer->isOptional());
}
}

View File

@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Tests\CacheWarmer;
use Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer\ProxyCacheWarmer;
class ProxyCacheWarmerTest extends \Symfony\Bundle\DoctrineMongoDBBundle\Tests\TestCase
{
/**
* This is not necessarily a good test, it doesnt generate any proxies
* because there are none in the AnnotationsBundle. However that is
* rather a task of doctrine to test. We touch the lines here and
* verify that the container is called correctly for the relevant information.
*
* @group DoctrineODMMongoDBProxy
*/
public function testWarmCache()
{
$testManager = $this->createTestDocumentManager(array(
__DIR__ . "/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Document")
);
$container = $this->getMock('Symfony\Component\DependencyInjection\Container');
$container->expects($this->at(0))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.proxy_dir'))
->will($this->returnValue(sys_get_temp_dir()));
$container->expects($this->at(1))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.auto_generate_proxy_classes'))
->will($this->returnValue( false ));
$container->expects($this->at(2))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.document_managers'))
->will($this->returnValue(array('default', 'foo')));
$container->expects($this->at(3))
->method('get')
->with($this->equalTo('doctrine.odm.mongodb.default_document_manager'))
->will($this->returnValue($testManager));
$container->expects($this->at(4))
->method('get')
->with($this->equalTo('doctrine.odm.mongodb.foo_document_manager'))
->will($this->returnValue($testManager));
$cacheWarmer = new ProxyCacheWarmer($container);
$cacheWarmer->warmUp(sys_get_temp_dir());
}
/**
* @group DoctrineODMMongoDBProxy
*/
public function testSkipWhenProxiesAreAutoGenerated()
{
$testManager = $this->createTestDocumentManager(array(
__DIR__ . "/../DependencyInjection/Fixtures/Bundles/AnnotationsBundle/Document")
);
$container = $this->getMock('Symfony\Component\DependencyInjection\Container');
$container->expects($this->at(0))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.proxy_dir'))
->will($this->returnValue(sys_get_temp_dir()));
$container->expects($this->at(1))
->method('getParameter')
->with($this->equalTo('doctrine.odm.mongodb.auto_generate_proxy_classes'))
->will($this->returnValue( true ));
$container->expects($this->at(2))
->method('getParameter')
->with($this->equalTo('assertion'))
->will($this->returnValue( true ));
$cacheWarmer = new ProxyCacheWarmer($container);
$cacheWarmer->warmUp(sys_get_temp_dir());
$container->getParameter('assertion'); // check that the assertion is really the third call.
}
/**
* @group DoctrineODMMongoDBProxy
*/
public function testProxyCacheWarmingIsNotOptional()
{
$container = $this->getMock('Symfony\Component\DependencyInjection\Container');
$cacheWarmer = new ProxyCacheWarmer($container);
$this->assertFalse($cacheWarmer->isOptional());
}
}

View File

@ -11,6 +11,9 @@
namespace Symfony\Bundle\DoctrineMongoDBBundle\Tests;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\MongoDB\Connection;
class TestCase extends \PHPUnit_Framework_TestCase
{
protected function setUp()
@ -19,4 +22,21 @@ class TestCase extends \PHPUnit_Framework_TestCase
$this->markTestSkipped('Doctrine MongoDB ODM is not available.');
}
}
/**
* @return DocumentManager
*/
protected function createTestDocumentManager($paths = array())
{
$config = new \Doctrine\ODM\MongoDB\Configuration();
$config->setAutoGenerateProxyClasses(true);
$config->setProxyDir(\sys_get_temp_dir());
$config->setHydratorDir(\sys_get_temp_dir());
$config->setProxyNamespace('SymfonyTests\Doctrine');
$config->setHydratorNamespace('SymfonyTests\Doctrine');
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths));
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
return DocumentManager::create(new Connection(), $config);
}
}