Skip to content

Commit

Permalink
Add tests for _parent, nested and object types
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Muetton committed Nov 26, 2012
1 parent ea31910 commit a36ec87
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
75 changes: 75 additions & 0 deletions Tests/Transformer/ModelToElasticaAutoTransformerTest.php
Expand Up @@ -92,6 +92,19 @@ public function getFileContents()
{
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
Expand Down Expand Up @@ -215,4 +228,66 @@ public function testFileContentsAddedForAttachmentMapping()
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());
}
}
2 changes: 1 addition & 1 deletion Transformer/ModelToElasticaAutoTransformer.php
Expand Up @@ -45,7 +45,7 @@ public function transform($object, array $fields)
$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));
Expand Down

0 comments on commit a36ec87

Please sign in to comment.