11
11
use Magento \FunctionalTestingFramework \ObjectManager \ObjectHandlerInterface ;
12
12
use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
13
13
14
- // @codingStandardsIgnoreFile
15
14
class DataObjectHandler implements ObjectHandlerInterface
16
15
{
16
+ const __ENV = '_ENV ' ;
17
+ const _ENTITY = 'entity ' ;
18
+ const _NAME = 'name ' ;
19
+ const _TYPE = 'type ' ;
20
+ const _DATA = 'data ' ;
21
+ const _KEY = 'key ' ;
22
+ const _VALUE = 'value ' ;
23
+ const _UNIQUE = 'unique ' ;
24
+ const _PREFIX = 'prefix ' ;
25
+ const _SUFFIX = 'suffix ' ;
26
+ const _ARRAY = 'array ' ;
27
+ const _ITEM = 'item ' ;
28
+ const _VAR = 'var ' ;
29
+ const _ENTITY_TYPE = 'entityType ' ;
30
+ const _ENTITY_KEY = 'entityKey ' ;
31
+ const _SEPARATOR = '-> ' ;
32
+ const _REQUIRED_ENTITY = 'required-entity ' ;
33
+
17
34
/**
18
- * @var DataObjectHandler $DATA_OBJECT_HANDLER
35
+ * The singleton instance of this class
36
+ *
37
+ * @var DataObjectHandler $INSTANCE
19
38
*/
20
- private static $ DATA_OBJECT_HANDLER ;
39
+ private static $ INSTANCE ;
21
40
22
41
/**
23
- * @var array $arrayData
42
+ * A collection of entity data objects that were seen in XML files and the .env file
43
+ *
44
+ * @var EntityDataObject[] $entityDataObjects
24
45
*/
25
- private $ arrayData = [];
46
+ private $ entityDataObjects = [];
26
47
27
48
/**
28
- * @var array $data
49
+ * Constructor
29
50
*/
30
- private $ data = [];
31
-
32
- const ENV_DATA_OBJECT_NAME = '_ENV ' ;
33
-
34
- const ENTITY_DATA = 'entity ' ;
35
- const ENTITY_DATA_NAME = 'name ' ;
36
- const ENTITY_DATA_TYPE = 'type ' ;
37
-
38
- const DATA_VALUES = 'data ' ;
39
- const DATA_ELEMENT_KEY = 'key ' ;
40
- const DATA_ELEMENT_VALUE = 'value ' ;
41
- const DATA_ELEMENT_UNIQUENESS_ATTR = 'unique ' ;
42
- const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_PREFIX = 'prefix ' ;
43
- const DATA_ELEMENT_UNIQUENESS_ATTR_VALUE_SUFFIX = 'suffix ' ;
44
-
45
- const ARRAY_VALUES = 'array ' ;
46
- const ARRAY_ELEMENT_KEY = 'key ' ;
47
- const ARRAY_ELEMENT_ITEM = 'item ' ;
48
- const ARRAY_ELEMENT_ITEM_VALUE = 'value ' ;
49
-
50
- const VAR_VALUES = 'var ' ;
51
- const VAR_KEY = 'key ' ;
52
- const VAR_ENTITY = 'entityType ' ;
53
- const VAR_FIELD = 'entityKey ' ;
54
- const VAR_ENTITY_FIELD_SEPARATOR = '-> ' ;
55
-
56
- const REQUIRED_ENTITY = 'required-entity ' ;
57
- const REQUIRED_ENTITY_TYPE = 'type ' ;
58
- const REQUIRED_ENTITY_VALUE = 'value ' ;
51
+ private function __construct ()
52
+ {
53
+ $ parser = ObjectManagerFactory::getObjectManager ()->create (DataProfileSchemaParser::class);
54
+ $ parserOutput = $ parser ->readDataProfiles ();
55
+ if (!$ parserOutput ) {
56
+ return ;
57
+ }
58
+ $ this ->entityDataObjects = $ this ->processParserOutput ($ parserOutput );
59
+ $ this ->entityDataObjects [self ::__ENV] = $ this ->processEnvFile ();
60
+ }
59
61
60
62
/**
61
- * Singleton method to retrieve instance of DataArrayProcessor
63
+ * Return the singleton instance of this class. Initialize it if needed.
62
64
*
63
65
* @return DataObjectHandler
64
66
* @throws \Exception
65
67
*/
66
68
public static function getInstance ()
67
69
{
68
- if (!self ::$ DATA_OBJECT_HANDLER ) {
69
- self ::$ DATA_OBJECT_HANDLER = new DataObjectHandler ();
70
- self ::$ DATA_OBJECT_HANDLER ->initDataObjects ();
70
+ if (!self ::$ INSTANCE ) {
71
+ self ::$ INSTANCE = new DataObjectHandler ();
71
72
}
72
-
73
- return self ::$ DATA_OBJECT_HANDLER ;
73
+ return self ::$ INSTANCE ;
74
74
}
75
75
76
76
/**
77
- * DataArrayProcessor constructor.
78
- * @constructor
79
- */
80
- private function __construct ()
81
- {
82
- // private constructor
83
- }
84
-
85
- /**
86
- * Retrieves the object representation of data represented in data.xml
87
- * @param string $entityName
77
+ * Get an EntityDataObject by name
78
+ *
79
+ * @param string $name The name of the entity you want. Comes from the name attribute in data xml.
88
80
* @return EntityDataObject | null
89
81
*/
90
- public function getObject ($ entityName )
82
+ public function getObject ($ name )
91
83
{
92
- if (array_key_exists ($ entityName , $ this ->getAllObjects ())) {
93
- return $ this ->getAllObjects ()[$ entityName ];
84
+ $ allObjects = $ this ->getAllObjects ();
85
+
86
+ if (array_key_exists ($ name , $ allObjects )) {
87
+ return $ allObjects [$ name ];
94
88
}
95
89
96
90
return null ;
97
91
}
98
92
99
93
/**
100
- * Retrieves all object representations of all data represented in data.xml
101
- * @return array
94
+ * Get all EntityDataObjects
95
+ *
96
+ * @return EntityDataObject[]
102
97
*/
103
98
public function getAllObjects ()
104
99
{
105
- return $ this ->data ;
100
+ return $ this ->entityDataObjects ;
106
101
}
107
102
108
103
/**
109
- * Method to initialize parsing of data.xml and read into objects.
104
+ * Convert the contents of the .env file into a single EntityDataObject so that the values can be accessed like
105
+ * normal data.
110
106
*
111
- * @return void
112
- * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
107
+ * @return EntityDataObject|null
113
108
*/
114
- private function initDataObjects ()
109
+ private function processEnvFile ()
115
110
{
116
- $ entityParser = ObjectManagerFactory::getObjectManager ()->create (DataProfileSchemaParser::class);
117
- $ entityParsedData = $ entityParser ->readDataProfiles ();
118
-
119
- if (!$ entityParsedData ) {
120
- // No *Data.xml files found so give up
121
- return ;
122
- }
111
+ // These constants are defined in the bootstrap file
112
+ $ path = PROJECT_ROOT . DIRECTORY_SEPARATOR . '.env ' ;
123
113
124
- $ this ->arrayData = $ entityParsedData ;
125
- $ this ->parseEnvVariables ();
126
- $ this ->parseDataEntities ();
127
- }
114
+ if (file_exists ($ path )) {
115
+ $ vars = [];
116
+ $ lines = file ($ path );
128
117
129
- /**
130
- * Adds all .env variables defined in the PROJECT_ROOT as EntityDataObjects. This is to allow resolution
131
- * of these variables when referenced in a cest.
132
- * @return void
133
- */
134
- private function parseEnvVariables ()
135
- {
136
- $ envFilename = PROJECT_ROOT . DIRECTORY_SEPARATOR . '.env ' ;
137
- if (file_exists ($ envFilename )) {
138
- $ envData = [];
139
- $ envFile = file ($ envFilename );
140
- foreach ($ envFile as $ entry ) {
141
- $ params = explode ("= " , $ entry );
142
- if (count ($ params ) != 2 ) {
118
+ foreach ($ lines as $ line ) {
119
+ $ parts = explode ("= " , $ line );
120
+ if (count ($ parts ) != 2 ) {
143
121
continue ;
144
122
}
145
- $ envData [strtolower (trim ($ params [0 ]))] = trim ($ params [1 ]);
123
+ $ key = strtolower (trim ($ parts [0 ]));
124
+ $ value = trim ($ parts [1 ]);
125
+ $ vars [$ key ] = $ value ;
146
126
}
147
- $ envDataObject = new EntityDataObject (
148
- self ::ENV_DATA_OBJECT_NAME ,
149
- 'environment ' ,
150
- $ envData ,
151
- null ,
152
- null
153
- );
154
- $ this ->data [$ envDataObject ->getName ()] = $ envDataObject ;
127
+
128
+ return new EntityDataObject (self ::__ENV, 'environment ' , $ vars , null , null );
155
129
}
130
+
131
+ return null ;
156
132
}
157
133
158
134
/**
159
- * Parses array output of parses into EntityDataObjects.
160
- * @return void
135
+ * Convert the parser output into a collection of EntityDataObjects
136
+ *
137
+ * @param string[] $parserOutput primitive array output from the Magento parser
138
+ * @return EntityDataObject[]
161
139
*/
162
- private function parseDataEntities ( )
140
+ private function processParserOutput ( $ parserOutput )
163
141
{
164
- $ entities = $ this ->arrayData ;
142
+ $ entityDataObjects = [];
143
+ $ rawEntities = $ parserOutput [self ::_ENTITY ];
165
144
166
- foreach ($ entities [self ::ENTITY_DATA ] as $ entityName => $ entity ) {
167
- $ entityType = $ entity [self ::ENTITY_DATA_TYPE ];
168
-
169
- $ dataValues = [];
145
+ foreach ($ rawEntities as $ name => $ rawEntity ) {
146
+ $ type = $ rawEntity [self ::_TYPE ];
147
+ $ data = [];
170
148
$ linkedEntities = [];
171
- $ arrayValues = [];
149
+ $ values = [];
150
+ $ uniquenessData = [];
172
151
$ vars = [];
173
- $ uniquenessValues = [];
174
152
175
- if (array_key_exists (self ::DATA_VALUES , $ entity )) {
176
- $ dataValues = $ this ->parseDataElements ( $ entity );
177
- $ uniquenessValues = $ this ->parseUniquenessValues ( $ entity );
153
+ if (array_key_exists (self ::_DATA , $ rawEntity )) {
154
+ $ data = $ this ->processDataElements ( $ rawEntity );
155
+ $ uniquenessData = $ this ->processUniquenessData ( $ rawEntity );
178
156
}
179
157
180
- if (array_key_exists (self ::REQUIRED_ENTITY , $ entity )) {
181
- $ linkedEntities = $ this ->parseRequiredEntityElements ( $ entity );
158
+ if (array_key_exists (self ::_REQUIRED_ENTITY , $ rawEntity )) {
159
+ $ linkedEntities = $ this ->processLinkedEntities ( $ rawEntity );
182
160
}
183
161
184
- if (array_key_exists (self ::ARRAY_VALUES , $ entity )) {
185
- foreach ($ entity [self ::ARRAY_VALUES ] as $ arrayElement ) {
186
- $ arrayKey = $ arrayElement [self ::ARRAY_ELEMENT_KEY ];
187
- foreach ($ arrayElement [self ::ARRAY_ELEMENT_ITEM ] as $ arrayValue ) {
188
- $ arrayValues [] = $ arrayValue [self ::ARRAY_ELEMENT_ITEM_VALUE ];
162
+ if (array_key_exists (self ::_ARRAY , $ rawEntity )) {
163
+ $ arrays = $ rawEntity [self ::_ARRAY ];
164
+ foreach ($ arrays as $ array ) {
165
+ $ items = $ array [self ::_ITEM ];
166
+ foreach ($ items as $ item ) {
167
+ $ values [] = $ item [self ::_VALUE ];
189
168
}
190
-
191
- $ dataValues [strtolower ($ arrayKey )] = $ arrayValues ;
169
+ $ key = $ array [ self :: _KEY ];
170
+ $ data [strtolower ($ key )] = $ values ;
192
171
}
193
172
}
194
173
195
- if (array_key_exists (self ::VAR_VALUES , $ entity )) {
196
- $ vars = $ this ->parseVarElements ( $ entity );
174
+ if (array_key_exists (self ::_VAR , $ rawEntity )) {
175
+ $ vars = $ this ->processVarElements ( $ rawEntity );
197
176
}
198
177
199
- $ entityDataObject = new EntityDataObject (
200
- $ entityName ,
201
- $ entityType ,
202
- $ dataValues ,
203
- $ linkedEntities ,
204
- $ uniquenessValues ,
205
- $ vars
206
- );
178
+ $ entityDataObject = new EntityDataObject ($ name , $ type , $ data , $ linkedEntities , $ uniquenessData , $ vars );
207
179
208
- $ this -> data [$ entityDataObject ->getName ()] = $ entityDataObject ;
180
+ $ entityDataObjects [$ entityDataObject ->getName ()] = $ entityDataObject ;
209
181
}
210
- unset( $ entityName );
211
- unset( $ entity ) ;
182
+
183
+ return $ entityDataObjects ;
212
184
}
213
185
214
186
/**
215
187
* Parses <data> elements in an entity, and returns them as an array of "lowerKey"=>value.
216
- * @param array $entityData
217
- * @return array
188
+ *
189
+ * @param string[] $entityData
190
+ * @return string[]
218
191
*/
219
- private function parseDataElements ($ entityData )
192
+ private function processDataElements ($ entityData )
220
193
{
221
194
$ dataValues = [];
222
- foreach ($ entityData [self ::DATA_VALUES ] as $ dataElement ) {
223
- $ dataElementKey = strtolower ($ dataElement [self ::DATA_ELEMENT_KEY ]);
224
- $ dataElementValue = $ dataElement [self ::DATA_ELEMENT_VALUE ] ?? "" ;
195
+ foreach ($ entityData [self ::_DATA ] as $ dataElement ) {
196
+ $ dataElementKey = strtolower ($ dataElement [self ::_KEY ]);
197
+ $ dataElementValue = $ dataElement [self ::_VALUE ] ?? "" ;
225
198
$ dataValues [$ dataElementKey ] = $ dataElementValue ;
226
199
}
227
200
return $ dataValues ;
228
201
}
229
202
230
203
/**
231
204
* Parses through <data> elements in an entity to return an array of "DataKey" => "UniquenessAttribute"
232
- * @param array $entityData
233
- * @return array
205
+ *
206
+ * @param string[] $entityData
207
+ * @return string[]
234
208
*/
235
- private function parseUniquenessValues ($ entityData )
209
+ private function processUniquenessData ($ entityData )
236
210
{
237
211
$ uniquenessValues = [];
238
- foreach ($ entityData [self ::DATA_VALUES ] as $ dataElement ) {
239
- if (array_key_exists (self ::DATA_ELEMENT_UNIQUENESS_ATTR , $ dataElement )) {
240
- $ dataElementKey = strtolower ($ dataElement [self ::DATA_ELEMENT_KEY ]);
241
- $ uniquenessValues [$ dataElementKey ] = $ dataElement [self ::DATA_ELEMENT_UNIQUENESS_ATTR ];
212
+ foreach ($ entityData [self ::_DATA ] as $ dataElement ) {
213
+ if (array_key_exists (self ::_UNIQUE , $ dataElement )) {
214
+ $ dataElementKey = strtolower ($ dataElement [self ::_KEY ]);
215
+ $ uniquenessValues [$ dataElementKey ] = $ dataElement [self ::_UNIQUE ];
242
216
}
243
217
}
244
218
return $ uniquenessValues ;
245
219
}
246
220
247
221
/**
248
222
* Parses <required-entity> elements given entity, and returns them as an array of "EntityValue"=>"EntityType"
249
- * @param array $entityData
250
- * @return array
223
+ *
224
+ * @param string[] $entityData
225
+ * @return string[]
251
226
*/
252
- private function parseRequiredEntityElements ($ entityData )
227
+ private function processLinkedEntities ($ entityData )
253
228
{
254
229
$ linkedEntities = [];
255
- foreach ($ entityData [self ::REQUIRED_ENTITY ] as $ linkedEntity ) {
256
- $ linkedEntityName = $ linkedEntity [self ::REQUIRED_ENTITY_VALUE ];
257
- $ linkedEntityType = $ linkedEntity [self ::REQUIRED_ENTITY_TYPE ];
230
+ foreach ($ entityData [self ::_REQUIRED_ENTITY ] as $ linkedEntity ) {
231
+ $ linkedEntityName = $ linkedEntity [self ::_VALUE ];
232
+ $ linkedEntityType = $ linkedEntity [self ::_TYPE ];
258
233
259
234
$ linkedEntities [$ linkedEntityName ] = $ linkedEntityType ;
260
235
}
@@ -263,15 +238,16 @@ private function parseRequiredEntityElements($entityData)
263
238
264
239
/**
265
240
* Parses <var> elements in given entity, and returns them as an array of "Key"=> entityType -> entityKey
266
- * @param array $entityData
267
- * @return array
241
+ *
242
+ * @param string[] $entityData
243
+ * @return string[]
268
244
*/
269
- private function parseVarElements ($ entityData )
245
+ private function processVarElements ($ entityData )
270
246
{
271
247
$ vars = [];
272
- foreach ($ entityData [self ::VAR_VALUES ] as $ varElement ) {
273
- $ varKey = $ varElement [self ::VAR_KEY ];
274
- $ varValue = $ varElement [self ::VAR_ENTITY ] . self ::VAR_ENTITY_FIELD_SEPARATOR . $ varElement [self ::VAR_FIELD ];
248
+ foreach ($ entityData [self ::_VAR ] as $ varElement ) {
249
+ $ varKey = $ varElement [self ::_KEY ];
250
+ $ varValue = $ varElement [self ::_ENTITY_TYPE ] . self ::_SEPARATOR . $ varElement [self ::_ENTITY_KEY ];
275
251
$ vars [$ varKey ] = $ varValue ;
276
252
}
277
253
return $ vars ;
0 commit comments