Code Duplication    Length = 23-38 lines in 2 locations

src/SilverStripe/BehatExtension/Context/BasicContext.php 2 locations

@@ 140-177 (lines=38) @@
137
     *
138
     * @BeforeStep
139
     */
140
	public function handleAjaxBeforeStep(StepEvent $event) {
141
		try{
142
			$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
143
			$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
144
145
			if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
146
				return;
147
			}
148
149
			$javascript = <<<JS
150
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.on) {
151
    window.jQuery(document).on('ajaxStart.ss.test.behaviour', function(){
152
        window.__ajaxStatus = function() {
153
            return 'waiting';
154
        };
155
    });
156
    window.jQuery(document).on('ajaxComplete.ss.test.behaviour', function(e, jqXHR){
157
        if (null === jqXHR.getResponseHeader('X-ControllerURL')) {
158
            window.__ajaxStatus = function() {
159
                return 'no ajax';
160
            };
161
        }
162
    });
163
    window.jQuery(document).on('ajaxSuccess.ss.test.behaviour', function(e, jqXHR){
164
        if (null === jqXHR.getResponseHeader('X-ControllerURL')) {
165
            window.__ajaxStatus = function() {
166
                return 'success';
167
            };
168
        }
169
    });
170
}
171
JS;
172
			$this->getSession()->wait(500); // give browser a chance to process and render response
173
			$this->getSession()->executeScript($javascript);
174
		}catch(\WebDriver\Exception $e){
175
			$this->logException($e);
176
        }
177
    }
178
179
    /**
180
     * Wait for the __ajaxStatus()to return anything but 'waiting'.
@@ 187-209 (lines=23) @@
184
     *
185
     * @AfterStep ~@modal
186
     */
187
	public function handleAjaxAfterStep(StepEvent $event) {
188
		try{
189
			$ajaxEnabledSteps = $this->getMainContext()->getAjaxSteps();
190
			$ajaxEnabledSteps = implode('|', array_filter($ajaxEnabledSteps));
191
192
			if (empty($ajaxEnabledSteps) || !preg_match('/(' . $ajaxEnabledSteps . ')/i', $event->getStep()->getText())) {
193
				return;
194
			}
195
196
			$this->handleAjaxTimeout();
197
198
			$javascript = <<<JS
199
if ('undefined' !== typeof window.jQuery && 'undefined' !== typeof window.jQuery.fn.off) {
200
window.jQuery(document).off('ajaxStart.ss.test.behaviour');
201
window.jQuery(document).off('ajaxComplete.ss.test.behaviour');
202
window.jQuery(document).off('ajaxSuccess.ss.test.behaviour');
203
}
204
JS;
205
			$this->getSession()->executeScript($javascript);
206
		}catch(\WebDriver\Exception $e){
207
			$this->logException($e);
208
        }
209
    }
210
211
	public function handleAjaxTimeout() {
212
        $timeoutMs = $this->getMainContext()->getAjaxTimeout();