diff --git a/Provider/Indexable.php b/Provider/Indexable.php index 827b3a4..197aeb8 100644 --- a/Provider/Indexable.php +++ b/Provider/Indexable.php @@ -112,16 +112,17 @@ class Indexable implements IndexableInterface if (is_array($callback)) { list($class, $method) = $callback + array(null, null); + + if (is_object($class)) { + $class = get_class($class); + } + if (strpos($class, '@') === 0) { $service = $this->container->get(substr($class, 1)); return array($service, $method); } - if (is_object($class)) { - $class = get_class($class); - } - if ($class && $method) { throw new \InvalidArgumentException(sprintf('Callback for type "%s", "%s::%s()", is not callable.', $type, $class, $method)); } diff --git a/Tests/Provider/IndexableTest.php b/Tests/Provider/IndexableTest.php index aae6484..6ef5669 100644 --- a/Tests/Provider/IndexableTest.php +++ b/Tests/Provider/IndexableTest.php @@ -12,12 +12,15 @@ namespace FOS\ElasticaBundle\Tests\Provider; use FOS\ElasticaBundle\Provider\Indexable; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; class IndexableTest extends \PHPUnit_Framework_TestCase { + public $container; + public function testIndexableUnknown() { - $indexable = new Indexable(array()); + $indexable = new Indexable(array(), $this->container); $index = $indexable->isObjectIndexable('index', 'type', new Entity); $this->assertTrue($index); @@ -30,7 +33,7 @@ class IndexableTest extends \PHPUnit_Framework_TestCase { $indexable = new Indexable(array( 'index/type' => $callback - )); + ), $this->container); $index = $indexable->isObjectIndexable('index', 'type', new Entity); $this->assertEquals($return, $index); @@ -44,7 +47,7 @@ class IndexableTest extends \PHPUnit_Framework_TestCase { $indexable = new Indexable(array( 'index/type' => $callback - )); + ), $this->container); $indexable->isObjectIndexable('index', 'type', new Entity); } @@ -63,12 +66,24 @@ class IndexableTest extends \PHPUnit_Framework_TestCase return array( array('isIndexable', false), array(array(new IndexableDecider(), 'isIndexable'), true), + array(array('@indexableService', 'isIndexable'), true), array(function(Entity $entity) { return $entity->maybeIndex(); }, true), array('entity.maybeIndex()', true), array('!object.isIndexable() && entity.property == "abc"', true), array('entity.property != "abc"', false), ); } + + protected function setUp() + { + $this->container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerInterface') + ->getMock(); + + $this->container->expects($this->any()) + ->method('get') + ->with('indexableService') + ->will($this->returnValue(new IndexableDecider())); + } } class Entity