/
DrupalAwareInitializer.php
166 lines (139 loc) · 5.05 KB
/
DrupalAwareInitializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Drupal\DrupalExtension\Context\Initializer;
use Behat\Behat\Context\Initializer\ContextInitializer,
Behat\Behat\Context\Context,
Behat\Behat\Event\ScenarioEvent,
Behat\Behat\Event\OutlineEvent;
use Drupal\Drupal;
use Drupal\DrupalExtension\Context\DrupalContext;
use Drupal\DrupalExtension\Context\DrupalAwareInterface;
use Drupal\DrupalExtension\Context\DrupalSubContextFinderInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Finder\Finder;
class DrupalAwareInitializer implements ContextInitializer, EventSubscriberInterface {
private $drupal, $parameters, $dispatcher;
public function __construct(Drupal $drupal, array $parameters, EventDispatcher $dispatcher) {
$this->drupal = $drupal;
$this->parameters = $parameters;
$this->dispatcher = $dispatcher;
}
/**
* {@inheritdocs}
*/
public function initializeContext(Context $context) {
// Set Drupal driver manager.
$context->setDrupal($this->drupal);
// Set event dispatcher.
$context->setDispatcher($this->dispatcher);
// Add all parameters to the context.
$context->setDrupalParameters($this->parameters);
// Add commonly used parameters as proper class variables.
if (isset($this->parameters['basic_auth'])) {
$context->basic_auth = $this->parameters['basic_auth'];
}
// 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 DrupalSubContextFinderInterface) {
$paths += $driver->getSubContextPaths();
}
}
}
// Additional subcontext locations may be specified manually in behat.yml.
if (isset($this->parameters['subcontexts']['paths'])) {
$paths = array_merge($paths, $this->parameters['subcontexts']['paths']);
}
foreach ($paths as $path) {
if ($subcontexts = $this->findAvailableSubContexts($path)) {
$this->loadSubContexts($subcontexts);
}
}
$context->initializeSubContexts();
}
}
public function loadSubContexts($subcontexts) {
foreach ($subcontexts as $path => $subcontext) {
if (!file_exists($path)) {
throw new \RuntimeException(sprintf('Subcontext path %s path does not exist.', $path));
}
// Load file.
require_once $path;
}
}
public function supports(ContextInterface $context) {
// @todo Create a DrupalAwareInterface instead, so developers don't have to
// directly extend the DrupalContext class.
return $context instanceof DrupalContext;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents() {
return array(
'beforeScenario' => array('prepareDefaultDrupalDriver', 11),
'beforeOutline' => array('prepareDefaultDrupalDriver', 11),
);
}
/**
* 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) {
$scenario = $event instanceof ScenarioEvent ? $event->getScenario() : $event->getOutline();
// Set the default driver.
$driver = $this->parameters['default_driver'];
foreach ($scenario->getTags() as $tag) {
if (isset($this->parameters[$tag . '_driver'])) {
$driver = $this->parameters[$tag . '_driver'];
}
}
$this->drupal->setDefaultDriverName($driver);
}
/**
* Find Sub-contexts matching a given pattern located at the passed path.
*
* @param string $path
* Absolute path to the directory to search for sub-contexts.
* @param string $pattern
* File pattern to match. Defaults to `*.behat.inc`.
*
* @return array
* An array of paths.
*/
public function findAvailableSubContexts($path, $pattern = '*.behat.inc') {
$paths = array();
$finder = new Finder();
$iterator = $finder
->files()
->name($pattern)
->in($path);
foreach ($iterator as $found) {
$paths[$found->getRealPath()] = $found->getFileName();
}
return $paths;
}
}