Skip to content

Commit b145def

Browse files
committed
Merge pull request php-vcr#91 from aaa2000/wrong-json-stored-in-casette
Correction of JSON when the file already exists and is blank
2 parents 0f186ba + 8d9a993 commit b145def

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/VCR/Storage/AbstractStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct($cassettePath, $cassetteName, $defaultContent = '[]'
5757
{
5858
$file = $cassettePath . DIRECTORY_SEPARATOR . $cassetteName;
5959

60-
if (!file_exists($file)) {
60+
if (!file_exists($file) || 0 === filesize($file)) {
6161
file_put_contents($file, $defaultContent);
6262

6363
$this->isNew = true;

tests/VCR/Storage/JsonTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ public function testValidJson()
106106

107107
}
108108

109+
public function testStoreRecordingWhenBlankFileAlreadyExists()
110+
{
111+
vfsStream::create(array('blank_file_test' => ''));
112+
$filePath = vfsStream::url('test/') . 'blank_file_test';
113+
114+
$jsonObject = new Json(vfsStream::url('test/'), 'blank_file_test');
115+
$stored = array(
116+
'request' => 'some request',
117+
'response' => 'some response'
118+
);
119+
$jsonObject->storeRecording($stored);
120+
121+
$this->assertJson(file_get_contents($filePath));
122+
}
123+
109124
private function iterateAndTest($json, $expected, $message)
110125
{
111126
file_put_contents($this->filePath, $json);

0 commit comments

Comments
 (0)