Ignore missing Doctrine results during hybridTransform()
This commit is contained in:
parent
eaa6c2e085
commit
9a5b80e723
|
@ -120,11 +120,17 @@ abstract class AbstractElasticaToModelTransformer implements ElasticaToModelTran
|
||||||
|
|
||||||
public function hybridTransform(array $elasticaObjects)
|
public function hybridTransform(array $elasticaObjects)
|
||||||
{
|
{
|
||||||
|
$indexedElasticaResults = array();
|
||||||
|
foreach ($elasticaObjects as $elasticaObject) {
|
||||||
|
$indexedElasticaResults[$elasticaObject->getId()] = $elasticaObject;
|
||||||
|
}
|
||||||
|
|
||||||
$objects = $this->transform($elasticaObjects);
|
$objects = $this->transform($elasticaObjects);
|
||||||
|
|
||||||
$result = array();
|
$result = array();
|
||||||
for ($i = 0; $i < count($elasticaObjects); $i++) {
|
foreach ($objects as $object) {
|
||||||
$result[] = new HybridResult($elasticaObjects[$i], $objects[$i]);
|
$id = $this->propertyAccessor->getValue($object, $this->options['identifier']);
|
||||||
|
$result[] = new HybridResult($indexedElasticaResults[$id], $object);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
66
Tests/Doctrine/AbstractElasticaToModelTransformerTest.php
Normal file
66
Tests/Doctrine/AbstractElasticaToModelTransformerTest.php
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FOS\ElasticaBundle\Tests\Doctrine;
|
||||||
|
|
||||||
|
use Elastica\Result;
|
||||||
|
use FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer;
|
||||||
|
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||||
|
|
||||||
|
class AbstractElasticaToModelTransformerTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \Doctrine\Common\Persistence\ManagerRegistry|\PHPUnit_Framework_MockObject_MockObject
|
||||||
|
*/
|
||||||
|
protected $registry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $objectClass = 'stdClass';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests if ignore_missing option is properly handled in transformHybrid() method
|
||||||
|
*/
|
||||||
|
public function testIgnoreMissingOptionDuringTransformHybrid()
|
||||||
|
{
|
||||||
|
$transformer = $this->getMock(
|
||||||
|
'FOS\ElasticaBundle\Doctrine\ORM\ElasticaToModelTransformer',
|
||||||
|
array('findByIdentifiers'),
|
||||||
|
array($this->registry, $this->objectClass, array('ignore_missing' => true))
|
||||||
|
);
|
||||||
|
|
||||||
|
$transformer->setPropertyAccessor(PropertyAccess::createPropertyAccessor());
|
||||||
|
|
||||||
|
$firstOrmResult = new \stdClass();
|
||||||
|
$firstOrmResult->id = 1;
|
||||||
|
$secondOrmResult = new \stdClass();
|
||||||
|
$secondOrmResult->id = 3;
|
||||||
|
$transformer->expects($this->once())
|
||||||
|
->method('findByIdentifiers')
|
||||||
|
->with(array(1, 2, 3))
|
||||||
|
->willReturn(array($firstOrmResult, $secondOrmResult));
|
||||||
|
|
||||||
|
$firstElasticaResult = new Result(array('_id' => 1));
|
||||||
|
$secondElasticaResult = new Result(array('_id' => 2));
|
||||||
|
$thirdElasticaResult = new Result(array('_id' => 3));
|
||||||
|
|
||||||
|
$hybridResults = $transformer->hybridTransform(array($firstElasticaResult, $secondElasticaResult, $thirdElasticaResult));
|
||||||
|
|
||||||
|
$this->assertCount(2, $hybridResults);
|
||||||
|
$this->assertEquals($firstOrmResult, $hybridResults[0]->getTransformed());
|
||||||
|
$this->assertEquals($firstElasticaResult, $hybridResults[0]->getResult());
|
||||||
|
$this->assertEquals($secondOrmResult, $hybridResults[1]->getTransformed());
|
||||||
|
$this->assertEquals($thirdElasticaResult, $hybridResults[1]->getResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
if (!interface_exists('Doctrine\Common\Persistence\ManagerRegistry')) {
|
||||||
|
$this->markTestSkipped('Doctrine Common is not present');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue