Completed
Push — master ( 008e2c...4f57de )
by Alexander
06:10 queued 02:38
created

GoutteDriverFactory::_buildGuzzle6Client()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 4
cp 0
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
crap 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
			'driverOptions' => array(
40 1
				'server_parameters' => array(),
41 1
				'guzzle_parameters' => array(),
42 1
			),
43 1
		);
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
				'Install MinkGoutteDriver in order to use goutte driver.'
59 1
			);
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
		if ( $reflection->getClass() && 'Guzzle\Http\ClientInterface' === $reflection->getClass()->getName() ) {
139
			return true;
140
		}
141
142
		return false;
143
	}
144
	
145
	/**
146
	 * Determines Guzzle version.
147
	 *
148
	 * @return boolean
149
	 */
150
	private function _isGuzzle6()
151
	{
152
		return interface_exists('GuzzleHttp\ClientInterface') && 
153
		version_compare(\GuzzleHttp\ClientInterface::VERSION, '6.0.0', '>=');
154
	}
155
156
}
157