Completed
Pull Request — master (#164)
by Vincent
02:48
created

BrowserKit::resetHttpHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Sanpi\Behatch\HttpCall\Request;
4
5
use Behat\Mink\Mink;
6
7
class BrowserKit
8
{
9
    protected $mink;
10
11
    public function __construct(Mink $mink)
12
    {
13
        $this->mink = $mink;
14
    }
15
16
    public function getMethod()
17
    {
18
        return $this->getRequest()
19
            ->getMethod();
20
    }
21
22
    public function getUri()
23
    {
24
        return $this->getRequest()
25
            ->getUri();
26
    }
27
28
    public function getServer()
29
    {
30
        return $this->getRequest()
31
            ->getServer();
32
    }
33
34
    public function getParameters()
35
    {
36
        return $this->getRequest()
37
            ->getParameters();
38
    }
39
40
    protected function getRequest()
41
    {
42
        $client = $this->mink->getSession()->getDriver()->getClient();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Mink\Driver\DriverInterface as the method getClient() does only exist in the following implementations of said interface: Behat\Mink\Driver\BrowserKitDriver, Behat\Mink\Driver\GoutteDriver.

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...
43
        // BC layer for BrowserKit 2.2.x and older
44
        if (method_exists($client, 'getInternalRequest')) {
45
            $request = $client->getInternalRequest();
46
        } else {
47
            $request = $client->getRequest();
48
        }
49
        return $request;
50
    }
51
52
    public function getContent()
53
    {
54
        return $this->mink->getSession()->getPage()->getContent();
55
    }
56
57
    public function send($method, $url, $parameters = [], $files = [], $content = null, $headers = [])
58
    {
59
        $client = $this->mink->getSession()->getDriver()->getClient();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Mink\Driver\DriverInterface as the method getClient() does only exist in the following implementations of said interface: Behat\Mink\Driver\BrowserKitDriver, Behat\Mink\Driver\GoutteDriver.

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...
60
61
        $client->followRedirects(false);
62
        $client->request($method, $url, $parameters, $files, $headers, $content);
63
        $client->followRedirects(true);
64
        $this->resetHttpHeaders();
65
66
        return $this->mink->getSession()->getPage();
67
    }
68
69
    public function setHttpHeader($name, $value)
70
    {
71
        $client = $this->mink->getSession()->getDriver()->getClient();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Mink\Driver\DriverInterface as the method getClient() does only exist in the following implementations of said interface: Behat\Mink\Driver\BrowserKitDriver, Behat\Mink\Driver\GoutteDriver.

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...
72
        // Goutte\Client
73
        if (method_exists($client, 'setHeader')) {
74
            $client->setHeader($name, $value);
75
        } else {
76
            // Symfony\Component\BrowserKit\Client
77
78
            /* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */
79
            $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true);
80
            $name = str_replace('-', '_', strtoupper($name));
81
82
            // CONTENT_* are not prefixed with HTTP_ in PHP when building $_SERVER
83
            if (!isset($contentHeaders[$name])) {
84
                $name = 'HTTP_' . $name;
85
            }
86
            /* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */
87
88
            $client->setServerParameter($name, $value);
89
        }
90
    }
91
92
    public function getHttpHeaders()
93
    {
94
        return array_change_key_case(
95
            $this->mink->getSession()->getResponseHeaders(),
96
            CASE_LOWER
97
        );
98
    }
99
100
    public function getHttpHeader($name)
101
    {
102
        $name = strtolower($name);
103
        $headers = $this->getHttpHeaders();
104
105
        if (isset($headers[$name])) {
106
            if (is_array($headers[$name])) {
107
                $value = implode(', ', $headers[$name]);
108
            }
109
            else {
110
                $value = $headers[$name];
111
            }
112
        }
113
        else {
114
            throw new \OutOfBoundsException(
115
                "The header '$name' doesn't exist"
116
            );
117
        }
118
        return $value;
119
    }
120
121
    private function resetHttpHeaders()
122
    {
123
        $client = $this->mink->getSession()->getDriver()->getClient();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Behat\Mink\Driver\DriverInterface as the method getClient() does only exist in the following implementations of said interface: Behat\Mink\Driver\BrowserKitDriver, Behat\Mink\Driver\GoutteDriver.

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...
124
        
125
        // At this point, `$client` is not supposed to be Goutte\Client
126
        // Otherwise, this code is overridden on HttpCall\Request\Goutte
127
        /** @var \Symfony\Component\BrowserKit\Client $client */
128
        $client->setServerParameters([]);
129
    }
130
}
131