Skip to content

Commit 9520791

Browse files
committed
Merge pull request php-vcr#110 from dbu/avoid-writeable-check-in-readonly-mode
Avoid writeable check when in mode 'none' to run on readonly filesystems
2 parents 715215b + e84e5fc commit 9520791

File tree

8 files changed

+44
-29
lines changed

8 files changed

+44
-29
lines changed

src/VCR/Configuration.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,11 @@ class Configuration
121121
private $blackList = array('src/VCR/LibraryHooks/', 'src/VCR/Util/SoapClient', 'tests/VCR/Filter');
122122

123123
/**
124-
* The mode which determines how requests are handled
125-
*
126-
* Currently supported modes:
127-
* - new_episodes (Always allows new HTTP requests)
128-
* - once (Will allow new HTTP requests the first time the cassette is created then throw an exception after that)
129-
* - none (Will never allow new HTTP requests)
124+
* The mode which determines how requests are handled. One of the MODE constants.
130125
*
131126
* @var string Current mode
132127
*/
133-
private $mode = 'new_episodes';
128+
private $mode = VCR::MODE_NEW_EPISODES;
134129

135130
/**
136131
* List of available modes.
@@ -143,9 +138,9 @@ class Configuration
143138
* @var array List of available modes.
144139
*/
145140
private $availableModes = array(
146-
'new_episodes',
147-
'once',
148-
'none'
141+
VCR::MODE_NEW_EPISODES,
142+
VCR::MODE_ONCE,
143+
VCR::MODE_NONE,
149144
);
150145

151146
/**
@@ -358,7 +353,7 @@ public function getMode()
358353
/**
359354
* Sets the current mode.
360355
*
361-
* @param string The mode to set VCR to
356+
* @param string $mode The mode to set VCR to
362357
*
363358
* @return Configuration
364359
*/

