Permalink
Browse files

API change the way ajax handlers are invoked to Doc Comments

  • Loading branch information...
michalochman committed Aug 7, 2012
1 parent fe6e6c9 commit e48151a332d7307536a651da5a387fc006516d75
Showing with 20 additions and 6 deletions.
  1. +20 −6 features/bootstrap/SilverStripe/Test/Behaviour/BasicContext.php
@@ -113,24 +113,38 @@ public function handleAjaxBeforeStep(StepEvent $event)
*
* Don't unregister handler if we're dealing with modal windows
*
+ * Below are the strings that are matched against step name
+ * that is possibly using AJAX
+ * @match go to
+ * @match follow
+ * @match press
+ * @match click
+ * @match submit
+ *
* @AfterStep ~@modal
*/
public function handleAjaxAfterStep(StepEvent $event)
{
- if (!preg_match('/(go to|follow|press|click|submit)/i', $event->getStep()->getText())) {
- return;
- }
+ $method = new \ReflectionMethod(__CLASS__, substr(__METHOD__, strpos(__METHOD__, '::') + 2));
+ if (preg_match_all('#\* @match (.+)#', $method->getDocComment(), $m)) {
+ array_walk($m[1], create_function('&$val', '$val = trim($val);'));
+ $matches = implode('|', array_filter($m[1]));
- $this->handleAjaxTimeout();
+ if (!preg_match('/(?:' . $matches . ')/i', $event->getStep()->getText())) {
+ return;
+ }
- $javascript = <<<JS
+ $this->handleAjaxTimeout();
+
+ $javascript = <<<JS
if ('undefined' !== typeof window.jQuery) {
window.jQuery(document).off('ajaxStart.ss.test.behaviour');
window.jQuery(document).off('ajaxComplete.ss.test.behaviour');
window.jQuery(document).off('ajaxSuccess.ss.test.behaviour');
}
JS;
- $this->getSession()->executeScript($javascript);
+ $this->getSession()->executeScript($javascript);
+ }
}
public function handleAjaxTimeout()

4 comments on commit e48151a

@chillu

This comment has been minimized.

Show comment Hide comment
@chillu

chillu Aug 7, 2012

Contributor

So its only executed after scenarios flagged with @modal? Is that a built-in flag to behat?
"Modal" to me means a blocking UI, where a user is forced to take a specific action,
and can't interact with other elements. I don't see how that fits in this scenario here.

And those @modal scenarios are then further limited to only steps matching a certain naming convention?
My original concern is the same: Too much magic. Step definitions should be able to declare themselves
if they require special ajax treatment or not (rather than hiding it behind certain keywords in their naming).

Also: Can you please document new features like that in the README?

Contributor

chillu replied Aug 7, 2012

So its only executed after scenarios flagged with @modal? Is that a built-in flag to behat?
"Modal" to me means a blocking UI, where a user is forced to take a specific action,
and can't interact with other elements. I don't see how that fits in this scenario here.

And those @modal scenarios are then further limited to only steps matching a certain naming convention?
My original concern is the same: Too much magic. Step definitions should be able to declare themselves
if they require special ajax treatment or not (rather than hiding it behind certain keywords in their naming).

Also: Can you please document new features like that in the README?

@michalochman

This comment has been minimized.

Show comment Hide comment
@michalochman

michalochman Aug 7, 2012

Owner

It's executed for scenarios NOT tagged @modal. This is a custom tag introduced by me that will prevent executing JS whenever modal dialogs come up to prevent runtime failures.

Owner

michalochman replied Aug 7, 2012

It's executed for scenarios NOT tagged @modal. This is a custom tag introduced by me that will prevent executing JS whenever modal dialogs come up to prevent runtime failures.

@michalochman

This comment has been minimized.

Show comment Hide comment
@michalochman

michalochman Aug 7, 2012

Owner

Step definitions should be able to declare themselves if they require special ajax treatment or not (rather than hiding it behind certain keywords in their naming).

How would you define steps then?

Owner

michalochman replied Aug 7, 2012

Step definitions should be able to declare themselves if they require special ajax treatment or not (rather than hiding it behind certain keywords in their naming).

How would you define steps then?

@michalochman

This comment has been minimized.

Show comment Hide comment
@michalochman

michalochman Aug 7, 2012

Owner

And here's @modal usage.

Owner

michalochman replied Aug 7, 2012

And here's @modal usage.

Please sign in to comment.