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

ApiBrowserConfiguration   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 96.15%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 20
c 7
b 0
f 0
lcom 2
cbo 7
dl 0
loc 211
ccs 50
cts 52
cp 0.9615
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setApiUsername() 0 4 1
A setApiKey() 0 4 1
A getPort() 0 4 1
getAPIClient() 0 1 ?
A __construct() 0 10 1
A getBrowserName() 0 6 2
A getJobName() 0 8 2
A getSessionId() 0 10 2
A setApi_username() 0 4 1
A setApi_key() 0 4 1
A onTestSetup() 0 20 4
B onTestEnded() 0 22 4
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\BrowserConfiguration;
12
13
14
use aik099\PHPUnit\APIClient\IAPIClient;
15
use aik099\PHPUnit\BrowserTestCase;
16
use aik099\PHPUnit\Event\TestEndedEvent;
17
use aik099\PHPUnit\Event\TestEvent;
18
use aik099\PHPUnit\MinkDriver\DriverFactoryRegistry;
19
use Behat\Mink\Driver\Selenium2Driver;
20
use Behat\Mink\Session;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
23
/**
24
 * Browser configuration tailored to use with API-based service.
25
 *
26
 * @method string getApiUsername() Returns API username.
27
 * @method string getApiKey() Returns API key.
28
 */
29
abstract class ApiBrowserConfiguration extends BrowserConfiguration
30
{
31
32
	/**
33
	 * The build number.
34
	 */
35
	const BUILD_NUMBER_CAPABILITY = 'build';
36
37
	/**
38
	 * The test name.
39
	 */
40
	const NAME_CAPABILITY = 'name';
41
42
	/**
43
	 * Creates browser configuration.
44
	 *
45
	 * @param EventDispatcherInterface $event_dispatcher        Event dispatcher.
46
	 * @param DriverFactoryRegistry    $driver_factory_registry Driver factory registry.
47
	 */
48 111
	public function __construct(
49
		EventDispatcherInterface $event_dispatcher,
50
		DriverFactoryRegistry $driver_factory_registry
51
	) {
52 111
		$this->defaults['driver'] = 'selenium2';
53 111
		$this->defaults['apiUsername'] = '';
54 111
		$this->defaults['apiKey'] = '';
55
56 111
		parent::__construct($event_dispatcher, $driver_factory_registry);
57 111
	}
58
59
	/**
60
	 * Sets API username.
61
	 *
62
	 * To be called from TestCase::setUp().
63
	 *
64
	 * @param string $api_username API username.
65
	 *
66
	 * @return self
67
	 */
68 14
	public function setApiUsername($api_username)
69
	{
70 14
		return $this->setParameter('apiUsername', $api_username);
71
	}
72
73
	/**
74
	 * Sets API key.
75
	 *
76
	 * To be called from TestCase::setUp().
77
	 *
78
	 * @param string $api_key API key.
79
	 *
80
	 * @return self
81
	 */
82 14
	public function setApiKey($api_key)
83
	{
84 14
		return $this->setParameter('apiKey', $api_key);
85
	}
86
87
	/**
88
	 * Sets API username.
89
	 *
90
	 * Used internally to to allow using "api_username" parameter and avoid BC break.
91
	 *
92
	 * @param string $api_username API username.
93
	 *
94
	 * @return     self
95
	 * @deprecated
96
	 */
97 2
	protected function setApi_username($api_username)
98
	{
99 2
		return $this->setApiUsername($api_username);
100
	}
101
102
	/**
103
	 * Sets API key.
104
	 *
105
	 * Used internally to to allow using "api_key" parameter and avoid BC break.
106
	 *
107
	 * @param string $api_key API key.
108
	 *
109
	 * @return     self
110
	 * @deprecated
111
	 */
112 2
	protected function setApi_key($api_key)
113
	{
114 2
		return $this->setApiKey($api_key);
115
	}
116
117
	/**
118
	 * Returns port from browser configuration.
119
	 *
120
	 * @return integer
121
	 */
122 14
	public function getPort()
123
	{
124 14
		return 80;
125
	}
126
127
	/**
128
	 * Returns browser name from browser configuration.
129
	 *
130
	 * @return string
131
	 */
132 14
	public function getBrowserName()
133
	{
134 14
		$browser_name = parent::getBrowserName();
135
136 14
		return strlen($browser_name) ? $browser_name : 'chrome';
137
	}
138
139
	/**
140
	 * Hook, called from "BrowserTestCase::setUp" method.
141
	 *
142
	 * @param TestEvent $event Test event.
143
	 *
144
	 * @return void
145
	 */
146 16
	public function onTestSetup(TestEvent $event)
147
	{
148 16
		if ( !$event->validateSubscriber($this->getTestCase()) ) {
149
			return;
150
		}
151
152 16
		parent::onTestSetup($event);
153
154 16
		$desired_capabilities = $this->getDesiredCapabilities();
155 16
		$desired_capabilities[self::NAME_CAPABILITY] = $this->getJobName($event->getTestCase());
156
157 16
		if ( getenv('BUILD_NUMBER') ) {
158 4
			$desired_capabilities[self::BUILD_NUMBER_CAPABILITY] = getenv('BUILD_NUMBER'); // Jenkins.
159
		}
160 12
		elseif ( getenv('TRAVIS_BUILD_NUMBER') ) {
161 4
			$desired_capabilities[self::BUILD_NUMBER_CAPABILITY] = getenv('TRAVIS_BUILD_NUMBER');
162
		}
163
164 16
		$this->setDesiredCapabilities($desired_capabilities);
165 16
	}
166
167
	/**
168
	 * Returns Job name for API service.
169
	 *
170
	 * @param BrowserTestCase $test_case Browser test case.
171
	 *
172
	 * @return string
173
	 */
174 16
	protected function getJobName(BrowserTestCase $test_case)
175
	{
176 16
		if ( $this->isShared() ) {
177 6
			return get_class($test_case);
178
		}
179
180 10
		return $test_case->toString();
181
	}
182
183
	/**
184
	 * Hook, called from "BrowserTestCase::run" method.
185
	 *
186
	 * @param TestEndedEvent $event Test ended event.
187
	 *
188
	 * @return void
189
	 */
190 8
	public function onTestEnded(TestEndedEvent $event)
191
	{
192 8
		if ( !$event->validateSubscriber($this->getTestCase()) ) {
193
			return;
194
		}
195
196 8
		parent::onTestEnded($event);
197
198 8
		$session = $event->getSession();
199
200 8
		if ( $session === null || !$session->isStarted() ) {
201
			// Session wasn't used in particular test.
202 4
			return;
203
		}
204
205 4
		$test_case = $event->getTestCase();
206
207 4
		$this->getAPIClient()->updateStatus(
208 4
			$this->getSessionId($session),
209 2
			$this->getTestStatus($test_case, $event->getTestResult())
210
		);
211 2
	}
212
213
	/**
214
	 * Returns API class for service interaction.
215
	 *
216
	 * @return IAPIClient
217
	 */
218
	public abstract function getAPIClient();
219
220
	/**
221
	 * Get Selenium2 current session id.
222
	 *
223
	 * @param Session $session Session.
224
	 *
225
	 * @return string
226
	 * @throws \RuntimeException When session was created using an unsupported driver.
227
	 */
228 4
	protected function getSessionId(Session $session)
229
	{
230 4
		$driver = $session->getDriver();
231
232 4
		if ( $driver instanceof Selenium2Driver ) {
233 2
			return $driver->getWebDriverSessionId();
234
		}
235
236 2
		throw new \RuntimeException('Unsupported session driver');
237
	}
238
239
}
240