Skip to content

Commit

Permalink
Revert to previous behaviour for managed classes (uses instanceof so …
Browse files Browse the repository at this point in the history
…that class checks are case-insensitive)

(cherry picked from commit 769dac2)
  • Loading branch information
pierre committed Apr 29, 2015
1 parent 23ca8ab commit 4e2c483
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 4 deletions.
18 changes: 15 additions & 3 deletions Listener/IndexSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,21 @@ private function scheduleForUnindexation($entity)
*/
private function isManaged($entity)
{
$entityClasses = array_merge(array(get_class($entity)), class_parents($entity));
$intersection = array_intersect($entityClasses, $this->managedClasses);
$managed = false;

return count($intersection) > 0;
if(in_array(get_class($entity), $this->managedClasses)) {
$managed = true;
}

if (!$managed) {
// if the entity is a Proxy
foreach ($this->managedClasses as $class) {
if ($entity instanceof $class) {
$managed = true;
}
}
}

return $managed;
}
}
8 changes: 8 additions & 0 deletions Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,17 @@ public function search($query, $index, $types = null)
*
* @param string $alias
* @param Indexer\IndexerInterface $indexer
* @throws \UnexpectedValueException
*/
public function registerIndexer($alias, IndexerInterface $indexer)
{
foreach($indexer->getManagedClasses() as $managedClass) {
if(!class_exists($managedClass)) {
$message = 'Invalid managed class "%s" provided in indexer "%s"';
throw new \UnexpectedValueException(sprintf($message, $managedClass, get_class($indexer)));
}
}

$this->indexers[$alias] = $indexer;
}

Expand Down
21 changes: 21 additions & 0 deletions Tests/Listener/IndexSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\ORM\Event\PostFlushEventArgs;
use Snowcap\ElasticaBundle\Listener\IndexSubscriber;
use Snowcap\ElasticaBundle\Tests\Listener\Mock\BarEntity;
use Snowcap\ElasticaBundle\Tests\Listener\Mock\BazEntity;
use Snowcap\ElasticaBundle\Tests\Listener\Mock\FooEntity;
use Snowcap\ElasticaBundle\Tests\Listener\Mock\FooEntityProxy;

Expand All @@ -31,6 +32,26 @@ public function testRelevantEntityIsIndexedWhenPersisted()
$indexSubscriber->postFlush($ea);
}

public function testLowercaseRelevantEntityIsIndexedWhenPersisted()
{
$em = $this->getMock('Doctrine\ORM\EntityManager', [], [], '', false);
$baz = new BazEntity();

$service = $this->getMock('Snowcap\ElasticaBundle\Tests\Listener\Mock\Service', ['index']);
$service
->expects($this->once())
->method('index')
->with($this->equalTo($baz));

$indexSubscriber = new IndexSubscriber($service);

$ea = new LifecycleEventArgs($baz, $em);
$indexSubscriber->postPersist($ea);

$ea = new PostFlushEventArgs($em);
$indexSubscriber->postFlush($ea);
}

public function testRelevantEntityIsIndexedOnlyOnceWhenPersisted()
{
$em = $this->getMock('Doctrine\ORM\EntityManager', [], [], '', false);
Expand Down
7 changes: 7 additions & 0 deletions Tests/Listener/Mock/BazEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Snowcap\ElasticaBundle\Tests\Listener\Mock;

class BazEntity {
public $baz;
}
7 changes: 6 additions & 1 deletion Tests/Listener/Mock/FooIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class FooIndexer implements IndexerInterface {
*/
public function getManagedClasses()
{
return array('Snowcap\ElasticaBundle\Tests\Listener\Mock\FooEntity');
return array(
'Snowcap\ElasticaBundle\Tests\Listener\Mock\FooEntity',
// Notice the lowercased entity name - we actually want to test this... should work even if the actual
// class name is BazEntity with a capital E
'Snowcap\ElasticaBundle\Tests\Listener\Mock\Bazentity'
);
}

/**
Expand Down
156 changes: 156 additions & 0 deletions Tests/Mock/BarIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Snowcap\ElasticaBundle\Tests\Mock;

use Doctrine\ORM\EntityManager;
use Elastica\Type;
use Snowcap\ElasticaBundle\Indexer\IndexerInterface;

/**
* Mock indexers for unit tests
*
* @package Snowcap\ElasticaBundle\Tests\Mock
*/
class BarIndexer implements IndexerInterface {
/**
* Return an array of classes managed by this indexer
* Must at least contain the class name of the main entity you wish to index, and may
* also contain additional classes whose update or deletion should trigger a reindex of
* the main entity mentionned above
*
* @return array
*/
public function getManagedClasses()
{
return array('Snowcap\ElasticaBundle\Tests\Mock\BarEntity');
}

/**
* Check if the passed entity can be managed by this indexer
*
* @param object $entity
* @return bool
*/
public function supports($entity)
{
// TODO: Implement supports() method.
}

/**
* Return a mapping array
* See http://ruflin.github.com/Elastica/ and
* http://www.elasticsearch.org/guide/reference/mapping/ for more information
*
* @return mixed
*/
public function getMappings()
{
// TODO: Implement getMappings() method.
}

/**
* Return of the ACTION_* constants depending on the provided entity
* Used to determine whether the given entity should be indexed or unindexed
*
* @param object $entity
* @param Type $type
* @return string
*/
public function getIndexAction($entity, Type $type)
{
// TODO: Implement getIndexAction() method.
}

/**
* Return an array of all the entities that need to be reindexed
* during a rebuild operation
*
* @param \Doctrine\ORM\EntityManager $em
* @param Type $type
* @return array
*/
public function getEntitiesToIndex(EntityManager $em, Type $type)
{
// TODO: Implement getEntitiesToIndex() method.
}

/**
* Get the entities to index provided a given entity
* In simple cases, this method should simply return an array with the provided entity
* In some cases, however (depending on the classes returned by getManagedClasses
* you might want, given an entity of class Foo, index in fact an associated entity of class Bar
*
* @param object $entity
* @return array
*/
public function getIndexableEntities($entity)
{
// TODO: Implement getIndexableEntities() method.
}

/**
* Determine the elasticsearch document identifier
*
* @param $entity
* @return mixed
*/
public function getDocumentIdentifier($entity)
{
// TODO: Implement getDocumentIdentifier() method.
}

/**
* Return an array of data that can be used to build a Elastica_Document instance
*
* @param object $entity
* @param Type $type
* @return array
*/
public function map($entity, Type $type)
{
// TODO: Implement map() method.
}

/**
* Add (or update) an elasticsearch document for the provided entity
*
* @param object $entity
* @param Type $type
*/
public function addIndex($entity, Type $type)
{
// TODO: Implement addIndex() method.
}

/**
* Remove (if existing) the elasticsearch document for the provided entity
*
* @param object $entity
* @param Type $type
*/
public function removeIndex($entity, Type $type)
{
// TODO: Implement removeIndex() method.
}

/**
* Remove (if existing) the elasticsearch document for the provided id
*
* @param integer $id
* @param Type $type
*/
public function removeIndexById($id, Type $type)
{
// TODO: Implement removeIndexById() method.
}

/**
* Store the entity manager
*
* @param \Doctrine\ORM\EntityManager $em
*/
public function setEntityManager(EntityManager $em)
{
// TODO: Implement setEntityManager() method.
}
}
7 changes: 7 additions & 0 deletions Tests/Mock/FooEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Snowcap\ElasticaBundle\Tests\Mock;

class FooEntity {
public $bar;
}
Loading

0 comments on commit 4e2c483

Please sign in to comment.