Completed
Pull Request — master (#78)
by Christophe
03:11
created

GoutteDriverFactory::getDriverName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 8
	public function getDriverName()
27
	{
28 8
		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
				'server_parameters' => array(),
41
				'guzzle_parameters' => array(),
42 1
			),
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
		else {
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 4 client.
79
	 *
80
	 * @param array $parameters Parameters.
81
	 *
82
	 * @return \GuzzleHttp\Client
83
	 */
84
	private function _buildGuzzle4Client(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(array('defaults' => $parameters));
91
92
	}
93
94
	/**
95
	 * Builds Guzzle 3 client.
96
	 *
97
	 * @param array $parameters Parameters.
98
	 *
99
	 * @return \Guzzle\Http\Client
100
	 */
101
	private function _buildGuzzle3Client(array $parameters)
102
	{
103
		// Force the parameters set by default in Goutte to reproduce its behavior.
104
		$parameters['redirect.disable'] = true;
105
106
		return new \Guzzle\Http\Client(null, $parameters);
107
	}
108
109
	/**
110
	 * Determines Goutte client version.
111
	 *
112
	 * @return boolean
113
	 */
114
	private function _isGoutte1()
115
	{
116
		$reflection = new \ReflectionParameter(array('Goutte\Client', 'setClient'), 0);
117
118
		if ( $reflection->getClass() && 'Guzzle\Http\ClientInterface' === $reflection->getClass()->getName() ) {
119
			return true;
120
		}
121
122
		return false;
123
	}
124
125
}
126