diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/CacheWarmer/HydratorCacheWarmer.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/CacheWarmer/HydratorCacheWarmer.php new file mode 100644 index 0000000000..4cdbd446f0 --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/CacheWarmer/HydratorCacheWarmer.php @@ -0,0 +1,76 @@ + + * + * 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 + * @author Jonathan H. Wage + */ +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); + } + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/CacheWarmer/ProxyCacheWarmer.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/CacheWarmer/ProxyCacheWarmer.php new file mode 100644 index 0000000000..f8648c251a --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/CacheWarmer/ProxyCacheWarmer.php @@ -0,0 +1,76 @@ + + * + * 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 + * @author Jonathan H. Wage + */ +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); + } + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/DoctrineODMCommand.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/DoctrineODMCommand.php index 77cbfa937b..3896587d77 100644 --- a/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/DoctrineODMCommand.php +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/DoctrineODMCommand.php @@ -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; + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateDocumentsDoctrineODMCommand.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateDocumentsDoctrineODMCommand.php new file mode 100644 index 0000000000..915385da5a --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateDocumentsDoctrineODMCommand.php @@ -0,0 +1,80 @@ + + * + * 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 + * @author Jonathan H. Wage + */ +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(<<doctrine:generate:documents command generates document classes and method stubs from your mapping information: + +You have to limit generation of documents to an individual bundle: + + ./app/console doctrine:generate:documents MyCustomBundle + +Alternatively, you can limit generation to a single document within a bundle: + + ./app/console doctrine:generate:documents "MyCustomBundle" --document="User" + +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 "%s"', $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 %s', $metadata->name)); + $documentGenerator->generate(array($metadata), $this->findBasePathForBundle($foundBundle)); + } + } else { + throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped documents."); + } + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateHydratorsDoctrineODMCommand.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateHydratorsDoctrineODMCommand.php new file mode 100644 index 0000000000..03503d81a5 --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateHydratorsDoctrineODMCommand.php @@ -0,0 +1,54 @@ + + * + * 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 + * @author Jonathan H. Wage + */ +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(<<doctrine:generate:hydrators command generates hydrator classes for your documents: + + ./app/console doctrine:generate:hydrators + +You can specify the document manager you want to generate the hydrators for: + + ./app/console doctrine:generate:hydrators --dm=name +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + DoctrineODMCommand::setApplicationDocumentManager($this->application, $input->getOption('dm')); + + return parent::execute($input, $output); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateProxiesDoctrineODMCommand.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateProxiesDoctrineODMCommand.php new file mode 100644 index 0000000000..180b56746d --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateProxiesDoctrineODMCommand.php @@ -0,0 +1,54 @@ + + * + * 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 + * @author Jonathan H. Wage + */ +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(<<doctrine:generate:proxies command generates proxy classes for your default document manager: + + ./app/console doctrine:generate:proxies + +You can specify the document manager you want to generate the proxies for: + + ./app/console doctrine:generate:proxies --dm=name +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + DoctrineODMCommand::setApplicationDocumentManager($this->application, $input->getOption('dm')); + + return parent::execute($input, $output); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateRepositoriesDoctrineODMCommand.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateRepositoriesDoctrineODMCommand.php new file mode 100644 index 0000000000..72ec006fe7 --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/GenerateRepositoriesDoctrineODMCommand.php @@ -0,0 +1,77 @@ + + * + * 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 + * @author Jonathan H. Wage + */ +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(<<doctrine:generate:repositories command generates the configured document repository classes from your mapping information: + + ./app/console doctrine:generate:repositories +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 "%s"', $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(' > OK generating %s', $metadata->customRepositoryClassName)); + $generator->writeDocumentRepositoryClass($metadata->customRepositoryClassName, $this->findBasePathForBundle($foundBundle)); + } else { + $output->writeln(sprintf(' > SKIP no custom repository for %s', $metadata->name)); + } + } + } else { + throw new \RuntimeException("Bundle " . $bundleName . " does not contain any mapped documents."); + } + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/QueryDoctrineODMCommand.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/QueryDoctrineODMCommand.php new file mode 100644 index 0000000000..014eb7c02f --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Command/QueryDoctrineODMCommand.php @@ -0,0 +1,44 @@ + + * + * 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 + * @author Jonathan H. Wage + */ +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); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/DependencyInjection/DoctrineMongoDBExtension.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/DependencyInjection/DoctrineMongoDBExtension.php index 3a1911f1a3..66c726842b 100644 --- a/src/Symfony/Bundle/DoctrineMongoDBBundle/DependencyInjection/DoctrineMongoDBExtension.php +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/DependencyInjection/DoctrineMongoDBExtension.php @@ -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)); } /** diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Resources/config/mongodb.xml b/src/Symfony/Bundle/DoctrineMongoDBBundle/Resources/config/mongodb.xml index 1f910bf33c..8f6e36af5b 100755 --- a/src/Symfony/Bundle/DoctrineMongoDBBundle/Resources/config/mongodb.xml +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Resources/config/mongodb.xml @@ -50,7 +50,13 @@ Symfony\Bundle\DoctrineMongoDBBundle\Security\DocumentUserProvider - + + + Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer\ProxyCacheWarmer + + + Symfony\Bundle\DoctrineMongoDBBundle\CacheWarmer\HydratorCacheWarmer + Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints\UniqueValidator @@ -88,7 +94,17 @@ - + + + + + + + + + + + diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/CacheWarmer/HydratorCacheWarmerTest.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/CacheWarmer/HydratorCacheWarmerTest.php new file mode 100644 index 0000000000..250073342d --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/CacheWarmer/HydratorCacheWarmerTest.php @@ -0,0 +1,97 @@ + + * + * 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()); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.php new file mode 100644 index 0000000000..0c75ae63d9 --- /dev/null +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/CacheWarmer/ProxyCacheWarmerTest.php @@ -0,0 +1,97 @@ + + * + * 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()); + } +} \ No newline at end of file diff --git a/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/TestCase.php b/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/TestCase.php index 7c78c2f531..762c96f12a 100644 --- a/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/TestCase.php +++ b/src/Symfony/Bundle/DoctrineMongoDBBundle/Tests/TestCase.php @@ -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); + } }