1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* Code Coverage Driver. |
6
|
|
|
* |
7
|
|
|
* @copyright 2013 Anthon Pang |
8
|
|
|
* |
9
|
|
|
* @license BSD-2-Clause |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DVDoug\Behat\CodeCoverage\Driver; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use SebastianBergmann\CodeCoverage\Driver\Driver; |
17
|
|
|
use SebastianBergmann\CodeCoverage\RawCodeCoverageData; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Remote xdebug driver. |
21
|
|
|
* |
22
|
|
|
* @author Anthon Pang <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class RemoteXdebug extends Driver |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var GuzzleHttp\Client |
|
|
|
|
33
|
|
|
*/ |
34
|
|
|
private $client; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* [ |
40
|
|
|
* 'base_uri' => 'http://api.example.com/1.0/coverage', |
41
|
|
|
* 'auth' => [ |
42
|
|
|
* 'user' => 'user name', |
43
|
|
|
* 'password' => 'password', |
44
|
|
|
* ], |
45
|
|
|
* 'create' => [ |
46
|
|
|
* 'method' => 'POST', |
47
|
|
|
* 'path' => '/', |
48
|
|
|
* ], |
49
|
|
|
* 'read' => [ |
50
|
|
|
* 'method' => 'GET', |
51
|
|
|
* 'path' => '/', |
52
|
|
|
* ], |
53
|
|
|
* 'delete' => [ |
54
|
|
|
* 'method' => 'DELETE', |
55
|
|
|
* 'path' => '/', |
56
|
|
|
* ], |
57
|
|
|
* ] |
58
|
|
|
* |
59
|
|
|
* @param array $config Configuration |
60
|
|
|
* @param GuzzleHttp\Client $client HTTP client |
61
|
|
|
*/ |
62
|
10 |
|
public function __construct(array $config, Client $client) |
63
|
|
|
{ |
64
|
10 |
|
$this->config = $config; |
65
|
|
|
|
66
|
10 |
|
$this->client = $client; |
|
|
|
|
67
|
|
|
//$this->client->setBaseUrl($config['base_url']); |
68
|
5 |
|
} |
69
|
|
|
|
70
|
6 |
|
public function start(bool $determineUnusedAndDead = true): void |
71
|
|
|
{ |
72
|
6 |
|
$response = $this->sendRequest('create'); |
73
|
|
|
|
74
|
4 |
|
if ($response->getStatusCode() !== 200) { |
75
|
4 |
|
throw new \Exception(sprintf('remote driver start failed: %s', $response->getReasonPhrase())); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
public function stop(): RawCodeCoverageData |
80
|
|
|
{ |
81
|
4 |
|
$response = $this->sendRequest('read', ['headers' => ['Accept' => 'application/json']]); |
82
|
|
|
|
83
|
4 |
|
if ($response->getStatusCode() !== 200) { |
84
|
4 |
|
throw new \Exception(sprintf('remote driver fetch failed: %s', $response->getReasonPhrase())); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->sendRequest('delete'); |
88
|
|
|
|
89
|
|
|
return RawCodeCoverageData::fromXdebugWithoutPathCoverage(json_decode($response->getBody(true), true)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
10 |
|
private function sendRequest(string $endpoint, array $headers = []): ResponseInterface |
93
|
|
|
{ |
94
|
10 |
|
$method = strtolower($this->config[$endpoint]['method']); |
95
|
|
|
|
96
|
10 |
|
if (!in_array($method, ['get', 'post', 'put', 'delete'])) { |
97
|
2 |
|
throw new \Exception(sprintf('%s method must be GET, POST, PUT, or DELETE', $endpoint)); |
98
|
|
|
} |
99
|
|
|
|
100
|
8 |
|
if (isset($this->config['auth'])) { |
101
|
8 |
|
$response = $this->client->request( |
102
|
8 |
|
$method, |
103
|
8 |
|
$this->config[$endpoint]['path'], |
104
|
|
|
[ |
105
|
8 |
|
'auth' => [$this->config['auth']['user'], $this->config['auth']['password']], |
106
|
8 |
|
'headers' => $headers, |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
} else { |
110
|
|
|
$response = $this->client->request( |
111
|
|
|
$method, |
112
|
|
|
$this->config[$endpoint]['path'], |
113
|
|
|
[ |
114
|
|
|
'headers' => $headers, |
115
|
|
|
] |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
8 |
|
return $response; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function nameAndVersion(): string |
123
|
|
|
{ |
124
|
|
|
return 'Remote Xdebug'; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|