From 66901a69686e2f2f41bbfa35f46ec3ad8666084d Mon Sep 17 00:00:00 2001 From: sam marshall Date: Tue, 15 Jan 2019 14:54:47 +0000 Subject: [PATCH] MDL-63977 Behat: Add generic way to get tag list --- lib/behat/behat_base.php | 10 ++++++++++ lib/tests/behat/behat_app.php | 17 +---------------- lib/tests/behat/behat_hooks.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php index a6cca8ad2f1..68738a17bf7 100644 --- a/lib/behat/behat_base.php +++ b/lib/behat/behat_base.php @@ -647,6 +647,16 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { return; } + /** + * Checks if the current scenario, or its feature, has a specified tag. + * + * @param string $tag Tag to check + * @return bool True if the tag exists in scenario or feature + */ + public function has_tag(string $tag) : bool { + return array_key_exists($tag, behat_hooks::get_tags_for_scenario()); + } + /** * Change browser window size. * - small: 640x480 diff --git a/lib/tests/behat/behat_app.php b/lib/tests/behat/behat_app.php index f5d723dd2c7..a211dcef9b2 100644 --- a/lib/tests/behat/behat_app.php +++ b/lib/tests/behat/behat_app.php @@ -29,7 +29,6 @@ require_once(__DIR__ . '/../../behat/behat_base.php'); use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\ExpectationException; -use Behat\Behat\Hook\Scope\BeforeScenarioScope; /** * Mobile/desktop app steps definitions. @@ -40,9 +39,6 @@ use Behat\Behat\Hook\Scope\BeforeScenarioScope; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class behat_app extends behat_base { - /** @var bool True if the current scenario has the app tag */ - protected $apptag = false; - /** @var stdClass Object with data about launched Ionic instance (if any) */ protected static $ionicrunning = null; @@ -55,17 +51,6 @@ class behat_app extends behat_base { return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; } - /** - * Checks tags before each scenario. - * - * @BeforeScenario - * @param BeforeScenarioScope $scope Scope information - */ - public function check_tags(BeforeScenarioScope $scope) { - $this->apptag = in_array('app', $scope->getScenario()->getTags()) || - in_array('app', $scope->getFeature()->getTags()); - } - /** * Opens the Moodle app in the browser. * @@ -101,7 +86,7 @@ class behat_app extends behat_base { global $CFG; // Check the app tag was set. - if (!$this->apptag) { + if (!$this->has_tag('app')) { throw new DriverException('Requires @app tag on scenario or feature.'); } diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index d5254b1b5dc..865831fc86a 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -103,6 +103,11 @@ class behat_hooks extends behat_base { */ protected static $runningsuite = ''; + /** + * @var array Array (with tag names in keys) of all tags in current scenario. + */ + protected static $scenariotags; + /** * Hook to capture BeforeSuite event so as to give access to moodle codebase. * This will try and catch any exception and exists if anything fails. @@ -384,6 +389,30 @@ class behat_hooks extends behat_base { // Run all test with medium (1024x768) screen size, to avoid responsive problems. $this->resize_window('medium'); + + // Set up the tags for current scenario. + self::fetch_tags_for_scenario($scope); + } + + /** + * Sets up the tags for the current scenario. + * + * @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope Scope + */ + protected static function fetch_tags_for_scenario(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scope) { + self::$scenariotags = array_flip(array_merge( + $scope->getScenario()->getTags(), + $scope->getFeature()->getTags() + )); + } + + /** + * Gets the tags for the current scenario + * + * @return array Array where key is tag name and value is an integer + */ + public static function get_tags_for_scenario() : array { + return self::$scenariotags; } /**