src/VCR/Storage/AbstractStorage.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,29 @@ abstract class AbstractStorage implements Storage
5454
* If the cassetteName contains PATH_SEPARATORs, subfolders of the
5555
* cassettePath are autocreated when not existing.
5656
*
57-
* @param string $cassettePath Path to the cassette directory.
58-
* @param string $cassetteName Path to the cassette file, relative to the path.
57+
* @param string $cassettePath Path to the cassette directory.
58+
* @param string $cassetteName Path to the cassette file, relative to the path.
59+
* @param string $defaultContent Default data for this cassette if its not existing
5960
*/
6061
public function __construct($cassettePath, $cassetteName, $defaultContent = '[]')
6162
{
6263
Assertion::directory($cassettePath, "Cassette path '{$cassettePath}' is not existing or not a directory");
6364

64-
$file = rtrim($cassettePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $cassetteName;
65+
$this->filePath = rtrim($cassettePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $cassetteName;
6566

66-
if (!is_dir(dirname($file))) {
67-
mkdir(dirname($file), 0777, true);
67+
if (!is_dir(dirname($this->filePath))) {
68+
mkdir(dirname($this->filePath), 0777, true);
6869
}
6970

70-
if (!file_exists($file) || 0 === filesize($file)) {
71-
file_put_contents($file, $defaultContent);
72-
71+
if (!file_exists($this->filePath) || 0 === filesize($this->filePath)) {
72+
file_put_contents($this->filePath, $defaultContent);
7373
$this->isNew = true;
74+
} else {
75+
Assertion::file($this->filePath, "Specified path '{$this->filePath}' is not a file.");
76+
Assertion::readable($this->filePath, "Specified file '{$this->filePath}' must be readable.");
7477
}
7578

76-
Assertion::file($file, "Specified path '{$file}' is not a file.");
77-
Assertion::readable($file, "Specified file '{$file}' must be readable.");
78-
Assertion::writeable($file, "Specified path '{$file}' must be writeable.");
79-
80-
$this->handle = fopen($file, 'r+');
79+
$this->handle = fopen($this->filePath, 'r+');
8180
}
8281

8382
/**

src/VCR/Storage/Json.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace VCR\Storage;
44

5+
use VCR\Util\Assertion;
6+
57
/**
68
* Json based storage for records.
79
*
@@ -15,6 +17,8 @@ class Json extends AbstractStorage
1517
*/
1618
public function storeRecording(array $recording)
1719
{
20+
Assertion::writeable($this->filePath, "Specified path '{$this->filePath}' must be writeable.");
21+
1822
fseek($this->handle, -1, SEEK_END);
1923
if (ftell($this->handle) > 2) {
2024
fwrite($this->handle, ',');

src/VCR/Storage/Yaml.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\Yaml\Parser;
66
use Symfony\Component\Yaml\Dumper;
7+
use VCR\Util\Assertion;
78

89
/**
910
* Yaml based storage for records.
@@ -44,6 +45,8 @@ public function __construct($cassettePath, $cassetteName, Parser $parser = null,
4445
*/
4546
public function storeRecording(array $recording)
4647
{
48+
Assertion::writeable($this->filePath, "Specified path '{$this->filePath}' must be writeable.");
49+
4750
fseek($this->handle, -1, SEEK_END);
4851
fwrite($this->handle, "\n" . $this->yamlDumper->dump(array($recording), 4));
4952
fflush($this->handle);

src/VCR/VCR.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
*/
1414
class VCR
1515
{
16+
/**
17+
* Always allow to do HTTP requests and add to the cassette. Default mode.
18+
*/
19+
const MODE_NEW_EPISODES = 'new_episodes';
20+
21+
/**
22+
* Only allow new HTTP requests when the cassette is newly created.
23+
*/
24+
const MODE_ONCE = 'once';
25+
26+
/**
27+
* Treat the fixtures as read only and never allow new HTTP requests.
28+
*/
29+
const MODE_NONE = 'none';
30+
1631
public static function __callStatic($method, $parameters)
1732
{
1833
$instance = VCRFactory::get('VCR\Videorecorder');

src/VCR/Videorecorder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function handleRequest(Request $request)
223223
return $response;
224224
}
225225

226-
if ($this->config->getMode() == 'none' || $this->config->getMode() == 'once' && $this->cassette->isNew() === false) {
226+
if (VCR::MODE_NONE === $this->config->getMode() || VCR::MODE_ONCE === $this->config->getMode() && $this->cassette->isNew() === false) {
227227
throw new \LogicException(
228228
"The request does not match a previously recorded request and the 'mode' is set to '{$this->config->getMode()}'. "
229229
. "If you want to send the request anyway, make sure your 'mode' is set to 'new_episodes'. "

tests/VCR/Storage/AbstractStorageTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ public function testRootNotExisting()
3030
$this->setExpectedException('\VCR\VCRException', "Cassette path 'vfs://test/foo' is not existing or not a directory");
3131

3232
vfsStream::setup('test');
33-
$this->storage = new TestStorage(vfsStream::url('test/foo'), 'file');
33+
new TestStorage(vfsStream::url('test/foo'), 'file');
3434
}
35-
3635
}
3736

3837
class TestStorage extends AbstractStorage

tests/VCR/VideorecorderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected function getClientMock($request, $response)
143143
return $client;
144144
}
145145

146-
protected function getCassetteMock($request, $response, $mode = 'new_episodes', $isNew = false)
146+
protected function getCassetteMock($request, $response, $mode = VCR::MODE_NEW_EPISODES, $isNew = false)
147147
{
148148
$cassette = $this->getMockBuilder('\VCR\Cassette')
149149
->disableOriginalConstructor()
@@ -155,7 +155,7 @@ protected function getCassetteMock($request, $response, $mode = 'new_episodes',
155155
->with($request)
156156
->will($this->returnValue(false));
157157

158-
if ($mode == 'new_episodes' || $mode == 'once' && $isNew === true) {
158+
if (VCR::MODE_NEW_EPISODES === $mode || VCR::MODE_ONCE === $mode && $isNew === true) {
159159
$cassette
160160
->expects($this->once())
161161
->method('record')

0 commit comments

Comments
 (0)