SelectFieldContext   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 135
Duplicated Lines 47.41 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 64
loc 135
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B assertSelectOptions() 32 32 4
B assertNoSelectOptions() 32 32 4
A theOptionFromIsSelected() 0 3 1
A theOptionFromIsNotSelected() 0 3 1
B getSelectedOptionByLabel() 0 21 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Gherkin\Node\TableNode;
6
use Behat\Mink\Element\NodeElement;
7
use Behat\Mink\Exception\ExpectationException;
8
9
/**
10
 * Class SelectFieldContext.
11
 *
12
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
13
 */
14
class SelectFieldContext extends RawMinkContext {
15
16
  /**
17
   * Checks that the given select field has the options listed in the table.
18
   *
19
   * @Then I should have the following options for :select:
20
   */
21 View Code Duplication
  public function assertSelectOptions($select, TableNode $options) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    // Retrieve the specified field.
23
    if (!$field = $this->getSession()->getPage()->findField($select)) {
24
      throw new ExpectationException("Field '$select' not found.", $this->getSession());
25
    }
26
27
    /* @var \Behat\Mink\Element\NodeElement $field */
28
29
    // Check that the specified field is a <select> field.
30
    $this->assertElementType($field, 'select');
31
32
    // Retrieve the options table from the test scenario and flatten it.
33
    $expected_options = $options->getColumnsHash();
34
    array_walk($expected_options, function (&$value) {
35
      $value = reset($value);
36
    });
37
38
    // Retrieve the actual options that are shown in the page.
39
    $actual_options = $field->findAll('css', 'option');
40
41
    // Convert into a flat list of option text strings.
42
    array_walk($actual_options, function (NodeElement &$value) {
43
      $value = $value->getText();
44
    });
45
46
    // Check that all expected options are present.
47
    foreach ($expected_options as $expected_option) {
48
      if (!in_array($expected_option, $actual_options)) {
49
        throw new ExpectationException("Option '$expected_option' is missing from select list '$select'.", $this->getSession());
50
      }
51
    }
52
  }
53
54
  /**
55
   * Checks that the given select field doesn't have the listed options.
56
   *
57
   * @Then I should not have the following options for :select:
58
   */
59 View Code Duplication
  public function assertNoSelectOptions($select, TableNode $options) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    // Retrieve the specified field.
61
    if (!$field = $this->getSession()->getPage()->findField($select)) {
62
      throw new ExpectationException("Field '$select' not found.", $this->getSession());
63
    }
64
65
    /* @var \Behat\Mink\Element\NodeElement $field */
66
67
    // Check that the specified field is a <select> field.
68
    $this->assertElementType($field, 'select');
69
70
    // Retrieve the options table from the test scenario and flatten it.
71
    $expected_options = $options->getColumnsHash();
72
    array_walk($expected_options, function (&$value) {
73
      $value = reset($value);
74
    });
75
76
    // Retrieve the actual options that are shown in the page.
77
    $actual_options = $field->findAll('css', 'option');
78
79
    // Convert into a flat list of option text strings.
80
    array_walk($actual_options, function (NodeElement &$value) {
81
      $value = $value->getText();
82
    });
83
84
    // Check that none of the expected options are present.
85
    foreach ($expected_options as $expected_option) {
86
      if (in_array($expected_option, $actual_options)) {
87
        throw new ExpectationException("Option '$expected_option' is unexpectedly found in select list '$select'.", $this->getSession());
88
      }
89
    }
90
  }
91
92
  /**
93
   * Assert that given option field is selected.
94
   *
95
   * @Then the option :option_label from select :select is selected
96
   */
97
  public function theOptionFromIsSelected($select, $option_label) {
98
    $this->getSelectedOptionByLabel($select, $option_label);
99
  }
100
101
  /**
102
   * Assert that given option field is not selected.
103
   *
104
   * @Then the option :option_label from select :select is not selected
105
   */
106
  public function theOptionFromIsNotSelected($select, $option_label) {
107
    $this->getSelectedOptionByLabel($select, $option_label, FALSE);
108
  }
109
110
  /**
111
   * Returns an option from a specific select.
112
   *
113
   * @param string $select
114
   *   The select name.
115
   * @param string $option_label
116
   *   The option text.
117
   * @param bool $check_selected
118
   *   (optional) If TRUE, will check for selected option, if FALSE for
119
   *   unselected. Defaults to TRUE.
120
   *
121
   * @return \Behat\Mink\Element\NodeElement|null
122
   *   The node element or NULL.
123
   *
124
   * @throws \Behat\Mink\Exception\ExpectationException
125
   */
126
  protected function getSelectedOptionByLabel($select, $option_label, $check_selected = TRUE) {
127
    if (!$field = $this->getSession()->getPage()->findField($select)) {
128
      throw new ExpectationException("Field '$select' not found.", $this->getSession());
129
    }
130
131
    /* @var \Behat\Mink\Element\NodeElement $field */
132
133
    // Check that the specified field is a <select> field.
134
    $this->assertElementType($field, 'select');
135
136
    $options = $field->findAll('css', 'option');
137
    $options = array_filter($options, function (NodeElement $option) use ($option_label) {
138
      return $option->getText() == $option_label ? $option : FALSE;
139
    });
140
141
    if (!($option = $options ? reset($options) : NULL)) {
142
      throw new ExpectationException("Option '$option_label' doesn't exist in '$select' select.", $this->getSession());
143
    }
144
145
    return $check_selected ? $option->isSelected() : !$option->isSelected();
146
  }
147
148
}
149