-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathVCRTestListener.php
216 lines (187 loc) · 4.67 KB
/
VCRTestListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace VCR\PHPUnit\TestListener;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
use VCR\VCR;
/**
* A TestListener that integrates with PHP-VCR.
*
* Here is an example XML configuration for activating this listener:
*
* <code>
* <listeners>
* <listener class="VCR\PHPUnit\TestListener\VCRTestListener" file="vendor/php-vcr/phpunit-testlistener-vcr/src/VCRTestListener.php" />
* </listeners>
* </code>
*
* @author Adrian Philipp <[email protected]>
* @author Davide Borsatto <[email protected]>
* @copyright 2011-2017 Adrian Philipp <[email protected]>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
*
* @version Release: @package_version@
*
* @see http://www.phpunit.de/
*/
class VCRTestListener implements TestListener
{
/**
* @var array
*/
protected $runs = array();
/**
* @var array
*/
protected $options = array();
/**
* @var int
*/
protected $suites = 0;
/**
* Constructor.
*
* @param array $options
*/
public function __construct(array $options = array())
{
}
/**
* An error occurred.
*
* @param Test $test
* @param \Throwable $t
* @param float $time
*/
public function addError(Test $test, \Throwable $t, float $time): void
{
}
/**
* A warning occurred.
*
* @param Test $test
* @param Warning $e
* @param float $time
*
* @since Method available since Release 5.1.0
*/
public function addWarning(Test $test, Warning $e, float $time): void
{
}
/**
* A failure occurred.
*
* @param Test $test
* @param AssertionFailedError $e
* @param float $time
*/
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
{
}
/**
* Incomplete test.
*
* @param Test $test
* @param \Throwable $e
* @param float $time
*/
public function addIncompleteTest(Test $test, \Throwable $e, float $time): void
{
}
/**
* Skipped test.
*
* @param Test $test
* @param \Throwable $e
* @param float $time
*/
public function addSkippedTest(Test $test, \Throwable $e, float $time): void
{
}
/**
* Risky test.
*
* @param Test $test
* @param \Throwable $e
* @param float $time
*/
public function addRiskyTest(Test $test, \Throwable $e, float $time): void
{
}
/**
* A test started.
*
* @param Test $test
*
* @throws \ReflectionException
*/
public function startTest(Test $test): void
{
$class = get_class($test);
$method = $test->getName(false);
if (!method_exists($class, $method)) {
return;
}
$reflection = new \ReflectionMethod($class, $method);
$docBlock = $reflection->getDocComment();
// Use regex to parse the doc_block for a specific annotation
$parsed = self::parseDocBlock($docBlock, '@vcr');
$cassetteName = array_pop($parsed);
// If the cassette name ends in .json, then use the JSON storage format
if (substr($cassetteName, '-5') == '.json') {
VCR::configure()->setStorage('json');
}
if (empty($cassetteName)) {
return;
}
VCR::turnOn();
VCR::insertCassette($cassetteName);
}
private static function parseDocBlock($docBlock, $tag)
{
$matches = array();
if (empty($docBlock)) {
return $matches;
}
$regex = "/{$tag} (.*)(\\r\\n|\\r|\\n)/U";
preg_match_all($regex, $docBlock, $matches);
if (empty($matches[1])) {
return array();
}
// Removed extra index
$matches = $matches[1];
// Trim the results, array item by array item
foreach ($matches as $ix => $match) {
$matches[$ix] = trim($match);
}
return $matches;
}
/**
* A test ended.
*
* @param Test $test
* @param float $time
*/
public function endTest(Test $test, float $time): void
{
VCR::turnOff();
}
/**
* A test suite started.
*
* @param TestSuite $suite
*/
public function startTestSuite(TestSuite $suite): void
{
}
/**
* A test suite ended.
*
* @param TestSuite $suite
*/
public function endTestSuite(TestSuite $suite): void
{
}
}