Code Duplication    Length = 32-32 lines in 2 locations

src/DrupalExtension/Context/SelectFieldContext.php 2 locations

@@ 21-52 (lines=32) @@
18
   *
19
   * @Then I should have the following options for :select:
20
   */
21
  public function assertSelectOptions($select, TableNode $options) {
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.
@@ 59-90 (lines=32) @@
56
   *
57
   * @Then I should not have the following options for :select:
58
   */
59
  public function assertNoSelectOptions($select, TableNode $options) {
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.