Permalink
Browse files

Add tests for `_parent`, `nested` and `object` types

  • Loading branch information...
1 parent ea31910 commit a36ec87f40b36ea24e81cb34c6b9573f398d2367 Julien Muetton committed Nov 26, 2012
@@ -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
@@ -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());
+ }
}
@@ -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));

0 comments on commit a36ec87

Please sign in to comment.