Completed
Pull Request — 3.1 (#290)
by
unknown
09:17
created

DrupalExtension   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 26
c 0
b 0
f 0
lcom 2
cbo 10
dl 0
loc 269
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A getConfigKey() 0 3 1
A initialize() 0 2 1
A load() 0 12 1
A process() 0 6 1
B configure() 0 98 1
A loadParameters() 0 10 2
A loadBlackBox() 0 4 1
A loadDrupal() 0 6 2
B loadDrush() 0 19 7
A setDrushOptions() 0 6 2
A processDriverPass() 0 4 1
A processEventSubscriberPass() 0 4 1
A processEnvironmentReaderPass() 0 9 2
A processClassGenerator() 0 4 1
1
<?php
2
3
namespace Drupal\DrupalExtension\ServiceContainer;
4
5
use Behat\Behat\Context\ServiceContainer\ContextExtension;
6
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
7
use Behat\Testwork\ServiceContainer\ExtensionManager;
8
use Behat\Testwork\ServiceContainer\ServiceProcessor;
9
use Drupal\DrupalExtension\Compiler\DriverPass;
10
use Drupal\DrupalExtension\Compiler\EventSubscriberPass;
11
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\Loader\FileLoader;
16
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
17
18
class DrupalExtension implements ExtensionInterface {
19
20
  /**
21
   * Extension configuration ID.
22
   */
23
  const DRUPAL_ID = 'drupal';
24
25
  /**
26
   * Selectors handler ID.
27
   */
28
  const SELECTORS_HANDLER_ID = 'drupal.selectors_handler';
29
30
  /**
31
   * @var ServiceProcessor
32
   */
33
  private $processor;
34
35
  /**
36
   * Initializes compiler pass.
37
   *
38
   * @param null|ServiceProcessor $processor
39
   */
40
  public function __construct(ServiceProcessor $processor = null) {
41
    $this->processor = $processor ? : new ServiceProcessor();
42
  }
43
44
  /**
45
   * {@inheritDoc}
46
   */
47
  public function getConfigKey() {
48
    return self::DRUPAL_ID;
49
  }
50
51
  /**
52
   * {@inheritDoc}
53
   */
54
  public function initialize(ExtensionManager $extensionManager) {
55
  }
56
57
  /**
58
   * {@inheritDoc}
59
   */
60
  public function load(ContainerBuilder $container, array $config) {
61
    $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/config'));
62
    $loader->load('services.yml');
63
    $container->setParameter('drupal.drupal.default_driver', $config['default_driver']);
64
65
    $this->loadParameters($container, $config);
66
67
    // Setup any drivers if requested.
68
    $this->loadBlackbox($loader, $config);
69
    $this->loadDrupal($loader, $container, $config);
70
    $this->loadDrush($loader, $container, $config);
71
  }
72
73
  /**
74
   * {@inheritDoc}
75
   */
76
  public function process(ContainerBuilder $container) {
77
    $this->processDriverPass($container);
78
    $this->processEventSubscriberPass($container);
79
    $this->processEnvironmentReaderPass($container);
80
    $this->processClassGenerator($container);
81
  }
82
83
  /**
84
   * {@inheritDoc}
85
   */
86
  public function configure(ArrayNodeDefinition $builder) {
87
    $builder->
88
      children()->
89
        scalarNode('default_driver')->
90
          defaultValue('blackbox')->
91
          info('Use "blackbox" to test remote site. See "api_driver" for easier integration.')->
92
        end()->
93
        scalarNode('api_driver')->
94
          defaultValue('drush')->
95
          info('Bootstraps drupal through "drupal8" or "drush".')->
96
        end()->
97
        scalarNode('drush_driver')->
98
          defaultValue('drush')->
99
        end()->
100
        arrayNode('region_map')->
101
          info("Targeting content in specific regions can be accomplished once those regions have been defined." . PHP_EOL
102
            . '  My region: "#css-selector"' . PHP_EOL
103
            . '  Content: "#main .region-content"'. PHP_EOL
104
            . '  Right sidebar: "#sidebar-second"'. PHP_EOL
105
          )->
106
          useAttributeAsKey('key')->
107
          prototype('variable')->
108
          end()->
109
        end()->
110
        arrayNode('text')->
111
          info(
112
              'Text strings, such as Log out or the Username field can be altered via behat.yml if they vary from the default values.' . PHP_EOL
113
            . '  log_out: "Sign out"' . PHP_EOL
114
            . '  log_in: "Sign in"' . PHP_EOL
115
            . '  password_field: "Enter your password"' . PHP_EOL
116
            . '  username_field: "Nickname"'
117
          )->
118
          addDefaultsIfNotSet()->
119
          children()->
120
            scalarNode('log_in')->
121
              defaultValue('Log in')->
122
            end()->
123
            scalarNode('log_out')->
124
              defaultValue('Log out')->
125
            end()->
126
            scalarNode('password_field')->
127
              defaultValue('Password')->
128
            end()->
129
            scalarNode('username_field')->
130
              defaultValue('Username')->
131
            end()->
132
          end()->
133
        end()->
134
        arrayNode('selectors')->
135
          children()->
136
            scalarNode('message_selector')->end()->
137
            scalarNode('error_message_selector')->end()->
138
            scalarNode('success_message_selector')->end()->
139
            scalarNode('warning_message_selector')->end()->
140
          end()->
141
        end()->
142
        // Drupal drivers.
143
        arrayNode('blackbox')->
144
        end()->
145
        arrayNode('drupal')->
146
          children()->
147
            scalarNode('drupal_root')->end()->
148
          end()->
149
        end()->
150
        arrayNode('drush')->
151
          children()->
152
            scalarNode('alias')->end()->
153
            scalarNode('binary')->defaultValue('drush')->end()->
154
            scalarNode('root')->end()->
155
            scalarNode('global_options')->end()->
156
          end()->
157
        end()->
158
        // Subcontext paths.
159
        arrayNode('subcontexts')->
160
          info(
161
              'The Drupal Extension is capable of discovering additional step-definitions provided by subcontexts.' . PHP_EOL
162
            . 'Module authors can provide these in files following the naming convention of foo.behat.inc. Once that module is enabled, the Drupal Extension will load these.' . PHP_EOL
163
            . PHP_EOL
164
            . 'Additional subcontexts can be loaded by either placing them in the bootstrap directory (typically features/bootstrap) or by adding them to behat.yml.'
165
          )->
166
          addDefaultsIfNotSet()->
167
          children()->
168
            arrayNode('paths')->
169
              info(
170
                '- /path/to/additional/subcontexts' . PHP_EOL
171
              . '- /another/path'
172
              )->
173
              useAttributeAsKey('key')->
174
              prototype('variable')->end()->
175
            end()->
176
            scalarNode('autoload')->
177
              defaultValue(TRUE)->
178
            end()->
179
          end()->
180
        end()->
181
      end()->
182
    end();
183
  }
184
185
  /**
186
   * Load test parameters.
187
   */
188
  private function loadParameters(ContainerBuilder $container, array $config) {
189
    // Store config in parameters array to be passed into the DrupalContext.
190
    $drupal_parameters = array();
191
    foreach ($config as $key => $value) {
192
      $drupal_parameters[$key] = $value;
193
    }
194
    $container->setParameter('drupal.parameters', $drupal_parameters);
195
196
    $container->setParameter('drupal.region_map', $config['region_map']);
197
  }
198
199
  /**
200
   * Load the blackbox driver.
201
   */
202
  private function loadBlackBox(FileLoader $loader, array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
    // Always include the blackbox driver.
204
    $loader->load('drivers/blackbox.yml');
205
  }
206
207
  /**
208
   * Load the Drupal driver.
209
   */
210
  private function loadDrupal(FileLoader $loader, ContainerBuilder $container, array $config) {
211
    if (isset($config['drupal'])) {
212
      $loader->load('drivers/drupal.yml');
213
      $container->setParameter('drupal.driver.drupal.drupal_root', $config['drupal']['drupal_root']);
214
    }
215
  }
216
217
  /**
218
   * Load the Drush driver.
219
   */
220
  private function loadDrush(FileLoader $loader, ContainerBuilder $container, array $config) {
221
    if (isset($config['drush'])) {
222
      $loader->load('drivers/drush.yml');
223
      if (!isset($config['drush']['alias']) && !isset($config['drush']['root'])) {
224
        throw new \RuntimeException('Drush `alias` or `root` path is required for the Drush driver.');
225
      }
226
      $config['drush']['alias'] = isset($config['drush']['alias']) ? $config['drush']['alias'] : FALSE;
227
      $container->setParameter('drupal.driver.drush.alias', $config['drush']['alias']);
228
229
      $config['drush']['binary'] = isset($config['drush']['binary']) ? $config['drush']['binary'] : 'drush';
230
      $container->setParameter('drupal.driver.drush.binary', $config['drush']['binary']);
231
232
      $config['drush']['root'] = isset($config['drush']['root']) ? $config['drush']['root'] : FALSE;
233
      $container->setParameter('drupal.driver.drush.root', $config['drush']['root']);
234
235
      // Set global arguments.
236
      $this->setDrushOptions($container, $config);
237
    }
238
  }
239
240
  /**
241
   * Set global drush arguments.
242
   */
243
  private function setDrushOptions(ContainerBuilder $container, array $config) {
244
    if (isset($config['drush']['global_options'])) {
245
      $definition = $container->getDefinition('drupal.driver.drush');
246
      $definition->addMethodCall('setArguments', array($config['drush']['global_options']));
247
    }
248
  }
249
250
  /**
251
   * Process the Driver Pass.
252
   */
253
  private function processDriverPass(ContainerBuilder $container) {
254
    $driverPass = new DriverPass();
255
    $driverPass->process($container);
256
  }
257
258
  /**
259
   * Process the Event Subscriber Pass.
260
   */
261
  private function processEventSubscriberPass(ContainerBuilder $container) {
262
    $eventSubscriberPass = new EventSubscriberPass();
263
    $eventSubscriberPass->process($container);
264
  }
265
266
  /**
267
   * Process the Environment Reader pass.
268
   */
269
  private function processEnvironmentReaderPass(ContainerBuilder $container) {
270
    // Register Behat context readers.
271
    $references = $this->processor->findAndSortTaggedServices($container, ContextExtension::READER_TAG);
272
    $definition = $container->getDefinition('drupal.context.environment.reader');
273
274
    foreach ($references as $reference) {
275
      $definition->addMethodCall('registerContextReader', array($reference));
276
    }
277
  }
278
279
  /**
280
   * Switch to custom class generator.
281
   */
282
  private function processClassGenerator(ContainerBuilder $container) {
283
    $definition = new Definition('Drupal\DrupalExtension\Context\ContextClass\ClassGenerator');
284
    $container->setDefinition(ContextExtension::CLASS_GENERATOR_TAG . '.simple', $definition);
285
  }
286
}
287