2012-01-03 23:16:56 +01:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Tests\Transformer;
|
2012-01-03 23:16:56 +01:00
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
use Elastica\Document;
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection;
|
2012-01-03 23:16:56 +01:00
|
|
|
|
|
|
|
class ElasticaToModelTransformerCollectionTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
2013-03-27 18:44:06 +01:00
|
|
|
* @var \FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerCollection
|
2012-01-03 23:16:56 +01:00
|
|
|
*/
|
|
|
|
protected $collection;
|
|
|
|
protected $transformers = array();
|
|
|
|
|
|
|
|
protected function collectionSetup()
|
|
|
|
{
|
2013-03-27 18:44:06 +01:00
|
|
|
$transformer1 = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
|
2012-01-03 23:16:56 +01:00
|
|
|
$transformer1->expects($this->any())
|
|
|
|
->method('getObjectClass')
|
2013-03-27 18:44:06 +01:00
|
|
|
->will($this->returnValue('FOS\ElasticaBundle\Tests\Transformer\POPO'));
|
2012-01-03 23:16:56 +01:00
|
|
|
|
2012-09-25 00:15:05 +02:00
|
|
|
$transformer1->expects($this->any())
|
|
|
|
->method('getIdentifierField')
|
|
|
|
->will($this->returnValue('id'));
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
$transformer2 = $this->getMock('FOS\ElasticaBundle\Transformer\ElasticaToModelTransformerInterface');
|
2012-01-03 23:16:56 +01:00
|
|
|
$transformer2->expects($this->any())
|
|
|
|
->method('getObjectClass')
|
2013-03-27 18:44:06 +01:00
|
|
|
->will($this->returnValue('FOS\ElasticaBundle\Tests\Transformer\POPO2'));
|
2012-01-03 23:16:56 +01:00
|
|
|
|
2012-09-25 00:15:05 +02:00
|
|
|
$transformer2->expects($this->any())
|
|
|
|
->method('getIdentifierField')
|
|
|
|
->will($this->returnValue('id'));
|
|
|
|
|
2012-01-03 23:16:56 +01:00
|
|
|
$this->collection = new ElasticaToModelTransformerCollection($this->transformers = array(
|
|
|
|
'type1' => $transformer1,
|
|
|
|
'type2' => $transformer2,
|
|
|
|
), array());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetObjectClass()
|
|
|
|
{
|
|
|
|
$this->collectionSetup();
|
|
|
|
|
|
|
|
$objectClasses = $this->collection->getObjectClass();
|
|
|
|
$this->assertEquals(array(
|
2013-03-27 18:44:06 +01:00
|
|
|
'type1' => 'FOS\ElasticaBundle\Tests\Transformer\POPO',
|
|
|
|
'type2' => 'FOS\ElasticaBundle\Tests\Transformer\POPO2'
|
2012-01-03 23:16:56 +01:00
|
|
|
), $objectClasses);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTransformDelegatesToTransformers()
|
|
|
|
{
|
|
|
|
$this->collectionSetup();
|
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
$document1 = new Document(123, array('data' => 'lots of data'), 'type1');
|
|
|
|
$document2 = new Document(124, array('data' => 'not so much data'), 'type2');
|
2012-01-03 23:16:56 +01:00
|
|
|
$result1 = new POPO(123, 'lots of data');
|
|
|
|
$result2 = new POPO2(124, 'not so much data');
|
|
|
|
|
|
|
|
$this->transformers['type1']->expects($this->once())
|
|
|
|
->method('transform')
|
|
|
|
->with(array($document1))
|
|
|
|
->will($this->returnValue(array($result1)));
|
|
|
|
|
|
|
|
$this->transformers['type2']->expects($this->once())
|
|
|
|
->method('transform')
|
|
|
|
->with(array($document2))
|
|
|
|
->will($this->returnValue(array($result2)));
|
|
|
|
|
|
|
|
$results = $this->collection->transform(array($document1, $document2));
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
$result1,
|
|
|
|
$result2,
|
|
|
|
), $results);
|
|
|
|
}
|
2012-05-03 04:06:41 +02:00
|
|
|
|
|
|
|
public function testTransformOrder()
|
|
|
|
{
|
|
|
|
$this->collectionSetup();
|
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
$document1 = new Document(123, array('data' => 'lots of data'), 'type1');
|
|
|
|
$document2 = new Document(124, array('data' => 'not so much data'), 'type1');
|
2012-05-03 04:06:41 +02:00
|
|
|
$result1 = new POPO(123, 'lots of data');
|
|
|
|
$result2 = new POPO2(124, 'not so much data');
|
|
|
|
|
|
|
|
$this->transformers['type1']->expects($this->once())
|
|
|
|
->method('transform')
|
|
|
|
->with(array($document1,$document2))
|
|
|
|
->will($this->returnValue(array($result1,$result2)));
|
|
|
|
|
|
|
|
$results = $this->collection->transform(array($document1, $document2));
|
|
|
|
|
|
|
|
$this->assertEquals(array(
|
|
|
|
$result1,
|
|
|
|
$result2,
|
|
|
|
), $results);
|
|
|
|
}
|
2012-01-03 23:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class POPO
|
|
|
|
{
|
|
|
|
public $id;
|
|
|
|
public $data;
|
|
|
|
|
|
|
|
public function __construct($id, $data)
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
$this->id = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class POPO2 extends POPO
|
|
|
|
{
|
|
|
|
}
|