Completed
Push — 1.0.x ( 235ffe...0a17d9 )
by Antonio
08:29
created

DrupalContext::iShouldSeeInTheHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace NuvoleWeb\Drupal\DrupalExtension\Context;
4
5
use Behat\Behat\Context\SnippetAcceptingContext;
6
use Behat\Gherkin\Node\TableNode;
7
8
/**
9
 * Extends Drupal Extension DrupalContext class.
10
 *
11
 * Load this context instead of Drupal\DrupalExtension\Context\DrupalContext.
12
 *
13
 * @package NuvoleWeb\Drupal\DrupalExtension\Context
14
 */
15
class DrupalContext extends RawDrupalContext implements SnippetAcceptingContext {
16
17
  /**
18
   * Assert access denied page.
19
   *
20
   * @Then I should get an access denied error
21
   */
22
  public function assertAccessDenied() {
23
    $this->assertSession()->statusCodeEquals(403);
24
  }
25
26
  /**
27
   * Pause execution for given number of seconds.
28
   *
29
   * @Then I wait :seconds seconds
30
   */
31
  public function iWaitSeconds($seconds) {
32
    sleep((int) $seconds);
33
  }
34
35
  /**
36
   * Creates content by filling specified form fields via the UI.
37
   *
38
   * Use as follow:
39
   *
40
   *  | Title    | Author     | Label | of the field      |
41
   *  | My title | Joe Editor | 1     | 2014-10-17 8:00am |
42
   *  | ...      | ...        | ...   | ...               |
43
   *
44
   * @Given I create :type content:
45
   */
46
  public function manuallyCreateNodes($type, TableNode $nodesTable) {
47
    $type = $this->getCore()->convertLabelToNodeTypeId($type);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Drupal\Driver\Cores\CoreInterface as the method convertLabelToNodeTypeId() does only exist in the following implementations of said interface: NuvoleWeb\Drupal\Driver\Cores\Drupal8.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
48
49
    foreach ($nodesTable->getHash() as $nodeHash) {
50
      $this->getSession()->visit($this->locatePath("/node/add/$type"));
51
      $element = $this->getSession()->getPage();
52
      // Fill in the form.
53
      foreach ($nodeHash as $field => $value) {
54
        $element->fillField($field, $value);
55
      }
56
      $submit = $element->findButton($this->getDrupalText('node_submit_label'));
57
      if (empty($submit)) {
58
        throw new \Exception(sprintf("No submit button at %s", $this->getSession()->getCurrentUrl()));
59
      }
60
      // Submit the form.
61
      $submit->click();
62
    }
63
64
  }
65
66
  /**
67
   * Assert string in HTTP response header.
68
   *
69
   * @Then I should see in the header :header::value
70
   */
71
  public function iShouldSeeInTheHeader($header, $value) {
72
    $headers = $this->getSession()->getResponseHeaders();
73
    if ($headers[$header] != $value) {
74
      throw new \Exception(sprintf("Did not see %s with value %s.", $header, $value));
75
    }
76
  }
77
78
}
79