3
3
namespace Sineflow \ElasticsearchBundle \Tests \Functional \Result ;
4
4
5
5
use Sineflow \ElasticsearchBundle \Document \MLProperty ;
6
+ use Sineflow \ElasticsearchBundle \Exception \DocumentConversionException ;
6
7
use Sineflow \ElasticsearchBundle \Tests \AbstractContainerAwareTestCase ;
7
8
use Sineflow \ElasticsearchBundle \Tests \app \fixture \Acme \BarBundle \Document \ObjCategory ;
8
9
use Sineflow \ElasticsearchBundle \Tests \app \fixture \Acme \BarBundle \Document \Product ;
@@ -26,6 +27,89 @@ class DocumentConverterTest extends AbstractContainerAwareTestCase
26
27
'nonexisting ' => 'should be skipped ' ,
27
28
];
28
29
30
+ public function testAssignArrayToObjectWithNestedSingleValueInsteadOfArray ()
31
+ {
32
+ $ converter = $ this ->getContainer ()->get ('sfes.document_converter ' );
33
+ $ metadataCollector = $ this ->getContainer ()->get ('sfes.document_metadata_collector ' );
34
+
35
+ // Raw doc with a single object value where an array of objects is expected according to metadata def
36
+ $ rawDoc = [
37
+ '_id ' => 'rawDocWithSingleObjValueInsteadOfArray ' ,
38
+ 'related_categories ' => [
39
+ 'id ' => '123 ' ,
40
+ 'title ' => 'Acme ' ,
41
+ ],
42
+ ];
43
+
44
+ $ product = new Product ();
45
+ $ result = $ converter ->assignArrayToObject (
46
+ $ rawDoc ,
47
+ $ product ,
48
+ $ metadataCollector ->getDocumentMetadata ('AcmeBarBundle:Product ' )->getPropertiesMetadata ()
49
+ );
50
+ $ category = $ result ->relatedCategories ->current ();
51
+
52
+ $ this ->assertEquals ($ category ->id , 123 );
53
+ }
54
+
55
+ public function testAssignArrayToObjectWithNestedSingleValueArrayInsteadOfSingleValue ()
56
+ {
57
+ $ converter = $ this ->getContainer ()->get ('sfes.document_converter ' );
58
+ $ metadataCollector = $ this ->getContainer ()->get ('sfes.document_metadata_collector ' );
59
+
60
+ // Raw doc with array of single object where a single object value is expected according to metadata def
61
+ $ rawDoc = [
62
+ '_id ' => 'rawDocWithArrayValueInsteadOfSingleObject ' ,
63
+ 'category ' => [
64
+ [
65
+ 'id ' => '123 ' ,
66
+ 'title ' => 'Acme ' ,
67
+ ],
68
+ ],
69
+ ];
70
+
71
+ $ product = new Product ();
72
+ $ result = $ converter ->assignArrayToObject (
73
+ $ rawDoc ,
74
+ $ product ,
75
+ $ metadataCollector ->getDocumentMetadata ('AcmeBarBundle:Product ' )->getPropertiesMetadata ()
76
+ );
77
+
78
+ $ this ->assertEquals ($ result ->category ->id , 123 );
79
+ }
80
+
81
+ public function testAssignArrayToObjectWithNestedMultiValueArrayInsteadOfSingleValue ()
82
+ {
83
+ $ converter = $ this ->getContainer ()->get ('sfes.document_converter ' );
84
+ $ metadataCollector = $ this ->getContainer ()->get ('sfes.document_metadata_collector ' );
85
+
86
+ // Raw doc with array of many objects where a single object value is expected according to metadata def
87
+ $ rawDoc = [
88
+ '_id ' => 'rawDocWithArrayValueInsteadOfSingleObject ' ,
89
+ 'category ' => [
90
+ [
91
+ 'id ' => '123 ' ,
92
+ 'title ' => 'Acme ' ,
93
+ ],
94
+ [
95
+ 'id ' => '234 ' ,
96
+ 'title ' => 'Ucme ' ,
97
+ ],
98
+ ],
99
+ ];
100
+
101
+ $ product = new Product ();
102
+
103
+ $ this ->expectException (DocumentConversionException::class);
104
+ $ this ->expectExceptionMessage ('Multiple objects found for a single object field `category` ' );
105
+
106
+ $ converter ->assignArrayToObject (
107
+ $ rawDoc ,
108
+ $ product ,
109
+ $ metadataCollector ->getDocumentMetadata ('AcmeBarBundle:Product ' )->getPropertiesMetadata ()
110
+ );
111
+ }
112
+
29
113
public function testAssignArrayToObjectWithAllFieldsCorrectlySet ()
30
114
{
31
115
$ converter = $ this ->getContainer ()->get ('sfes.document_converter ' );
0 commit comments