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
|
4 |
|
public static function getSubscribedEvents() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
4 |
|
CoverageEvent::BEFORE_START => 'beforeCoverageStart', |
49
|
|
|
CoverageEvent::REFRESH => 'coverageRefresh', |
50
|
|
|
ReportEvent::BEFORE_PROCESS => 'beforeReportProcess', |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
public function setMink($mink) |
55
|
|
|
{ |
56
|
4 |
|
$this->mink = $mink; |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
public function setRemoteUrl($url) |
60
|
|
|
{ |
61
|
10 |
|
$this->remoteUrl = $url; |
62
|
|
|
} |
63
|
|
|
|
64
|
10 |
|
public function setHttpClient(ClientInterface $httpClient) |
65
|
|
|
{ |
66
|
10 |
|
$this->httpClient = $httpClient; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setMinkSessionName($name) |
70
|
|
|
{ |
71
|
|
|
$this->minkSessionName = $name; |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
public function coverageRefresh() |
75
|
|
|
{ |
76
|
5 |
|
$client = $this->httpClient; |
77
|
5 |
|
$filterOptions = $this->filterOptions; |
78
|
5 |
|
$coverageOptions = $this->codeCoverageOptions; |
79
|
5 |
|
$url = $this->remoteUrl; |
80
|
|
|
|
81
|
|
|
$data = [ |
82
|
5 |
|
'filterOptions' => $filterOptions, |
83
|
5 |
|
'codeCoverageOptions' => $coverageOptions |
84
|
|
|
]; |
85
|
5 |
|
$body = json_encode($data); |
86
|
|
|
$options = [ |
87
|
5 |
|
'body' => $body, |
88
|
|
|
'query' => [ |
89
|
5 |
|
'action' => 'init', |
90
|
5 |
|
'session' => $this->session->getName() |
91
|
|
|
] |
92
|
|
|
]; |
93
|
5 |
|
$this->hasInitialized = false; |
94
|
|
|
try{ |
95
|
5 |
|
$response = $client->request( |
96
|
5 |
|
'POST', |
97
|
|
|
$url, |
98
|
|
|
$options |
99
|
|
|
); |
100
|
4 |
|
if($response->getStatusCode() === Response::HTTP_ACCEPTED){ |
101
|
4 |
|
$this->hasInitialized = true; |
102
|
|
|
} |
103
|
1 |
|
}catch (\Exception $e){ |
104
|
1 |
|
$this->hasInitialized = false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
public function hasInitialized() |
109
|
|
|
{ |
110
|
2 |
|
return $this->hasInitialized; |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
public function beforeCoverageStart(CoverageEvent $event) |
114
|
|
|
{ |
115
|
4 |
|
$sessionName = $this->session->getName(); |
116
|
4 |
|
$testCaseName = $event->getTestCase()->getName(); |
117
|
|
|
|
118
|
4 |
|
$mink = $this->mink; |
119
|
|
|
|
120
|
|
|
/* @var \Behat\Mink\Driver\Goutte\Client $client */ |
121
|
4 |
|
$driver = $mink->getSession()->getDriver(); |
122
|
4 |
|
$driver->setRequestHeader(RemoteSession::HEADER_SESSION_KEY, $sessionName); |
123
|
4 |
|
$driver->setRequestHeader(RemoteSession::HEADER_TEST_CASE_KEY, $testCaseName); |
124
|
|
|
|
125
|
|
|
/* patch for browserkit driver */ |
126
|
4 |
|
if(method_exists($driver,'getClient')){ |
127
|
3 |
|
$client = $driver->getClient(); |
128
|
3 |
|
$client->setServerParameters([ |
129
|
3 |
|
RemoteSession::HEADER_SESSION_KEY => $sessionName, |
130
|
3 |
|
RemoteSession::HEADER_TEST_CASE_KEY => $testCaseName |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
5 |
|
public function beforeReportProcess(ReportEvent $event) |
136
|
|
|
{ |
137
|
5 |
|
$session = $this->session; |
138
|
5 |
|
$client = $this->httpClient; |
139
|
5 |
|
$uri = $this->remoteUrl; |
140
|
|
|
|
141
|
|
|
$options = [ |
142
|
|
|
'query' => [ |
143
|
5 |
|
'action' => 'read', |
144
|
5 |
|
'session' => $session->getName() |
145
|
|
|
] |
146
|
|
|
]; |
147
|
|
|
try{ |
148
|
5 |
|
$response = $client->request('GET', $uri, $options); |
149
|
4 |
|
if($response->getStatusCode() === Response::HTTP_OK){ |
150
|
4 |
|
$data = $response->getBody()->getContents(); |
151
|
4 |
|
$data = json_decode($data, true); |
152
|
4 |
|
$event->getProcessor()->updateCoverage($data); |
153
|
|
|
} |
154
|
1 |
|
}catch (\Exception $exception){ |
155
|
1 |
|
$event->addException($exception); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|