2011-07-14 20:49:18 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle\Tests\Transformer\ModelToElasticaAutoTransformer;
|
2011-07-14 20:49:18 +02:00
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
use FOS\ElasticaBundle\Transformer\ModelToElasticaAutoTransformer;
|
2013-03-27 22:19:50 +01:00
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess;
|
2011-07-14 20:49:18 +02:00
|
|
|
|
2011-12-05 22:44:12 +01:00
|
|
|
class POPO
|
2011-07-14 20:49:18 +02:00
|
|
|
{
|
2012-07-08 15:31:19 +02:00
|
|
|
public $id = 123;
|
|
|
|
public $name = 'someName';
|
|
|
|
private $desc = 'desc';
|
|
|
|
public $float = 7.2;
|
|
|
|
public $bool = true;
|
2012-02-14 18:08:38 +01:00
|
|
|
public $falseBool = false;
|
2011-12-05 22:44:12 +01:00
|
|
|
public $date;
|
2012-02-14 18:08:38 +01:00
|
|
|
public $nullValue;
|
2012-04-19 22:26:19 +02:00
|
|
|
public $file;
|
|
|
|
public $fileContents;
|
2011-12-05 22:44:12 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2012-04-19 22:26:19 +02:00
|
|
|
$this->date = new \DateTime('1979-05-05');
|
|
|
|
$this->file = new \SplFileInfo(__DIR__ . '/../fixtures/attachment.odt');
|
|
|
|
$this->fileContents = file_get_contents(__DIR__ . '/../fixtures/attachment.odt');
|
2011-12-05 22:44:12 +01:00
|
|
|
}
|
2011-07-14 20:49:18 +02:00
|
|
|
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
$iterator = new \ArrayIterator();
|
|
|
|
$iterator->append('value1');
|
|
|
|
|
|
|
|
return $iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArray()
|
|
|
|
{
|
2012-07-08 15:31:19 +02:00
|
|
|
return array(
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => 'value2'
|
|
|
|
);
|
2011-07-14 20:49:18 +02:00
|
|
|
}
|
2011-12-05 22:44:12 +01:00
|
|
|
|
|
|
|
public function getMultiArray()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => array('value2', false, 123, 8.9, new \DateTime('1978-09-07')),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBool()
|
|
|
|
{
|
|
|
|
return $this->bool;
|
|
|
|
}
|
|
|
|
|
2012-02-14 18:08:38 +01:00
|
|
|
public function getFalseBool()
|
|
|
|
{
|
|
|
|
return $this->falseBool;
|
|
|
|
}
|
|
|
|
|
2011-12-05 22:44:12 +01:00
|
|
|
public function getFloat()
|
|
|
|
{
|
|
|
|
return $this->float;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDate()
|
|
|
|
{
|
|
|
|
return $this->date;
|
|
|
|
}
|
|
|
|
|
2012-02-14 18:08:38 +01:00
|
|
|
public function getNullValue()
|
|
|
|
{
|
|
|
|
return $this->nullValue;
|
|
|
|
}
|
|
|
|
|
2012-04-19 22:26:19 +02:00
|
|
|
public function getFile()
|
|
|
|
{
|
|
|
|
return $this->file;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileContents()
|
|
|
|
{
|
|
|
|
return $this->file;
|
|
|
|
}
|
2012-11-26 14:37:47 +01:00
|
|
|
|
|
|
|
public function getSub()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
(object) array('foo' => 'foo', 'bar' => 'foo', 'id' => 1),
|
|
|
|
(object) array('foo' => 'bar', 'bar' => 'bar', 'id' => 2),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUpper()
|
|
|
|
{
|
|
|
|
return (object) array('id' => 'parent', 'name' => 'a random name');
|
|
|
|
}
|
2011-07-14 20:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-07-28 22:37:54 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
2013-04-22 13:20:10 +02:00
|
|
|
if (!class_exists('Elastica\Document')) {
|
2012-07-08 15:31:19 +02:00
|
|
|
;
|
|
|
|
$this->markTestSkipped('The Elastica library classes are not available');
|
|
|
|
}
|
2011-07-28 22:37:54 +02:00
|
|
|
}
|
|
|
|
|
2011-07-14 20:49:18 +02:00
|
|
|
public function testThatCanTransformObject()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$document = $transformer->transform(new POPO(), array('name' => array()));
|
2011-07-14 20:49:18 +02:00
|
|
|
$data = $document->getData();
|
2011-12-05 22:44:12 +01:00
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
$this->assertInstanceOf('Elastica\Document', $document);
|
2011-07-14 20:49:18 +02:00
|
|
|
$this->assertEquals(123, $document->getId());
|
|
|
|
$this->assertEquals('someName', $data['name']);
|
|
|
|
}
|
2011-12-05 22:44:12 +01:00
|
|
|
|
|
|
|
public function testThatCanTransformObjectWithCorrectTypes()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-07-08 16:38:09 +02:00
|
|
|
$document = $transformer->transform(
|
|
|
|
new POPO(), array(
|
|
|
|
'name' => array(),
|
|
|
|
'float' => array(),
|
|
|
|
'bool' => array(),
|
|
|
|
'date' => array(),
|
|
|
|
'falseBool' => array()
|
|
|
|
)
|
|
|
|
);
|
2011-12-05 22:44:12 +01:00
|
|
|
$data = $document->getData();
|
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
$this->assertInstanceOf('Elastica\Document', $document);
|
2011-12-05 22:44:12 +01:00
|
|
|
$this->assertEquals(123, $document->getId());
|
|
|
|
$this->assertEquals('someName', $data['name']);
|
|
|
|
$this->assertEquals(7.2, $data['float']);
|
|
|
|
$this->assertEquals(true, $data['bool']);
|
2012-02-14 18:08:38 +01:00
|
|
|
$this->assertEquals(false, $data['falseBool']);
|
2011-12-05 22:44:12 +01:00
|
|
|
$expectedDate = new \DateTime('1979-05-05');
|
2013-03-07 23:53:08 +01:00
|
|
|
$this->assertEquals($expectedDate->format('c'), $data['date']);
|
2011-12-05 22:44:12 +01:00
|
|
|
}
|
|
|
|
|
2011-07-14 20:49:18 +02:00
|
|
|
public function testThatCanTransformObjectWithIteratorValue()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$document = $transformer->transform(new POPO(), array('iterator' => array()));
|
2011-07-14 20:49:18 +02:00
|
|
|
$data = $document->getData();
|
2011-12-05 22:44:12 +01:00
|
|
|
|
2011-07-14 20:49:18 +02:00
|
|
|
$this->assertEquals(array('value1'), $data['iterator']);
|
|
|
|
}
|
2011-12-05 22:44:12 +01:00
|
|
|
|
2011-07-14 20:49:18 +02:00
|
|
|
public function testThatCanTransformObjectWithArrayValue()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$document = $transformer->transform(new POPO(), array('array' => array()));
|
2011-07-14 20:49:18 +02:00
|
|
|
$data = $document->getData();
|
2011-12-05 22:44:12 +01:00
|
|
|
|
2012-07-08 15:31:19 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => 'value2'
|
|
|
|
), $data['array']
|
|
|
|
);
|
2011-07-14 20:49:18 +02:00
|
|
|
}
|
|
|
|
|
2011-12-05 22:44:12 +01:00
|
|
|
public function testThatCanTransformObjectWithMultiDimensionalArrayValue()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$document = $transformer->transform(new POPO(), array('multiArray' => array()));
|
2011-12-05 22:44:12 +01:00
|
|
|
$data = $document->getData();
|
|
|
|
|
|
|
|
$expectedDate = new \DateTime('1978-09-07');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'key1' => 'value1',
|
2013-03-07 23:53:08 +01:00
|
|
|
'key2' => array('value2', false, 123, 8.9, $expectedDate->format('c')),
|
2011-12-05 22:44:12 +01:00
|
|
|
), $data['multiArray']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-27 20:37:59 +01:00
|
|
|
public function testThatNullValuesAreNotFilteredOut()
|
2012-02-14 18:08:38 +01:00
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$document = $transformer->transform(new POPO(), array('nullValue' => array()));
|
2012-02-14 18:08:38 +01:00
|
|
|
$data = $document->getData();
|
|
|
|
|
2012-02-27 20:37:59 +01:00
|
|
|
$this->assertTrue(array_key_exists('nullValue', $data));
|
2012-02-14 18:08:38 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 17:24:27 +02:00
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\PropertyAccess\Exception\RuntimeException
|
|
|
|
*/
|
2012-07-08 15:31:19 +02:00
|
|
|
public function testThatCannotTransformObjectWhenGetterDoesNotExistForPrivateMethod()
|
2011-07-14 20:49:18 +02:00
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$transformer->transform(new POPO(), array('desc' => array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileAddedForAttachmentMapping()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$document = $transformer->transform(new POPO(), array('file' => array('type' => 'attachment')));
|
|
|
|
$data = $document->getData();
|
|
|
|
|
|
|
|
$this->assertEquals(base64_encode(file_get_contents(__DIR__ . '/../fixtures/attachment.odt')), $data['file']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileContentsAddedForAttachmentMapping()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-04-19 22:26:19 +02:00
|
|
|
$document = $transformer->transform(new POPO(), array('fileContents' => array('type' => 'attachment')));
|
|
|
|
$data = $document->getData();
|
|
|
|
|
2012-07-08 16:38:09 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
base64_encode(file_get_contents(__DIR__ . '/../fixtures/attachment.odt')), $data['fileContents']
|
|
|
|
);
|
2011-07-14 20:49:18 +02:00
|
|
|
}
|
2012-11-26 14:37:47 +01:00
|
|
|
|
|
|
|
public function testNestedMapping()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-11-26 14:37:47 +01:00
|
|
|
$document = $transformer->transform(new POPO(), array(
|
|
|
|
'sub' => array(
|
|
|
|
'type' => 'nested',
|
|
|
|
'properties' => array('foo' => '~')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$data = $document->getData();
|
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('sub', $data));
|
|
|
|
$this->assertInternalType('array', $data['sub']);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array('foo' => 'foo'),
|
|
|
|
array('foo' => 'bar')
|
|
|
|
), $data['sub']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tesObjectMapping()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-11-26 14:37:47 +01:00
|
|
|
$document = $transformer->transform(new POPO(), array(
|
|
|
|
'sub' => array(
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => array('bar')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$data = $document->getData();
|
|
|
|
|
|
|
|
$this->assertTrue(array_key_exists('sub', $data));
|
|
|
|
$this->assertInternalType('array', $data['sub']);
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array('bar' => 'foo'),
|
|
|
|
array('bar' => 'bar')
|
|
|
|
), $data['sub']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testParentMapping()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-11-26 14:37:47 +01:00
|
|
|
$document = $transformer->transform(new POPO(), array(
|
|
|
|
'upper' => array(
|
|
|
|
'_parent' => array('type' => 'upper', 'identifier' => 'id'),
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals("parent", $document->getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testParentMappingWithCustomIdentifier()
|
|
|
|
{
|
2013-03-27 22:19:50 +01:00
|
|
|
$transformer = $this->getTransformer();
|
2012-11-26 14:37:47 +01:00
|
|
|
$document = $transformer->transform(new POPO(), array(
|
|
|
|
'upper' => array(
|
|
|
|
'_parent' => array('type' => 'upper', 'identifier' => 'name'),
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals("a random name", $document->getParent());
|
|
|
|
}
|
2013-03-27 22:19:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ModelToElasticaAutoTransformer
|
|
|
|
*/
|
|
|
|
private function getTransformer()
|
|
|
|
{
|
|
|
|
$transformer = new ModelToElasticaAutoTransformer();
|
2013-04-04 15:44:42 +02:00
|
|
|
$transformer->setPropertyAccessor(PropertyAccess::getPropertyAccessor());
|
2013-03-27 22:19:50 +01:00
|
|
|
|
|
|
|
return $transformer;
|
|
|
|
}
|
2011-07-14 20:49:18 +02:00
|
|
|
}
|