/** * Returns the Behat context that corresponds with the given class name. * * This is inspired by InitializedContextEnvironment::getContext() but also * returns subclasses of the given class name. This allows us to retrieve for * example DrupalContext even if it is overridden in a project. * * @param string $class * A fully namespaced class name. * * @return \Behat\Behat\Context\Context|false * The requested context, or FALSE if the context is not registered. */ protected function getContext($class) { /** @var InitializedContextEnvironment $environment */ $environment = $this->drupal->getEnvironment(); foreach ($environment->getContexts() as $context) { if ($context instanceof $class) { return $context; } } return FALSE; }
/** * Finds and loads available subcontext classes. */ private function findSubContextClasses() { $class_names = array(); // Initialize any available sub-contexts. if (isset($this->parameters['subcontexts'])) { $paths = array(); // Drivers may specify paths to subcontexts. if ($this->parameters['subcontexts']['autoload']) { foreach ($this->drupal->getDrivers() as $name => $driver) { if ($driver instanceof SubDriverFinderInterface) { $paths += $driver->getSubDriverPaths(); } } } // Additional subcontext locations may be specified manually in behat.yml. if (isset($this->parameters['subcontexts']['paths'])) { $paths = array_merge($paths, $this->parameters['subcontexts']['paths']); } // Load each class. foreach ($paths as $path) { if ($subcontexts = $this->findAvailableSubContexts($path)) { $this->loadSubContexts($subcontexts); } } // Find all subcontexts, excluding abstract base classes. $classes = get_declared_classes(); foreach ($classes as $class) { $reflect = new \ReflectionClass($class); if (!$reflect->isAbstract() && $reflect->implementsInterface('Drupal\\DrupalExtension\\Context\\DrupalSubContextInterface')) { $class_names[] = $class; } } } return $class_names; }
/** * Configures default Drupal driver to use before each scenario or outline. * * `@api` tagged scenarios will get the `api_driver` as the default driver. * * Other scenarios get the `default_driver` as the default driver. * * @param ScenarioEvent|OutlineEvent $event */ public function prepareDefaultDrupalDriver($event) { $feature = $event->getFeature(); $scenario = $event instanceof ScenarioLikeTested ? $event->getScenario() : $event->getOutline(); // Get the default driver. $driver = $this->parameters['default_driver']; foreach (array_merge($feature->getTags(), $scenario->getTags()) as $tag) { if (!empty($this->parameters[$tag . '_driver'])) { $driver = $this->parameters[$tag . '_driver']; } } // Set the default driver. $this->drupal->setDefaultDriverName($driver); // Set the environment. $environment = $event->getEnvironment(); $this->drupal->setEnvironment($environment); }