@@ -76,6 +76,96 @@ class MagentoWebDriver extends WebDriver
76
76
LC_MESSAGES => null ,
77
77
];
78
78
79
+ public function _initialize ()
80
+ {
81
+ $ this ->sanitizeConfig ();
82
+ parent ::_initialize ();
83
+ }
84
+
85
+ public function _resetConfig ()
86
+ {
87
+ parent ::_resetConfig ();
88
+ $ this ->sanitizeConfig ();
89
+ }
90
+
91
+ /**
92
+ * Sanitizes URL and Selenium Variables, then assigns them to the config array.
93
+ * @return void
94
+ */
95
+ private function sanitizeConfig ()
96
+ {
97
+ if ($ this ->config ['url ' ] === "" ) {
98
+ trigger_error ("MAGENTO_BASE_URL must be defined in .env " , E_USER_ERROR );
99
+ }
100
+
101
+ //Determine if url sanitize is required
102
+ if (!preg_match ("/(http|https):\/\/[\w.:]+\// " , $ this ->config ['url ' ])) {
103
+ $ urlParts = parse_url ($ this ->config ['url ' ]);
104
+
105
+ if (!isset ($ urlParts ['scheme ' ])) {
106
+ $ urlParts ['scheme ' ] = "http " ;
107
+ }
108
+ if (!isset ($ urlParts ['host ' ])) {
109
+ $ urlParts ['host ' ] = rtrim ($ urlParts ['path ' ], "/ " );
110
+ unset($ urlParts ['path ' ]);
111
+ }
112
+
113
+ if (!isset ($ urlParts ['path ' ])) {
114
+ $ urlParts ['path ' ] = "/ " ;
115
+ } else {
116
+ $ urlParts ['path ' ] = rtrim ($ urlParts ['path ' ], "/ " ) . "/ " ;
117
+ }
118
+
119
+ $ _ENV ['MAGENTO_BASE_URL ' ] = str_replace ("/// " , "// " , $ this ->build_url ($ urlParts ));
120
+ $ this ->config ['url ' ] = str_replace ("/// " , "// " , $ this ->build_url ($ urlParts ));
121
+ }
122
+
123
+ //Assign default Values to Selenium configs if they are defined
124
+ if ($ this ->config ['protocol ' ] == '%SELENIUM_PROTOCOL% ' ) {
125
+ $ this ->config ['protocol ' ] = "http " ;
126
+ }
127
+ if ($ this ->config ['host ' ] == '%SELENIUM_HOST% ' ) {
128
+ $ this ->config ['host ' ] = "127.0.0.1 " ;
129
+ }
130
+ if ($ this ->config ['port ' ] == '%SELENIUM_PORT% ' ) {
131
+ $ this ->config ['port ' ] = "4444 " ;
132
+ }
133
+ if ($ this ->config ['path ' ] == '%SELENIUM_PATH% ' ) {
134
+ $ this ->config ['path ' ] = "/wd/hub " ;
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Returns url from $parts given, used with parse_url output for convenience.
140
+ * This only exists because of deprecation of http_build_url, which does the exact same thing as the code below.
141
+ * @param array $parts
142
+ * @return string
143
+ */
144
+ private function build_url (array $ parts ) {
145
+ $ get = function ($ key ) use ($ parts ) {
146
+ return isset ($ parts [$ key ]) ? $ parts [$ key ] : null ;
147
+ };
148
+
149
+ $ pass = $ get ('pass ' );
150
+ $ user = $ get ('user ' );
151
+ $ userinfo = $ pass !== null ? "$ user: $ pass " : $ user ;
152
+ $ port = $ get ('port ' );
153
+ $ scheme = $ get ('scheme ' );
154
+ $ query = $ get ('query ' );
155
+ $ fragment = $ get ('fragment ' );
156
+ $ authority =
157
+ ($ userinfo !== null ? "$ userinfo@ " : '' ) .
158
+ $ get ('host ' ) .
159
+ ($ port ? ": $ port " : '' );
160
+
161
+ return
162
+ (strlen ($ scheme ) ? "$ scheme: " : '' ) .
163
+ (strlen ($ authority ) ? "// $ authority " : '' ) .
164
+ $ get ('path ' ) .
165
+ (strlen ($ query ) ? "? $ query " : '' ) .
166
+ (strlen ($ fragment ) ? "# $ fragment " : '' );
167
+ }
168
+
79
169
/**
80
170
* Returns URL of a host.
81
171
*
0 commit comments