Fix Indexable tests

This commit is contained in:
Tim Nagel 2014-06-26 18:01:34 +10:00
parent 5cc8c2978f
commit 77f2b99a3e
2 changed files with 23 additions and 7 deletions

View file

@ -112,16 +112,17 @@ class Indexable implements IndexableInterface
if (is_array($callback)) { if (is_array($callback)) {
list($class, $method) = $callback + array(null, null); list($class, $method) = $callback + array(null, null);
if (is_object($class)) {
$class = get_class($class);
}
if (strpos($class, '@') === 0) { if (strpos($class, '@') === 0) {
$service = $this->container->get(substr($class, 1)); $service = $this->container->get(substr($class, 1));
return array($service, $method); return array($service, $method);
} }
if (is_object($class)) {
$class = get_class($class);
}
if ($class && $method) { if ($class && $method) {
throw new \InvalidArgumentException(sprintf('Callback for type "%s", "%s::%s()", is not callable.', $type, $class, $method)); throw new \InvalidArgumentException(sprintf('Callback for type "%s", "%s::%s()", is not callable.', $type, $class, $method));
} }

View file

@ -12,12 +12,15 @@
namespace FOS\ElasticaBundle\Tests\Provider; namespace FOS\ElasticaBundle\Tests\Provider;
use FOS\ElasticaBundle\Provider\Indexable; use FOS\ElasticaBundle\Provider\Indexable;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
class IndexableTest extends \PHPUnit_Framework_TestCase class IndexableTest extends \PHPUnit_Framework_TestCase
{ {
public $container;
public function testIndexableUnknown() public function testIndexableUnknown()
{ {
$indexable = new Indexable(array()); $indexable = new Indexable(array(), $this->container);
$index = $indexable->isObjectIndexable('index', 'type', new Entity); $index = $indexable->isObjectIndexable('index', 'type', new Entity);
$this->assertTrue($index); $this->assertTrue($index);
@ -30,7 +33,7 @@ class IndexableTest extends \PHPUnit_Framework_TestCase
{ {
$indexable = new Indexable(array( $indexable = new Indexable(array(
'index/type' => $callback 'index/type' => $callback
)); ), $this->container);
$index = $indexable->isObjectIndexable('index', 'type', new Entity); $index = $indexable->isObjectIndexable('index', 'type', new Entity);
$this->assertEquals($return, $index); $this->assertEquals($return, $index);
@ -44,7 +47,7 @@ class IndexableTest extends \PHPUnit_Framework_TestCase
{ {
$indexable = new Indexable(array( $indexable = new Indexable(array(
'index/type' => $callback 'index/type' => $callback
)); ), $this->container);
$indexable->isObjectIndexable('index', 'type', new Entity); $indexable->isObjectIndexable('index', 'type', new Entity);
} }
@ -63,12 +66,24 @@ class IndexableTest extends \PHPUnit_Framework_TestCase
return array( return array(
array('isIndexable', false), array('isIndexable', false),
array(array(new IndexableDecider(), 'isIndexable'), true), array(array(new IndexableDecider(), 'isIndexable'), true),
array(array('@indexableService', 'isIndexable'), true),
array(function(Entity $entity) { return $entity->maybeIndex(); }, true), array(function(Entity $entity) { return $entity->maybeIndex(); }, true),
array('entity.maybeIndex()', true), array('entity.maybeIndex()', true),
array('!object.isIndexable() && entity.property == "abc"', true), array('!object.isIndexable() && entity.property == "abc"', true),
array('entity.property != "abc"', false), 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 class Entity