@@ -30,41 +30,105 @@ class DataObjectHandlerTest extends MagentoTestCase
30
30
'value ' => 'testValue '
31
31
]
32
32
]
33
- ]
33
+ ],
34
+ 'EntityTwo ' => [
35
+ 'type ' => 'testType ' ,
36
+ 'extends ' => 'EntityOne ' ,
37
+ 'data ' => [
38
+ 0 => [
39
+ 'key ' => 'testKeyTwo ' ,
40
+ 'value ' => 'testValueTwo '
41
+ ]
42
+ ]
43
+ ],
34
44
]
35
45
];
36
46
37
- /**
38
- * Set up everything required to mock DataObjectHander::getInstance()
39
- * The first call to getInstance() uses these mocks to emulate the parser, initializing internal state
40
- * according to the PARSER_OUTPUT value
41
- */
42
- public static function setUpBeforeClass ()
43
- {
44
- $ mockDataProfileSchemaParser = AspectMock::double (DataProfileSchemaParser::class, [
45
- 'readDataProfiles ' => self ::PARSER_OUTPUT
46
- ])->make ();
47
-
48
- $ mockObjectManager = AspectMock::double (ObjectManager::class, [
49
- 'create ' => $ mockDataProfileSchemaParser
50
- ])->make ();
47
+ const PARSER_OUTPUT_WITH_EXTEND = [
48
+ 'entity ' => [
49
+ 'EntityOne ' => [
50
+ 'name ' => 'EntityOne ' ,
51
+ 'type ' => 'testType ' ,
52
+ 'data ' => [
53
+ 0 => [
54
+ 'key ' => 'testKey ' ,
55
+ 'value ' => 'testValue '
56
+ ]
57
+ ]
58
+ ],
59
+ 'EntityTwo ' => [
60
+ 'name ' => 'EntityTwo ' ,
61
+ 'type ' => 'testType ' ,
62
+ 'extends ' => 'EntityOne ' ,
63
+ 'data ' => [
64
+ 0 => [
65
+ 'key ' => 'testKeyTwo ' ,
66
+ 'value ' => 'testValueTwo '
67
+ ]
68
+ ],
69
+ ],
70
+ 'EntityThree ' => [
71
+ 'name ' => 'EntityThree ' ,
72
+ 'type ' => 'testType ' ,
73
+ 'extends ' => 'EntityOne ' ,
74
+ 'data ' => [
75
+ 0 => [
76
+ 'key ' => 'testKeyThree ' ,
77
+ 'value ' => 'testValueThree '
78
+ ]
79
+ ],
80
+ ]
81
+ ]
82
+ ];
51
83
52
- AspectMock::double (ObjectManagerFactory::class, [
53
- 'getObjectManager ' => $ mockObjectManager
54
- ]);
55
- }
84
+ const PARSER_OUTPUT_WITH_EXTEND_INVALID = [
85
+ 'entity ' => [
86
+ 'EntityOne ' => [
87
+ 'name ' => 'EntityOne ' ,
88
+ 'type ' => 'testType ' ,
89
+ 'extends ' => 'EntityOne ' ,
90
+ 'data ' => [
91
+ 0 => [
92
+ 'key ' => 'testKey ' ,
93
+ 'value ' => 'testValue '
94
+ ]
95
+ ]
96
+ ],
97
+ 'EntityTwo ' => [
98
+ 'name ' => 'EntityTwo ' ,
99
+ 'type ' => 'testType ' ,
100
+ 'data ' => [
101
+ 0 => [
102
+ 'key ' => 'testKeyTwo ' ,
103
+ 'value ' => 'testValueTwo '
104
+ ]
105
+ ],
106
+ ],
107
+ 'EntityThree ' => [
108
+ 'name ' => 'EntityThree ' ,
109
+ 'type ' => 'testType ' ,
110
+ 'extends ' => 'EntityThree ' ,
111
+ 'data ' => [
112
+ 0 => [
113
+ 'key ' => 'testKeyThree ' ,
114
+ 'value ' => 'testValueThree '
115
+ ]
116
+ ],
117
+ ]
118
+ ]
119
+ ];
56
120
57
121
/**
58
122
* getAllObjects should contain the expected data object
59
123
*/
60
124
public function testGetAllObjects ()
61
125
{
62
- // Call the method under test
126
+ $ this -> setUpMockDataObjectHander ( self :: PARSER_OUTPUT );
63
127
128
+ // Call the method under test
64
129
$ actual = DataObjectHandler::getInstance ()->getAllObjects ();
65
130
66
131
// Assert
67
-
68
132
$ expected = new EntityDataObject ('EntityOne ' , 'testType ' , ['testkey ' => 'testValue ' ], [], null , []);
69
133
$ this ->assertArrayHasKey ('EntityOne ' , $ actual );
70
134
$ this ->assertEquals ($ expected , $ actual ['EntityOne ' ]);
@@ -75,22 +139,134 @@ public function testGetAllObjects()
75
139
*/
76
140
public function testGetObject ()
77
141
{
78
- // Call the method under test
142
+ $ this -> setUpMockDataObjectHander ( self :: PARSER_OUTPUT );
79
143
144
+ // Call the method under test
80
145
$ actual = DataObjectHandler::getInstance ()->getObject ('EntityOne ' );
81
146
82
147
// Assert
83
-
84
148
$ expected = new EntityDataObject ('EntityOne ' , 'testType ' , ['testkey ' => 'testValue ' ], [], null , []);
85
149
$ this ->assertEquals ($ expected , $ actual );
86
150
}
87
151
88
152
/**
89
- * getObject should return null if the data object does not exist
153
+ * getAllObjects should return the expected data object if it exists
90
154
*/
91
155
public function testGetObjectNull ()
92
156
{
157
+ $ this ->setUpMockDataObjectHander (self ::PARSER_OUTPUT );
158
+
93
159
$ actual = DataObjectHandler::getInstance ()->getObject ('h953u789h0g73t521 ' ); // doesnt exist
94
160
$ this ->assertNull ($ actual );
95
161
}
162
+
163
+ /**
164
+ * getAllObjects should contain the expected data object with extends
165
+ */
166
+ public function testGetAllObjectsWithDataExtends ()
167
+ {
168
+ $ this ->setUpMockDataObjectHander (self ::PARSER_OUTPUT_WITH_EXTEND );
169
+
170
+ // Call the method under test
171
+ $ actual = DataObjectHandler::getInstance ()->getAllObjects ();
172
+
173
+ // Assert
174
+ $ expected = new EntityDataObject (
175
+ 'EntityTwo ' ,
176
+ 'testType ' ,
177
+ ['testkey ' => 'testValue ' , 'testkeytwo ' => 'testValueTwo ' ],
178
+ [],
179
+ null ,
180
+ [],
181
+ 'EntityOne '
182
+ );
183
+ $ this ->assertArrayHasKey ('EntityTwo ' , $ actual );
184
+ $ this ->assertEquals ($ expected , $ actual ['EntityTwo ' ]);
185
+ }
186
+
187
+ /**
188
+ * getObject should return the expected data object with extended data if it exists
189
+ */
190
+ public function testGetObjectWithDataExtends ()
191
+ {
192
+ $ this ->setUpMockDataObjectHander (self ::PARSER_OUTPUT_WITH_EXTEND );
193
+
194
+ // Call the method under test
195
+ $ actual = DataObjectHandler::getInstance ()->getObject ('EntityTwo ' );
196
+
197
+ // Assert
198
+ $ expected = new EntityDataObject (
199
+ 'EntityTwo ' ,
200
+ 'testType ' ,
201
+ ['testkey ' => 'testValue ' , 'testkeytwo ' => 'testValueTwo ' ],
202
+ [],
203
+ null ,
204
+ [],
205
+ 'EntityOne '
206
+ );
207
+ $ this ->assertEquals ($ expected , $ actual );
208
+ }
209
+
210
+ /**
211
+ * getAllObjects should throw TestFrameworkException exception if some data extends itself
212
+ */
213
+ public function testGetAllObjectsWithDataExtendsItself ()
214
+ {
215
+ $ this ->setUpMockDataObjectHander (self ::PARSER_OUTPUT_WITH_EXTEND_INVALID );
216
+
217
+ $ this ->expectException (\Magento \FunctionalTestingFramework \Exceptions \TestFrameworkException::class);
218
+ $ this ->expectExceptionMessage (
219
+ "Mftf Data can not extend from itself: "
220
+ . self ::PARSER_OUTPUT_WITH_EXTEND_INVALID ['entity ' ]['EntityOne ' ]['name ' ]
221
+ );
222
+
223
+ // Call the method under test
224
+ DataObjectHandler::getInstance ()->getAllObjects ();
225
+ }
226
+
227
+ /**
228
+ * getObject should throw TestFrameworkException exception if requested data extends itself
229
+ */
230
+ public function testGetObjectWithDataExtendsItself ()
231
+ {
232
+ $ this ->setUpMockDataObjectHander (self ::PARSER_OUTPUT_WITH_EXTEND_INVALID );
233
+
234
+ $ this ->expectException (\Magento \FunctionalTestingFramework \Exceptions \TestFrameworkException::class);
235
+ $ this ->expectExceptionMessage (
236
+ "Mftf Data can not extend from itself: "
237
+ . self ::PARSER_OUTPUT_WITH_EXTEND_INVALID ['entity ' ]['EntityOne ' ]['name ' ]
238
+ );
239
+
240
+ // Call the method under test
241
+ DataObjectHandler::getInstance ()->getObject (
242
+ self ::PARSER_OUTPUT_WITH_EXTEND_INVALID ['entity ' ]['EntityOne ' ]['name ' ]
243
+ );
244
+ }
245
+
246
+ /**
247
+ * Set up everything required to mock DataObjectHander::getInstance()
248
+ * The first call to getInstance() uses these mocks to emulate the parser, initializing internal state
249
+ * according to the PARSER_OUTPUT value
250
+ *
251
+ * @param array $entityDataArray
252
+ */
253
+ private function setUpMockDataObjectHander ($ entityDataArray )
254
+ {
255
+ // Clear DataObjectHandler singleton if already set
256
+ $ property = new \ReflectionProperty (DataObjectHandler::class, "INSTANCE " );
257
+ $ property ->setAccessible (true );
258
+ $ property ->setValue (null );
259
+
260
+ $ mockDataProfileSchemaParser = AspectMock::double (DataProfileSchemaParser::class, [
261
+ 'readDataProfiles ' => $ entityDataArray
262
+ ])->make ();
263
+
264
+ $ mockObjectManager = AspectMock::double (ObjectManager::class, [
265
+ 'create ' => $ mockDataProfileSchemaParser
266
+ ])->make ();
267
+
268
+ AspectMock::double (ObjectManagerFactory::class, [
269
+ 'getObjectManager ' => $ mockObjectManager
270
+ ]);
271
+ }
96
272
}
0 commit comments