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 GuzzleHttp\Client; |
19
|
|
|
use GuzzleHttp\ClientInterface; |
20
|
|
|
use spec\Doyo\Behat\Coverage\Listener\AbstractSessionCoverageListener; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
22
|
|
|
use Symfony\Component\HttpFoundation\Response; |
23
|
|
|
|
24
|
|
|
class RemoteCoverageListener extends AbstractSessionCoverageListener implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var \Behat\Mink\Mink |
28
|
|
|
*/ |
29
|
|
|
private $mink; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ClientInterface |
33
|
|
|
*/ |
34
|
|
|
private $httpClient; |
35
|
|
|
|
36
|
|
|
private $hasInitialized = false; |
37
|
|
|
|
38
|
|
|
private $minkSessionName; |
39
|
|
|
|
40
|
|
|
private $remoteUrl; |
41
|
|
|
|
42
|
1 |
|
public static function getSubscribedEvents() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
1 |
|
CoverageEvent::BEFORE_START => 'beforeCoverageStart', |
46
|
|
|
CoverageEvent::REFRESH => 'coverageRefresh', |
47
|
|
|
CoverageEvent::COMPLETED => 'coverageCompleted', |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function setMink($mink) |
52
|
|
|
{ |
53
|
1 |
|
$this->mink = $mink; |
54
|
1 |
|
} |
55
|
|
|
|
56
|
5 |
|
public function setRemoteUrl($url) |
57
|
|
|
{ |
58
|
5 |
|
$this->remoteUrl = $url; |
59
|
5 |
|
} |
60
|
|
|
|
61
|
5 |
|
public function setHttpClient(ClientInterface $httpClient) |
62
|
|
|
{ |
63
|
5 |
|
$this->httpClient = $httpClient; |
64
|
5 |
|
} |
65
|
|
|
|
66
|
|
|
public function setMinkSessionName($name) |
67
|
|
|
{ |
68
|
|
|
$this->minkSessionName = $name; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function coverageRefresh() |
72
|
|
|
{ |
73
|
2 |
|
$client = $this->httpClient; |
74
|
2 |
|
$session = $this->session; |
75
|
2 |
|
$processor = $session->getProcessor(); |
76
|
2 |
|
$filter = $processor->getCodeCoverageFilter(); |
77
|
2 |
|
$coverageOptions = $processor->getCodeCoverageOptions(); |
78
|
2 |
|
$url = $this->remoteUrl; |
79
|
|
|
|
80
|
|
|
$data = [ |
81
|
|
|
'filterOptions' => [ |
82
|
2 |
|
'whitelistedFiles' => $filter->getWhitelistedFiles(), |
83
|
|
|
], |
84
|
2 |
|
'codeCoverageOptions' => $coverageOptions, |
85
|
|
|
]; |
86
|
2 |
|
$body = json_encode($data); |
87
|
|
|
$options = [ |
88
|
2 |
|
'body' => $body, |
89
|
|
|
'query' => [ |
90
|
2 |
|
'action' => 'init', |
91
|
2 |
|
'session' => $this->session->getName(), |
92
|
|
|
], |
93
|
|
|
]; |
94
|
2 |
|
$this->hasInitialized = false; |
95
|
|
|
try { |
96
|
2 |
|
$response = $client->request( |
97
|
2 |
|
'POST', |
98
|
|
|
$url, |
99
|
|
|
$options |
100
|
|
|
); |
101
|
1 |
|
if (Response::HTTP_ACCEPTED === $response->getStatusCode()) { |
102
|
1 |
|
$this->hasInitialized = true; |
103
|
|
|
} |
104
|
1 |
|
} catch (\Exception $e) { |
105
|
1 |
|
$this->hasInitialized = false; |
106
|
|
|
} |
107
|
2 |
|
} |
108
|
|
|
|
109
|
2 |
|
public function hasInitialized() |
110
|
|
|
{ |
111
|
2 |
|
return $this->hasInitialized; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
public function beforeCoverageStart(CoverageEvent $event) |
115
|
|
|
{ |
116
|
1 |
|
$sessionName = $this->session->getName(); |
117
|
1 |
|
$testCaseName = $event->getTestCase()->getName(); |
118
|
|
|
|
119
|
1 |
|
$mink = $this->mink; |
120
|
|
|
|
121
|
|
|
/** @var \Behat\Mink\Driver\Goutte\Client $client */ |
122
|
1 |
|
$driver = $mink->getSession()->getDriver(); |
123
|
1 |
|
$driver->setRequestHeader(RemoteSession::HEADER_SESSION_KEY, $sessionName); |
124
|
1 |
|
$driver->setRequestHeader(RemoteSession::HEADER_TEST_CASE_KEY, $testCaseName); |
125
|
|
|
|
126
|
|
|
/* patch for browserkit driver */ |
127
|
1 |
|
if (method_exists($driver, 'getClient')) { |
128
|
|
|
$client = $driver->getClient(); |
129
|
|
|
$client->setServerParameters([ |
130
|
|
|
RemoteSession::HEADER_SESSION_KEY => $sessionName, |
131
|
|
|
RemoteSession::HEADER_TEST_CASE_KEY => $testCaseName, |
132
|
|
|
]); |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
public function coverageCompleted(CoverageEvent $event) |
137
|
|
|
{ |
138
|
|
|
$session = $this->session; |
139
|
|
|
$client = $this->httpClient; |
140
|
|
|
$uri = $this->remoteUrl; |
141
|
|
|
|
142
|
|
|
$options = [ |
143
|
|
|
'query' => [ |
144
|
|
|
'action' => 'read', |
145
|
|
|
'session' => $session->getName(), |
146
|
|
|
], |
147
|
|
|
]; |
148
|
|
|
try { |
149
|
|
|
$response = $client->request('GET', $uri, $options); |
150
|
|
|
if (Response::HTTP_OK === $response->getStatusCode()) { |
151
|
|
|
$data = $response->getBody()->getContents(); |
152
|
|
|
$processor = unserialize($data); |
153
|
|
|
$event->getProcessor()->merge($processor); |
154
|
|
|
} |
155
|
|
|
} catch (\Exception $exception) { |
156
|
|
|
$event->addException($exception); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.