Skip to content

Commit cdb5d13

Browse files
committed
Adds VCR listener, tests and test setup.
1 parent 653a517 commit cdb5d13

File tree

6 files changed

+762
-0
lines changed

6 files changed

+762
-0
lines changed

PHPUnit/Util/Log/VCR.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
/**
3+
* A TestListener that integrates with PHP-VCR.
4+
*
5+
* Here is an example XML configuration for activating this listener:
6+
*
7+
* <code>
8+
* <listeners>
9+
* <listener class="PHPUnit_Util_Log_VCR" file="PHPUnit/Util/Log/VCR.php" />
10+
* </listeners>
11+
* </code>
12+
*
13+
* @package PHPUnit
14+
* @subpackage Util_Log
15+
* @author Adrian Philipp <mail@adrian-philipp.com>
16+
* @copyright 2011-2012 Adrian Philipp <mail@adrian-philipp.com>
17+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
18+
* @version Release: @package_version@
19+
* @link http://www.phpunit.de/
20+
* @since Class available since Release 1.0.0
21+
*/
22+
class PHPUnit_Util_Log_VCR implements PHPUnit_Framework_TestListener
23+
{
24+
/**
25+
* @var array
26+
*/
27+
protected $runs = array();
28+
29+
/**
30+
* @var array
31+
*/
32+
protected $options = array();
33+
34+
/**
35+
* @var integer
36+
*/
37+
protected $suites = 0;
38+
39+
/**
40+
* Constructor.
41+
*
42+
* @param array $options
43+
*/
44+
public function __construct(array $options = array())
45+
{
46+
}
47+
48+
/**
49+
* An error occurred.
50+
*
51+
* @param PHPUnit_Framework_Test $test
52+
* @param Exception $e
53+
* @param float $time
54+
*/
55+
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
56+
{
57+
}
58+
59+
/**
60+
* A failure occurred.
61+
*
62+
* @param PHPUnit_Framework_Test $test
63+
* @param PHPUnit_Framework_AssertionFailedError $e
64+
* @param float $time
65+
*/
66+
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
67+
{
68+
}
69+
70+
/**
71+
* Incomplete test.
72+
*
73+
* @param PHPUnit_Framework_Test $test
74+
* @param Exception $e
75+
* @param float $time
76+
*/
77+
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
78+
{
79+
}
80+
81+
/**
82+
* Skipped test.
83+
*
84+
* @param PHPUnit_Framework_Test $test
85+
* @param Exception $e
86+
* @param float $time
87+
*/
88+
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
89+
{
90+
}
91+
92+
/**
93+
* A test started.
94+
*
95+
* @param PHPUnit_Framework_Test $test
96+
*/
97+
public function startTest(PHPUnit_Framework_Test $test)
98+
{
99+
$class = get_class($test);
100+
$method = $test->getName();
101+
102+
if (!method_exists($class, $method)) {
103+
return;
104+
}
105+
106+
$reflection = new ReflectionMethod($class, $method);
107+
$doc_block = $reflection->getDocComment();
108+
109+
// Use regex to parse the doc_block for a specific annotation
110+
$cassetteName = self::parseDocBlock($doc_block, '@vcr');
111+
112+
if (empty($cassetteName)) {
113+
return true;
114+
}
115+
116+
\VCR\VCR::turnOn();
117+
\VCR\VCR::insertCassette(array_pop($cassetteName));
118+
}
119+
120+
private static function parseDocBlock($doc_block, $tag)
121+
{
122+
$matches = array();
123+
124+
if (empty($doc_block))
125+
return $matches;
126+
127+
$regex = "/{$tag} (.*)(\\r\\n|\\r|\\n)/U";
128+
preg_match_all($regex, $doc_block, $matches);
129+
130+
if (empty($matches[1])) {
131+
return array();
132+
}
133+
134+
// Removed extra index
135+
$matches = $matches[1];
136+
137+
// Trim the results, array item by array item
138+
foreach ($matches as $ix => $match)
139+
$matches[$ix] = trim( $match );
140+
141+
return $matches;
142+
}
143+
/**
144+
* A test ended.
145+
*
146+
* @param PHPUnit_Framework_Test $test
147+
* @param float $time
148+
*/
149+
public function endTest(PHPUnit_Framework_Test $test, $time)
150+
{
151+
\VCR\VCR::turnOff();
152+
}
153+
154+
/**
155+
* A test suite started.
156+
*
157+
* @param PHPUnit_Framework_TestSuite $suite
158+
*/
159+
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
160+
{
161+
}
162+
163+
/**
164+
* A test suite ended.
165+
*
166+
* @param PHPUnit_Framework_TestSuite $suite
167+
*/
168+
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
169+
{
170+
171+
}
172+
}

0 commit comments

Comments
 (0)