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

BrowserStackAPIClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 27.78%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 93
ccs 5
cts 18
cp 0.2778
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getInfo() 0 6 1
A updateStatus() 0 7 2
A execute() 0 13 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
namespace aik099\PHPUnit\APIClient;
12
13
14
use WebDriver\Service\CurlServiceInterface;
15
16
class BrowserStackAPIClient implements IAPIClient
17
{
18
19
	/**
20
	 * API username.
21
	 *
22
	 * @var string
23
	 */
24
	private $_apiUsername;
25
26
	/**
27
	 * API key.
28
	 *
29
	 * @var string
30
	 */
31
	private $_apiKey;
32
33
	/**
34
	 * Curl service.
35
	 *
36
	 * @var CurlServiceInterface
37
	 */
38
	private $_curlService;
39
40
	/**
41
	 * Creates instance of API client.
42
	 *
43
	 * @param string               $api_username API Username.
44
	 * @param string               $api_key      API Password.
45
	 * @param CurlServiceInterface $curl_service Curl service.
46
	 */
47 1
	public function __construct($api_username, $api_key, CurlServiceInterface $curl_service)
48
	{
49 1
		$this->_apiUsername = $api_username;
50 1
		$this->_apiKey = $api_key;
51 1
		$this->_curlService = $curl_service;
52 1
	}
53
54
	/**
55
	 * Returns information about session.
56
	 *
57
	 * @param string $session_id Session ID.
58
	 *
59
	 * @return array
60
	 */
61
	public function getInfo($session_id)
62
	{
63
		$result = $this->execute('GET', 'sessions/' . $session_id . '.json');
64
65
		return $result['automation_session'];
66
	}
67
68
	/**
69
	 * Update status of the test, that was executed in the given session.
70
	 *
71
	 * @param string  $session_id  Session ID.
72
	 * @param boolean $test_status Test status.
73
	 *
74
	 * @return array
75
	 */
76
	public function updateStatus($session_id, $test_status)
77
	{
78
		$data = array('status' => $test_status ? 'completed' : 'error');
79
		$result = $this->execute('PUT', 'sessions/' . $session_id . '.json', $data);
80
81
		return $result['automation_session'];
82
	}
83
84
	/**
85
	 * Execute BrowserStack REST API command.
86
	 *
87
	 * @param string $request_method HTTP request method.
88
	 * @param string $url            URL.
89
	 * @param mixed  $parameters     Parameters.
90
	 *
91
	 * @return mixed
92
	 * @see    http://www.browserstack.com/automate/rest-api
93
	 */
94
	protected function execute($request_method, $url, $parameters = null)
95
	{
96
		$extra_options = array(
97
			CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
98
			CURLOPT_USERPWD => $this->_apiUsername . ':' . $this->_apiKey,
99
		);
100
101
		$url = 'https://www.browserstack.com/automate/' . $url;
102
103
		list($raw_results,) = $this->_curlService->execute($request_method, $url, $parameters, $extra_options);
104
105
		return json_decode($raw_results, true);
106
	}
107
108
}
109