Add tests for _parent, nested and object types

This commit is contained in:
Julien Muetton 2012-11-26 14:37:47 +01:00
commit a36ec87f40
2 changed files with 76 additions and 1 deletions

View file

@ -92,6 +92,19 @@ class POPO
{
return $this->file;
}
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');
}
}
class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
@ -215,4 +228,66 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
base64_encode(file_get_contents(__DIR__ . '/../fixtures/attachment.odt')), $data['fileContents']
);
}
public function testNestedMapping()
{
$transformer = new ModelToElasticaAutoTransformer();
$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()
{
$transformer = new ModelToElasticaAutoTransformer();
$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()
{
$transformer = new ModelToElasticaAutoTransformer();
$document = $transformer->transform(new POPO(), array(
'upper' => array(
'_parent' => array('type' => 'upper', 'identifier' => 'id'),
)
));
$this->assertEquals("parent", $document->getParent());
}
public function testParentMappingWithCustomIdentifier()
{
$transformer = new ModelToElasticaAutoTransformer();
$document = $transformer->transform(new POPO(), array(
'upper' => array(
'_parent' => array('type' => 'upper', 'identifier' => 'name'),
)
));
$this->assertEquals("a random name", $document->getParent());
}
}

View file

@ -45,7 +45,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
$document = new \Elastica_Document($identifier);
foreach ($fields as $key => $mapping) {
$property = new PropertyPath($key);
if (isset($mapping['_parent'])) {
if (!empty($mapping['_parent'])) {
$parent = $property->getValue($object);
$identifierProperty = new PropertyPath($mapping['_parent']['identifier']);
$document->setParent($identifierProperty->getValue($parent));