Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding manager that returns Doctrine style repositories

  • Loading branch information...
commit 7dbc4221ae23fe1626a830f5f8ead051ebbc96d8 1 parent eb19002
@richardmiller richardmiller authored
View
1  DependencyInjection/Configuration.php
@@ -138,6 +138,7 @@ protected function getTypesNode()
->children()
->scalarNode('driver')->end()
->scalarNode('model')->end()
+ ->scalarNode('repository')->end()
->scalarNode('identifier')->defaultValue('id')->end()
->arrayNode('provider')
->children()
View
9 DependencyInjection/FOQElasticaExtension.php
@@ -305,6 +305,15 @@ protected function loadTypeFinder(array $typeConfig, ContainerBuilder $container
$finderDef->replaceArgument(1, new Reference($elasticaToModelId));
$container->setDefinition($finderId, $finderDef);
+ $managerDef = $container->getDefinition('foq_elastica.manager');
+ $arguments = array( $typeConfig['model'], new Reference($finderId));
+ if (isset($typeConfig['repository'])) {
+ $arguments[] = $typeConfig['repository'];
+ }
+
+ $managerDef->addMethodCall('addEntity', $arguments);
+ $container->setDefinition('foq_elastica.manager', $managerDef);
+
return $finderId;
}
View
57 Manager.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace FOQ\ElasticaBundle;
+
+use RuntimeException;
+
+/**
+ * @author Richard Miller <info@limethinking.co.uk>
+ *
+ * Allows retrieval of basic or custom repository for mapped Doctrine
+ * entities/documents.
+ */
+class Manager
+{
+ protected $entities;
+ protected $repositories;
+
+ public function addEntity($entityName, $finder, $repositoryName = null)
+ {
+ $this->entities[$entityName]= array();
+ $this->entities[$entityName]['finder'] = $finder;
+ $this->entities[$entityName]['repositoryName'] = $repositoryName;
+ }
+
+ /**
+ * Return repository for entity
+ *
+ * Returns custom repository if one specified otherwise
+ * returns a basic respository.
+ */
+ public function getRepository($entityName)
+ {
+ if (isset($this->repositories[$entityName])) {
+ return $this->repositories[$entityName];
+ }
+
+ if (!isset($this->entities[$entityName])) {
+ throw new RuntimeException(sprintf('No search finder configured for %s', $entityName));
+ }
+
+ if (isset($this->entities[$entityName]['repositoryName'])) {
+
+ $repositoryName = $this->entities[$entityName]['repositoryName'];
+ if (!class_exists($repositoryName)) {
+ throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $entityName));
+ }
+ $repository = new $repositoryName($this->entities[$entityName]['finder']);
+ $this->repositories[$entityName] = $repository;
+ return $repository;
+ }
+
+ $repository = new Repository($this->entities[$entityName]['finder']);
+ $this->repositories[$entityName] = $repository;
+ return $repository;
+ }
+
+}
View
31 Repository.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace FOQ\ElasticaBundle;
+
+/**
+ * @author Richard Miller <info@limethinking.co.uk>
+ *
+ * Basic respoitory to be extended to hold custom queries to be run
+ * in the finder.
+ */
+class Repository
+{
+ protected $finder;
+
+ public function __construct($finder)
+ {
+ $this->finder = $finder;
+ }
+
+
+ public function find($query)
+ {
+ return $this->finder->find($query);
+ }
+
+ public function findPaginated($query)
+ {
+ return $this->finder->findPaginated($query);
+ }
+
+}
View
3  Resources/config/config.xml
@@ -10,6 +10,7 @@
<parameter key="foq_elastica.type.class">Elastica_Type</parameter>
<parameter key="foq_elastica.logger.class">FOQ\ElasticaBundle\Logger\ElasticaLogger</parameter>
<parameter key="foq_elastica.data_collector.class">FOQ\ElasticaBundle\DataCollector\ElasticaDataCollector</parameter>
+ <parameter key="foq_elastica.manager.class">FOQ\ElasticaBundle\Manager</parameter>
</parameters>
@@ -62,6 +63,8 @@
<argument /> <!-- options -->
</service>
+ <service id="foq_elastica.manager" class="%foq_elastica.manager.class%" />
+
</services>
</container>
View
74 Tests/ManagerTest.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace FOQ\ElasticaBundle\Tests;
+
+use FOQ\ElasticaBundle\Manager;
+
+class CustomRepository{}
+
+/**
+ * @author Richard Miller <info@limethinking.co.uk>
+ */
+class ManagerTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testThatGetRepositoryReturnsDefaultRepository()
+ {
+ $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $entityName = 'Test Entity';
+
+ $manager = new Manager($finderMock);
+ $manager->addEntity($entityName, $finderMock);
+ $repository = $manager->getRepository($entityName);
+ $this->assertInstanceOf('FOQ\ElasticaBundle\Repository', $repository);
+ }
+
+ public function testThatGetRepositoryReturnsCustomRepository()
+ {
+ $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $entityName = 'Test Entity';
+
+ $manager = new Manager($finderMock);
+ $manager->addEntity($entityName, $finderMock, 'FOQ\ElasticaBundle\Tests\CustomRepository');
+ $repository = $manager->getRepository($entityName);
+ $this->assertInstanceOf('FOQ\ElasticaBundle\Tests\CustomRepository', $repository);
+ }
+
+ /**
+ * @expectedException RuntimeException
+ */
+ public function testThatGetRepositoryThrowsExceptionIfEntityNotConfigured()
+ {
+ $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $entityName = 'Test Entity';
+
+ $manager = new Manager($finderMock);
+ $manager->addEntity($entityName, $finderMock);
+ $manager->getRepository('Missing Entity');
+ }
+
+ /**
+ * @expectedException RuntimeException
+ */
+ public function testThatGetRepositoryThrowsExceptionIfCustomRepositoryNotFound()
+ {
+ $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $entityName = 'Test Entity';
+
+ $manager = new Manager($finderMock);
+ $manager->addEntity($entityName, $finderMock, 'FOQ\ElasticaBundle\Tests\MissingRepository');
+ $manager->getRepository('Missing Entity');
+ }
+}
View
43 Tests/RepositoryTest.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace FOQ\ElasticaBundle\Tests;
+
+use FOQ\ElasticaBundle\Repository;
+
+/**
+ * @author Richard Miller <info@limethinking.co.uk>
+ */
+class RepositoryTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testThatFindCallsFindOnFinder()
+ {
+ $testQuery = 'Test Query';
+
+ $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $finderMock->expects($this->once())
+ ->method('find')
+ ->with($this->equalTo($testQuery));
+
+ $repository = new Repository($finderMock);
+ $repository->find($testQuery);
+ }
+
+ public function testThatFindPaginatedCallsFindPaginatedOnFinder()
+ {
+ $testQuery = 'Test Query';
+
+ $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $finderMock->expects($this->once())
+ ->method('findPaginated')
+ ->with($this->equalTo($testQuery));
+
+ $repository = new Repository($finderMock);
+ $repository->findPaginated($testQuery);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.