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

SahiDriverFactory::createDriver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.4578

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 4
cts 14
cp 0.2857
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
crap 3.4578
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 SahiDriverFactory 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 'sahi';
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
			'port' => 9999,
40
			'driverOptions' => array(
41
				'sid' => null,
42
				'limit' => 600,
43
				'browser' => null,
44
			),
45
		);
46
	}
47
48
	/**
49
	 * Returns a new driver instance according to the browser configuration.
50
	 *
51
	 * @param BrowserConfiguration $browser The browser configuration.
52
	 *
53
	 * @return DriverInterface
54
	 * @throws \RuntimeException When driver isn't installed.
55
	 */
56 1
	public function createDriver(BrowserConfiguration $browser)
57
	{
58 1
		if ( !class_exists('Behat\Mink\Driver\SahiDriver') ) {
59 1
			throw new \RuntimeException(
60 1
				'Install MinkSahiDriver in order to use sahi driver.'
61
			);
62
		}
63
64
		$driver_options = $browser->getDriverOptions();
65
66
		$connection = new \Behat\SahiClient\Connection(
67
			$driver_options['sid'],
68
			$browser->getHost(),
69
			$browser->getPort(),
70
			$driver_options['browser'],
71
			$driver_options['limit']
72
		);
73
74
		return new \Behat\Mink\Driver\SahiDriver(
75
			$browser->getBrowserName(),
76
			new \Behat\SahiClient\Client($connection)
77
		);
78
	}
79
80
}
81