Completed
Pull Request — master (#100)
by Michał
01:35
created

GoutteDriverFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 23.53%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 135
ccs 8
cts 34
cp 0.2353
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDriverName() 0 4 1
A _buildGuzzle6Client() 0 9 1
A _buildGuzzle4Client() 0 9 1
A _buildGuzzle3Client() 0 7 1
A getDriverDefaults() 0 9 1
B createDriver() 0 25 4
A _isGoutte1() 0 6 2
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;
13
14
15
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
16
use Behat\Mink\Driver\DriverInterface;
17
18
class GoutteDriverFactory implements IMinkDriverFactory
19
{
20
21
	/**
22
	 * Returns driver name, that can be used in browser configuration.
23
	 *
24
	 * @return string
25
	 */
26 10
	public function getDriverName()
27
	{
28 10
		return 'goutte';
29
	}
30
31
	/**
32
	 * Returns default values for browser configuration.
33
	 *
34
	 * @return array
35
	 */
36 1
	public function getDriverDefaults()
37
	{
38
		return array(
39 1
			'driverOptions' => array(
40
				'server_parameters' => array(),
41
				'guzzle_parameters' => array(),
42
			),
43
		);
44
	}
45
46
	/**
47
	 * Returns a new driver instance according to the browser configuration.
48
	 *
49
	 * @param BrowserConfiguration $browser The browser configuration.
50
	 *
51
	 * @return DriverInterface
52
	 * @throws \RuntimeException When driver isn't installed.
53
	 */
54 1
	public function createDriver(BrowserConfiguration $browser)
55
	{
56 1
		if ( !class_exists('Behat\Mink\Driver\GoutteDriver') ) {
57 1
			throw new \RuntimeException(
58 1
				'Install MinkGoutteDriver in order to use goutte driver.'
59
			);
60
		}
61
62
		$driver_options = $browser->getDriverOptions();
63
64
		if ( $this->_isGoutte1() ) {
65
			$guzzle_client = $this->_buildGuzzle3Client($driver_options['guzzle_parameters']);
66
		}
67
		elseif ( $this->_isGuzzle6() ) {
68
			$guzzle_client = $this->_buildGuzzle6Client($driver_options['guzzle_parameters']);
69
		}
70
		else {
71
			$guzzle_client = $this->_buildGuzzle4Client($driver_options['guzzle_parameters']);
72
		}
73
74
		$goutte_client = new \Behat\Mink\Driver\Goutte\Client($driver_options['server_parameters']);
75
		$goutte_client->setClient($guzzle_client);
76
77
		return new \Behat\Mink\Driver\GoutteDriver($goutte_client);
78
	}
79
80
	/**
81
	 * Builds Guzzle 6 client.
82
	 *
83
	 * @param array $parameters Parameters.
84
	 *
85
	 * @return \GuzzleHttp\Client
86
	 */
87
	private function _buildGuzzle6Client(array $parameters)
88
	{
89
		// Force the parameters set by default in Goutte to reproduce its behavior.
90
		$parameters['allow_redirects'] = false;
91
		$parameters['cookies'] = true;
92
93
		return new \GuzzleHttp\Client($parameters);
94
95
	}
96
97
	/**
98
	 * Builds Guzzle 4 client.
99
	 *
100
	 * @param array $parameters Parameters.
101
	 *
102
	 * @return \GuzzleHttp\Client
103
	 */
104
	private function _buildGuzzle4Client(array $parameters)
105
	{
106
		// Force the parameters set by default in Goutte to reproduce its behavior.
107
		$parameters['allow_redirects'] = false;
108
		$parameters['cookies'] = true;
109
110
		return new \GuzzleHttp\Client(array('defaults' => $parameters));
111
112
	}
113
114
	/**
115
	 * Builds Guzzle 3 client.
116
	 *
117
	 * @param array $parameters Parameters.
118
	 *
119
	 * @return \Guzzle\Http\Client
120
	 */
121
	private function _buildGuzzle3Client(array $parameters)
122
	{
123
		// Force the parameters set by default in Goutte to reproduce its behavior.
124
		$parameters['redirect.disable'] = true;
125
126
		return new \Guzzle\Http\Client(null, $parameters);
127
	}
128
129
	/**
130
	 * Determines Goutte client version.
131
	 *
132
	 * @return boolean
133
	 */
134
	private function _isGoutte1()
135
	{
136
		$reflection = new \ReflectionParameter(array('Goutte\Client', 'setClient'), 0);
137
138
		return $reflection->getClass() && 'Guzzle\Http\ClientInterface' === $reflection->getClass()->name;
139
	}
140
	
141
	/**
142
	 * Determines Guzzle version.
143
	 *
144
	 * @return boolean
145
	 */
146
	private function _isGuzzle6()
147
	{
148
		return interface_exists('GuzzleHttp\ClientInterface') && 
149
		version_compare(\GuzzleHttp\ClientInterface::VERSION, '6.0.0', '>=');
150
	}
151
152
}
153