1111use Magento \FunctionalTestingFramework \ObjectManager \ObjectHandlerInterface ;
1212use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
1313
14- // @codingStandardsIgnoreFile
1514class DataObjectHandler implements ObjectHandlerInterface
1615{
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+
1734 /**
18- * @var DataObjectHandler $DATA_OBJECT_HANDLER
35+ * The singleton instance of this class
36+ *
37+ * @var DataObjectHandler $INSTANCE
1938 */
20- private static $ DATA_OBJECT_HANDLER ;
39+ private static $ INSTANCE ;
2140
2241 /**
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
2445 */
25- private $ arrayData = [];
46+ private $ entityDataObjects = [];
2647
2748 /**
28- * @var array $data
49+ * Constructor
2950 */
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+ }
5961
6062 /**
61- * Singleton method to retrieve instance of DataArrayProcessor
63+ * Return the singleton instance of this class. Initialize it if needed.
6264 *
6365 * @return DataObjectHandler
6466 * @throws \Exception
6567 */
6668 public static function getInstance ()
6769 {
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 ();
7172 }
72-
73- return self ::$ DATA_OBJECT_HANDLER ;
73+ return self ::$ INSTANCE ;
7474 }
7575
7676 /**
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.
8880 * @return EntityDataObject | null
8981 */
90- public function getObject ($ entityName )
82+ public function getObject ($ name )
9183 {
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 ];
9488 }
9589
9690 return null ;
9791 }
9892
9993 /**
100- * Retrieves all object representations of all data represented in data.xml
101- * @return array
94+ * Get all EntityDataObjects
95+ *
96+ * @return EntityDataObject[]
10297 */
10398 public function getAllObjects ()
10499 {
105- return $ this ->data ;
100+ return $ this ->entityDataObjects ;
106101 }
107102
108103 /**
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.
110106 *
111- * @return void
112- * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
107+ * @return EntityDataObject|null
113108 */
114- private function initDataObjects ()
109+ private function processEnvFile ()
115110 {
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 ' ;
123113
124- $ this ->arrayData = $ entityParsedData ;
125- $ this ->parseEnvVariables ();
126- $ this ->parseDataEntities ();
127- }
114+ if (file_exists ($ path )) {
115+ $ vars = [];
116+ $ lines = file ($ path );
128117
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 ) {
143121 continue ;
144122 }
145- $ envData [strtolower (trim ($ params [0 ]))] = trim ($ params [1 ]);
123+ $ key = strtolower (trim ($ parts [0 ]));
124+ $ value = trim ($ parts [1 ]);
125+ $ vars [$ key ] = $ value ;
146126 }
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 );
155129 }
130+
131+ return null ;
156132 }
157133
158134 /**
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[]
161139 */
162- private function parseDataEntities ( )
140+ private function processParserOutput ( $ parserOutput )
163141 {
164- $ entities = $ this ->arrayData ;
142+ $ entityDataObjects = [];
143+ $ rawEntities = $ parserOutput [self ::_ENTITY ];
165144
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 = [];
170148 $ linkedEntities = [];
171- $ arrayValues = [];
149+ $ values = [];
150+ $ uniquenessData = [];
172151 $ vars = [];
173- $ uniquenessValues = [];
174152
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 );
178156 }
179157
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 );
182160 }
183161
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 ];
189168 }
190-
191- $ dataValues [strtolower ($ arrayKey )] = $ arrayValues ;
169+ $ key = $ array [ self :: _KEY ];
170+ $ data [strtolower ($ key )] = $ values ;
192171 }
193172 }
194173
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 );
197176 }
198177
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 );
207179
208- $ this -> data [$ entityDataObject ->getName ()] = $ entityDataObject ;
180+ $ entityDataObjects [$ entityDataObject ->getName ()] = $ entityDataObject ;
209181 }
210- unset( $ entityName );
211- unset( $ entity ) ;
182+
183+ return $ entityDataObjects ;
212184 }
213185
214186 /**
215187 * 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[]
218191 */
219- private function parseDataElements ($ entityData )
192+ private function processDataElements ($ entityData )
220193 {
221194 $ 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 ] ?? "" ;
225198 $ dataValues [$ dataElementKey ] = $ dataElementValue ;
226199 }
227200 return $ dataValues ;
228201 }
229202
230203 /**
231204 * 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[]
234208 */
235- private function parseUniquenessValues ($ entityData )
209+ private function processUniquenessData ($ entityData )
236210 {
237211 $ 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 ];
242216 }
243217 }
244218 return $ uniquenessValues ;
245219 }
246220
247221 /**
248222 * 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[]
251226 */
252- private function parseRequiredEntityElements ($ entityData )
227+ private function processLinkedEntities ($ entityData )
253228 {
254229 $ 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 ];
258233
259234 $ linkedEntities [$ linkedEntityName ] = $ linkedEntityType ;
260235 }
@@ -263,15 +238,16 @@ private function parseRequiredEntityElements($entityData)
263238
264239 /**
265240 * 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[]
268244 */
269- private function parseVarElements ($ entityData )
245+ private function processVarElements ($ entityData )
270246 {
271247 $ 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 ];
275251 $ vars [$ varKey ] = $ varValue ;
276252 }
277253 return $ vars ;
0 commit comments