Permalink
Browse files

move indexing-functionality to indexer-component

  • Loading branch information...
1 parent 4abb87c commit d2db84533704b5854c8a80566e76711864fdf4ff Florian Semm committed May 18, 2012
View
@@ -0,0 +1,61 @@
+<?php
+namespace FS\SolrBundle\Indexer;
+
+use FS\SolrBundle\Doctrine\Mapper\Mapping\CommandFactory;
+use FS\SolrBundle\SolrConnection;
+use FS\SolrBundle\Doctrine\Mapper\EntityMapper;
+use FS\SolrBundle\Doctrine\Mapper\MetaInformation;
+
+class Indexer implements IndexerInterface {
+
+ /**
+ *
+ * @var EntityMapper
+ */
+ private $mapper = null;
+
+ /**
+ *
+ * @var CommandFactory
+ */
+ private $commandFactory = null;
+
+ /**
+ *
+ * @var \SolrClient
+ */
+ private $solrClient = null;
+
+ /**
+ *
+ * @param SolrConnection $connection
+ * @param CommandFactory $commandFactory
+ */
+ public function __construct(SolrConnection $connection, CommandFactory $commandFactory) {
+ $this->solrClient = $connection->getClient();
+ $this->commandFactory = $commandFactory;
+ $this->mapper = new EntityMapper();
+ }
+
+ /* (non-PHPdoc)
+ * @see FS\SolrBundle\Indexer.IndexerInterface::toIndex()
+ */
+ public function toIndex(MetaInformation $metaInformation) {
+ $command = $this->commandFactory->get('all');
+ $this->mapper->setMappingCommand($command);
+ $document = $this->mapper->toDocument($metaInformation);
+
+ $this->addDocumentToIndex($document);
+ }
+
+ private function addDocumentToIndex($doc) {
+ try {
+ $updateResponse = $this->solrClient->addDocument($doc);
+
+ $this->solrClient->commit();
+ } catch (\Exception $e) {
+ throw new IndexerException('could not index entity');
+ }
+ }
+
+}
@@ -0,0 +1,4 @@
+<?php
+namespace FS\SolrBundle\Indexer;
+
+class IndexerException extends \RuntimeException { }
@@ -0,0 +1,11 @@
+<?php
+namespace FS\SolrBundle\Indexer;
+
+use FS\SolrBundle\Doctrine\Mapper\MetaInformation;
+
+interface IndexerInterface {
+
+ public function toIndex(MetaInformation $metaInformation);
+}
+
+?>
@@ -0,0 +1,6 @@
+<?php
+namespace FS\SolrBundle\Tests\Indexer;
+
+class IndexerException extends \RuntimeException {
+
+}
@@ -0,0 +1,100 @@
+<?php
+
+namespace FS\SolrBundle\Tests\Indexer;
+
+use FS\SolrBundle\Indexer\Indexer;
+
+use FS\SolrBundle\Event\EventManager;
+
+use FS\SolrBundle\Tests\SolrClientFake;
+
+use FS\SolrBundle\Tests\Doctrine\Mapper\ValidTestEntity;
+
+use FS\SolrBundle\Tests\Doctrine\Annotation\Entities\EntityWithRepository;
+
+use FS\SolrBundle\Doctrine\Mapper\MetaInformation;
+
+use FS\SolrBundle\Tests\Util\MetaTestInformationFactory;
+
+use FS\SolrBundle\SolrFacade;
+use FS\SolrBundle\Tests\Doctrine\Annotation\Entities\ValidEntityRepository;
+use FS\SolrBundle\Tests\Util\CommandFactoryStub;
+use FS\SolrBundle\Doctrine\Mapper\Mapping\MapAllFieldsCommand;
+use FS\SolrBundle\Doctrine\Annotation\AnnotationReader;
+use FS\SolrBundle\Doctrine\Mapper\Mapping\CommandFactory;
+use FS\SolrBundle\Query\SolrQuery;
+use FS\SolrBundle\SolrQueryFacade;
+
+/**
+ * test case.
+ */
+class IndexerTest extends \PHPUnit_Framework_TestCase {
+
+ private $solrConnection = null;
+ private $commandFactory = null;
+
+ public function setUp() {
+ $this->solrConnection = $this->getMock('FS\SolrBundle\SolrConnection', array(), array(), '', false);
+ $this->commandFactory = CommandFactoryStub::getFactoryWithAllMappingCommand();
+ }
+
+ private function setupMetaFactoryLoadOneCompleteInformation($metaInformation = null) {
+ if (null === $metaInformation) {
+ $metaInformation = MetaTestInformationFactory::getMetaInformation();
+ }
+
+ $this->metaFactory->expects($this->once())
+ ->method('loadInformation')
+ ->will($this->returnValue($metaInformation));
+ }
+
+ public function testAddDocument() {
+ $solrClientFake = new SolrClientFake();
+
+ $this->solrConnection->expects($this->once())
+ ->method('getClient')
+ ->will($this->returnValue($solrClientFake));
+
+ $metaInformation = MetaTestInformationFactory::getMetaInformation();
+
+ $indexer = new Indexer($this->solrConnection, $this->commandFactory);
+ $indexer->toIndex($metaInformation);
+
+ $this->assertTrue($solrClientFake->isCommited(), 'commit was never called');
+ }
+
+ public function UpdateDocument() {
+ $solrClientFake = new SolrClientFake();
+
+ $this->solrConnection->expects($this->once())
+ ->method('getClient')
+ ->will($this->returnValue($solrClientFake));
+
+ $this->setupMetaFactoryLoadOneCompleteInformation();
+
+ $solr = new SolrFacade($this->solrConnection, $this->commandFactory, $this->eventManager, $this->metaFactory);
+ $solr->updateDocument(new ValidTestEntity());
+
+ $this->assertTrue($solrClientFake->isCommited(), 'commit was never called');
+ }
+
+ public function RemoveDocument() {
+ $solrClientFake = new SolrClientFake();
+
+ $this->solrConnection->expects($this->once())
+ ->method('getClient')
+ ->will($this->returnValue($solrClientFake));
+
+ $this->eventManager->expects($this->once())
+ ->method('handle')
+ ->with(EventManager::DELETE);
+
+ $this->setupMetaFactoryLoadOneCompleteInformation();
+
+ $solr = new SolrFacade($this->solrConnection, $this->commandFactory, $this->eventManager, $this->metaFactory);
+ $solr->removeDocument(new ValidTestEntity());
+
+ $this->assertTrue($solrClientFake->isCommited(), 'commit was never called');
+ }
+}
+

0 comments on commit d2db845

Please sign in to comment.