1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the doyo/behat-coverage-extension project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\Behat\Coverage\Listener; |
15
|
|
|
|
16
|
|
|
use Doyo\Behat\Coverage\Bridge\CodeCoverage\Session\RemoteSession; |
17
|
|
|
use Doyo\Behat\Coverage\Event\CoverageEvent; |
18
|
|
|
use Doyo\Behat\Coverage\Event\ReportEvent; |
19
|
|
|
use GuzzleHttp\Client; |
20
|
|
|
use GuzzleHttp\ClientInterface; |
21
|
|
|
use spec\Doyo\Behat\Coverage\Listener\AbstractSessionCoverageListener; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
|
25
|
|
|
class RemoteCoverageListener extends AbstractSessionCoverageListener implements EventSubscriberInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \Behat\Mink\Mink |
29
|
|
|
*/ |
30
|
|
|
private $mink; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ClientInterface |
34
|
|
|
*/ |
35
|
|
|
private $httpClient; |
36
|
|
|
|
37
|
|
|
private $hasInitialized = false; |
38
|
|
|
|
39
|
|
|
private $minkSessionName; |
40
|
|
|
|
41
|
|
|
private $minkParameters; |
42
|
|
|
|
43
|
|
|
private $remoteUrl; |
44
|
|
|
|
45
|
7 |
|
public static function getSubscribedEvents() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
7 |
|
CoverageEvent::BEFORE_START => 'beforeCoverageStart', |
49
|
|
|
CoverageEvent::REFRESH => 'coverageRefresh', |
50
|
|
|
ReportEvent::BEFORE_PROCESS => 'beforeReportProcess', |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
public function setMink($mink) |
55
|
|
|
{ |
56
|
7 |
|
$this->mink = $mink; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setMinkParameters(array $parameters) |
60
|
|
|
{ |
61
|
|
|
$this->minkParameters = $parameters; |
62
|
|
|
} |
63
|
|
|
|
64
|
13 |
|
public function setRemoteUrl($url) |
65
|
|
|
{ |
66
|
13 |
|
$this->remoteUrl = $url; |
67
|
|
|
} |
68
|
|
|
|
69
|
13 |
|
public function setHttpClient(ClientInterface $httpClient) |
70
|
|
|
{ |
71
|
13 |
|
$this->httpClient = $httpClient; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setMinkSessionName($name) |
75
|
|
|
{ |
76
|
|
|
$this->minkSessionName = $name; |
77
|
|
|
} |
78
|
|
|
|
79
|
7 |
|
public function coverageRefresh() |
80
|
|
|
{ |
81
|
7 |
|
$client = $this->httpClient; |
82
|
7 |
|
$filterOptions = $this->filterOptions; |
83
|
7 |
|
$coverageOptions = $this->codeCoverageOptions; |
84
|
7 |
|
$url = $this->remoteUrl; |
85
|
|
|
|
86
|
|
|
$data = [ |
87
|
7 |
|
'filterOptions' => $filterOptions, |
88
|
7 |
|
'codeCoverageOptions' => $coverageOptions |
89
|
|
|
]; |
90
|
7 |
|
$body = json_encode($data); |
91
|
|
|
$options = [ |
92
|
7 |
|
'body' => $body, |
93
|
|
|
'query' => [ |
94
|
7 |
|
'action' => 'init', |
95
|
7 |
|
'session' => $this->session->getName() |
96
|
|
|
] |
97
|
|
|
]; |
98
|
7 |
|
$this->hasInitialized = false; |
99
|
|
|
try{ |
100
|
7 |
|
$response = $client->request( |
101
|
7 |
|
'POST', |
102
|
|
|
$url, |
103
|
|
|
$options |
104
|
|
|
); |
105
|
6 |
|
if($response->getStatusCode() === Response::HTTP_ACCEPTED){ |
106
|
6 |
|
$this->hasInitialized = true; |
107
|
|
|
} |
108
|
1 |
|
}catch (\Exception $e){ |
109
|
1 |
|
$this->hasInitialized = false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
public function hasInitialized() |
114
|
|
|
{ |
115
|
2 |
|
return $this->hasInitialized; |
116
|
|
|
} |
117
|
|
|
|
118
|
6 |
|
public function beforeCoverageStart(CoverageEvent $event) |
119
|
|
|
{ |
120
|
6 |
|
$sessionName = $this->session->getName(); |
121
|
6 |
|
$testCaseName = $event->getTestCase()->getName(); |
122
|
|
|
|
123
|
6 |
|
$mink = $this->mink; |
124
|
|
|
|
125
|
|
|
/* @var \Behat\Mink\Driver\Goutte\Client $client */ |
126
|
6 |
|
$driver = $mink->getSession()->getDriver(); |
127
|
6 |
|
$driver->setRequestHeader(RemoteSession::HEADER_SESSION_KEY, $sessionName); |
128
|
6 |
|
$driver->setRequestHeader(RemoteSession::HEADER_TEST_CASE_KEY, $testCaseName); |
129
|
|
|
|
130
|
|
|
/* patch for browserkit driver */ |
131
|
6 |
|
if(method_exists($driver,'getClient')){ |
132
|
5 |
|
$client = $driver->getClient(); |
133
|
5 |
|
$client->setServerParameters([ |
134
|
5 |
|
RemoteSession::HEADER_SESSION_KEY => $sessionName, |
135
|
5 |
|
RemoteSession::HEADER_TEST_CASE_KEY => $testCaseName |
136
|
|
|
]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
7 |
|
public function beforeReportProcess(ReportEvent $event) |
141
|
|
|
{ |
142
|
7 |
|
$session = $this->session; |
143
|
7 |
|
$client = $this->httpClient; |
144
|
7 |
|
$uri = $this->remoteUrl; |
145
|
|
|
|
146
|
|
|
$options = [ |
147
|
|
|
'query' => [ |
148
|
7 |
|
'action' => 'read', |
149
|
7 |
|
'session' => $session->getName() |
150
|
|
|
] |
151
|
|
|
]; |
152
|
|
|
try{ |
153
|
7 |
|
$response = $client->request('GET', $uri, $options); |
154
|
6 |
|
if($response->getStatusCode() === Response::HTTP_OK){ |
155
|
6 |
|
$data = $response->getBody()->getContents(); |
156
|
6 |
|
$data = json_decode($data, true); |
157
|
6 |
|
$event->getProcessor()->updateCoverage($data); |
158
|
|
|
} |
159
|
1 |
|
}catch (\Exception $exception){ |
160
|
1 |
|
$event->addException($exception); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|