4
4
5
5
use Magento \AcceptanceTestFramework \PageObject \Page \Page ;
6
6
use Magento \AcceptanceTestFramework \PageObject \Section \Section ;
7
+ use Magento \AcceptanceTestFramework \DataGenerator \Managers \DataManager ;
7
8
8
9
class ActionObject
9
10
{
@@ -67,39 +68,151 @@ public function getTimeout()
67
68
}
68
69
69
70
/**
70
- * Resolves all references
71
+ * Populate the resolved custom attributes array with lookup values for the following attributes:
72
+ *
73
+ * selector
74
+ * url
75
+ * userInput
76
+ *
77
+ * @return void
71
78
*/
72
79
public function resolveReferences ()
73
80
{
74
- if (empty ($ this ->resolvedCustomAttributes )){
75
- $ this ->resolveSelectorReference ();
81
+ if (empty ($ this ->resolvedCustomAttributes )) {
82
+ $ this ->resolveSelectorReferenceAndTimeout ();
76
83
$ this ->resolveUrlReference ();
84
+ $ this ->resolveUserInputReference ();
77
85
}
78
86
}
79
87
80
88
/**
81
- * Checks if selector is an attribute, and if the selector refers to a defined section.
82
- * If not, assume selector is CSS/xpath literal and leave it be.
89
+ * Look up the selector for SomeSectionName.ElementName and set it as the selector attribute in the
90
+ * resolved custom attributes. Also set the timeout value.
91
+ *
92
+ * e.g. {{SomeSectionName.ElementName}} becomes #login-button
93
+ *
94
+ * @return void
83
95
*/
84
- private function resolveSelectorReference ()
96
+ private function resolveSelectorReferenceAndTimeout ()
85
97
{
86
- if (array_key_exists ('selector ' , $ this ->actionAttributes )
87
- and array_key_exists (strtok ($ this ->actionAttributes ['selector ' ], '. ' ), Section::getSection ()) ) {
88
- list ($ section , $ element ) = explode ('. ' , $ this ->actionAttributes ['selector ' ]);
89
- $ this ->resolvedCustomAttributes ['selector ' ] = Section::getElementLocator ($ section , $ element );
90
- $ this ->timeout = Section::getElementTimeOut ($ section , $ element );
98
+ if (!array_key_exists ('selector ' , $ this ->actionAttributes )) {
99
+ return ;
91
100
}
101
+ $ selector = $ this ->actionAttributes ['selector ' ];
102
+
103
+ $ reference = $ this ->findReference ($ selector );
104
+ if ($ reference == null ) {
105
+ // Nothing to replace
106
+ return ;
107
+ }
108
+
109
+ list ($ sectionName , $ elementName ) = $ this ->stripAndSplitReference ($ reference );
110
+ $ section = Section::getSection ($ sectionName );
111
+ if ($ section == null ) {
112
+ // Bad section reference
113
+ return ;
114
+ }
115
+ $ replacement = Section::getElementLocator ($ sectionName , $ elementName );
116
+
117
+ $ this ->resolvedCustomAttributes ['selector ' ] = str_replace ($ reference , $ replacement , $ selector );
118
+ $ this ->timeout = Section::getElementTimeOut ($ sectionName , $ elementName );
92
119
}
93
120
94
121
/**
95
- * Checks if url is an attribute, and if the url given is a defined page.
96
- * If not, assume url is literal and leave it be.
122
+ * Look up the url for SomePageName and set it, with MAGENTO_BASE_URL prepended, as the url attribute in the
123
+ * resolved custom attributes.
124
+ *
125
+ * e.g. {{SomePageName}} becomes http://localhost:76543/some/url
126
+ *
127
+ * @return void
97
128
*/
98
129
private function resolveUrlReference ()
99
130
{
100
- if (array_key_exists ('url ' , $ this ->actionAttributes )
101
- and array_key_exists ($ this ->actionAttributes ['url ' ], Page::getPage ())) {
102
- $ this ->resolvedCustomAttributes ['url ' ] = $ _ENV ['MAGENTO_BASE_URL ' ] . Page::getPageUrl ($ this ->actionAttributes ['url ' ]);
131
+ if (!array_key_exists ('url ' , $ this ->actionAttributes )) {
132
+ return ;
133
+ }
134
+ $ url = $ this ->actionAttributes ['url ' ];
135
+
136
+ $ reference = $ this ->findReference ($ url );
137
+ if ($ reference == null ) {
138
+ // Nothing to replace
139
+ return ;
140
+ }
141
+
142
+ list ($ pageName ) = $ this ->stripAndSplitReference ($ reference );
143
+ $ page = Page::getPage ($ pageName );
144
+ if ($ page == null ) {
145
+ // Bad page reference
146
+ return ;
147
+ }
148
+ $ replacement = $ _ENV ['MAGENTO_BASE_URL ' ] . Page::getPageUrl ($ pageName );
149
+
150
+ $ this ->resolvedCustomAttributes ['url ' ] = str_replace ($ reference , $ replacement , $ url );
151
+ }
152
+
153
+
154
+ /**
155
+ * Look up the value for EntityDataObjectName.Key and set it as the userInput attribute in the resolved custom
156
+ * attributes.
157
+ *
158
+ * e.g. {{CustomerEntityFoo.FirstName}} becomes Jerry
159
+ *
160
+ * @return void
161
+ */
162
+ private function resolveUserInputReference ()
163
+ {
164
+ if (!array_key_exists ('userInput ' , $ this ->actionAttributes )) {
165
+ return ;
166
+ }
167
+ $ userInput = $ this ->actionAttributes ['userInput ' ];
168
+
169
+ $ reference = $ this ->findReference ($ userInput );
170
+ if ($ reference == null ) {
171
+ // Nothing to replace
172
+ return ;
173
+ }
174
+
175
+ list ($ entityName , $ entityKey ) = $ this ->stripAndSplitReference ($ userInput );
176
+ $ entityObj = DataManager::getInstance ()->getEntity ($ entityName );
177
+ if ($ entityObj == null ) {
178
+ // Bad entity reference
179
+ return ;
180
+ }
181
+
182
+ $ replacement = $ entityObj ->getDataByName ($ entityKey );
183
+ if ($ replacement == null ) {
184
+ // Bad entity.key reference
185
+ return ;
186
+ }
187
+
188
+ $ this ->resolvedCustomAttributes ['userInput ' ] = str_replace ($ reference , $ replacement , $ userInput );
189
+ }
190
+
191
+ /**
192
+ * Return an array containing the name (before the period) and key (after the period) in a {{reference.foo}}.
193
+ *
194
+ * @param string $reference
195
+ * @return string[] The name and key that is referenced.
196
+ */
197
+ private function stripAndSplitReference ($ reference )
198
+ {
199
+ $ strippedReference = str_replace ('}} ' , '' , str_replace ('{{ ' , '' , $ reference ));
200
+ return explode ('. ' , $ strippedReference );
201
+ }
202
+
203
+ /**
204
+ * Return a {{reference.foo}} if it exists in the string.
205
+ *
206
+ * @param string $str
207
+ * @return string|null
208
+ */
209
+ private function findReference ($ str )
210
+ {
211
+ preg_match ('/{{[\w.]+}}/ ' , $ str , $ matches );
212
+ if (empty ($ matches )) {
213
+ return null ;
214
+ } else {
215
+ return $ matches [0 ];
103
216
}
104
217
}
105
- }
218
+ }
0 commit comments