Failed Conditions
Pull Request — master (#102)
by
unknown
03:22
created

GoutteDriverFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 22.22%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 134
ccs 8
cts 36
cp 0.2222
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriverName() 0 4 1
A getDriverDefaults() 0 9 1
B createDriver() 0 23 4
A _buildGuzzle6Client() 0 8 1
A _buildGuzzle4Client() 0 8 1
A _buildGuzzle3Client() 0 7 1
A _isGoutte1() 0 10 3
A _isGuzzle6() 0 5 2
1
<?php
2
/**
3
 * This file is part of the phpunit-mink library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/aik099/phpunit-mink
9
 */
10
11
12
namespace aik099\PHPUnit\MinkDriver;
0 ignored issues
show
introduced by
Expected 2 blank line(-s) after namespace declaration; 1 found
Loading history...
13
14
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
15
use Behat\Mink\Driver\DriverInterface;
16
17
class GoutteDriverFactory implements IMinkDriverFactory
18
{
19
20
    /**
21
     * Returns driver name, that can be used in browser configuration.
22
     *
23
     * @return string
24
     */
25 10
    public function getDriverName()
26
    {
27 10
        return 'goutte';
28
    }
29
30
    /**
31
     * Returns default values for browser configuration.
32
     *
33
     * @return array
34
     */
35 1
    public function getDriverDefaults()
36
    {
37
        return array(
38 1
            'driverOptions' => array(
39
                'server_parameters' => array(),
40
                'guzzle_parameters' => array(),
41
            ),
42
        );
43
    }
44
45
    /**
46
     * Returns a new driver instance according to the browser configuration.
47
     *
48
     * @param BrowserConfiguration $browser The browser configuration.
49
     *
50
     * @return DriverInterface
51
     * @throws \RuntimeException When driver isn't installed.
52
     */
53 1
    public function createDriver(BrowserConfiguration $browser)
54
    {
55 1
        if (!class_exists('Behat\Mink\Driver\GoutteDriver')) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
56 1
            throw new \RuntimeException(
57 1
                'Install MinkGoutteDriver in order to use goutte driver.'
58
            );
59
        }
60
61
        $driver_options = $browser->getDriverOptions();
62
63
        if ($this->_isGoutte1()) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
64
            $guzzle_client = $this->_buildGuzzle3Client($driver_options['guzzle_parameters']);
65
        } elseif ($this->_isGuzzle6()) {
0 ignored issues
show
introduced by
Beginning of the "elseif" control structure must be first content on the line
Loading history...
introduced by
Expected "}\nelseif (...) \n"; found " elseif (...) {\n"
Loading history...
introduced by
Expected 1 spaces after "elseif" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "elseif" closing bracket; 0 found
Loading history...
66
            $guzzle_client = $this->_buildGuzzle6Client($driver_options['guzzle_parameters']);
67
        } else {
0 ignored issues
show
introduced by
Beginning of the "else" control structure must be first content on the line
Loading history...
introduced by
Expected "}\nelse \n"; found " else {\n"
Loading history...
68
            $guzzle_client = $this->_buildGuzzle4Client($driver_options['guzzle_parameters']);
69
        }
70
71
        $goutte_client = new \Behat\Mink\Driver\Goutte\Client($driver_options['server_parameters']);
72
        $goutte_client->setClient($guzzle_client);
73
74
        return new \Behat\Mink\Driver\GoutteDriver($goutte_client);
75
    }
76
77
    /**
78
     * Builds Guzzle 6 client.
79
     *
80
     * @param array $parameters Parameters.
81
     *
82
     * @return \GuzzleHttp\Client
83
     */
84
    private function _buildGuzzle6Client(array $parameters)
85
    {
86
        // Force the parameters set by default in Goutte to reproduce its behavior.
87
        $parameters['allow_redirects'] = false;
88
        $parameters['cookies'] = true;
89
90
        return new \GuzzleHttp\Client($parameters);
91
    }
92
93
    /**
94
     * Builds Guzzle 4 client.
95
     *
96
     * @param array $parameters Parameters.
97
     *
98
     * @return \GuzzleHttp\Client
99
     */
100
    private function _buildGuzzle4Client(array $parameters)
101
    {
102
        // Force the parameters set by default in Goutte to reproduce its behavior.
103
        $parameters['allow_redirects'] = false;
104
        $parameters['cookies'] = true;
105
106
        return new \GuzzleHttp\Client(array('defaults' => $parameters));
107
    }
108
109
    /**
110
     * Builds Guzzle 3 client.
111
     *
112
     * @param array $parameters Parameters.
113
     *
114
     * @return \Guzzle\Http\Client
115
     */
116
    private function _buildGuzzle3Client(array $parameters)
117
    {
118
        // Force the parameters set by default in Goutte to reproduce its behavior.
119
        $parameters['redirect.disable'] = true;
120
121
        return new \Guzzle\Http\Client(null, $parameters);
122
    }
123
124
    /**
125
     * Determines Goutte client version.
126
     *
127
     * @return boolean
128
     */
129
    private function _isGoutte1()
130
    {
131
        $reflection = new \ReflectionParameter(array('Goutte\Client', 'setClient'), 0);
132
133
        if ($reflection->getClass() && 'Guzzle\Http\ClientInterface' === $reflection->getClass()->getName()) {
0 ignored issues
show
introduced by
Expected 1 spaces after "if" opening bracket; 0 found
Loading history...
introduced by
Expected 1 spaces before "if" closing bracket; 0 found
Loading history...
134
            return true;
135
        }
136
137
        return false;
138
    }
139
    
140
    /**
141
     * Determines Guzzle version.
142
     *
143
     * @return boolean
144
     */
145
    private function _isGuzzle6()
146
    {
147
        return interface_exists('GuzzleHttp\ClientInterface') &&
148
        version_compare(\GuzzleHttp\ClientInterface::VERSION, '6.0.0', '>=');
149
    }
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line after function; 0 found
Loading history...
150
}
151