Skip to content

Commit

Permalink
fix: remove tmp data after saving production data;
Browse files Browse the repository at this point in the history
  • Loading branch information
vitgrams committed Mar 7, 2023
1 parent 4453ec7 commit 76f09fc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function getTmpData()
public function saveData()
{
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));

if (file_exists($this->tempFilePath)) {
unlink($this->tempFilePath);
}
}

public function getDocumentation(): array
Expand Down
18 changes: 11 additions & 7 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class StorageDriver implements SwaggerDriverInterface

public function __construct()
{
$this->disk = config('auto-doc.drivers.storage.disk');
$this->disk = Storage::disk(config('auto-doc.drivers.storage.disk'));
$this->prodFilePath = config('auto-doc.drivers.storage.production_path');
$this->tempFilePath = 'temp_documentation.json';

Expand All @@ -26,13 +26,13 @@ public function __construct()

public function saveTmpData($data)
{
Storage::disk($this->disk)->put($this->tempFilePath, json_encode($data));
$this->disk->put($this->tempFilePath, json_encode($data));
}

public function getTmpData()
{
if (Storage::disk($this->disk)->exists($this->tempFilePath)) {
$content = Storage::disk($this->disk)->get($this->tempFilePath);
if ($this->disk->exists($this->tempFilePath)) {
$content = $this->disk->get($this->tempFilePath);

return json_decode($content, true);
}
Expand All @@ -42,16 +42,20 @@ public function getTmpData()

public function saveData()
{
Storage::disk($this->disk)->put($this->prodFilePath, json_encode($this->getTmpData()));
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));

if ($this->disk->exists($this->tempFilePath)) {
$this->disk->delete($this->tempFilePath);
}
}

public function getDocumentation(): array
{
if (!Storage::disk($this->disk)->exists($this->prodFilePath)) {
if (!$this->disk->exists($this->prodFilePath)) {
throw new FileNotFoundException();
}

$fileContent = Storage::disk($this->disk)->get($this->prodFilePath);
$fileContent = $this->disk->get($this->prodFilePath);

return json_decode($fileContent, true);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ public function testGetAndSaveTmpData()

public function testSaveData()
{
$this->localDriverClass->saveTmpData($this->tmpData);
file_put_contents($this->tmpDocumentationFilePath, json_encode($this->tmpData));

$this->localDriverClass->saveData();

$this->assertFileExists($this->productionFilePath);
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), $this->productionFilePath);

$this->assertEqualsJsonFixture('tmp_data', $this->localDriverClass->getTmpData());
$this->assertFileDoesNotExist($this->tmpDocumentationFilePath);
}

public function testGetDocumentation()
Expand Down
7 changes: 3 additions & 4 deletions tests/StorageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function testSaveTmpData()

$this->disk->assertExists($this->tmpDocumentationFilePath);

$tmpDocumentation = json_decode($this->disk->get($this->tmpDocumentationFilePath), true);
$this->assertEqualsJsonFixture('tmp_data_non_formatted', $tmpDocumentation);
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get($this->tmpDocumentationFilePath));
}

public function testGetTmpData()
Expand Down Expand Up @@ -76,14 +75,14 @@ public function testGetAndSaveTmpData()

public function testSaveData()
{
$this->storageDriverClass->saveTmpData($this->tmpData);
$this->disk->put($this->tmpDocumentationFilePath, json_encode($this->tmpData));

$this->storageDriverClass->saveData();

$this->disk->assertExists($this->productionFilePath);
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get($this->productionFilePath));

$this->assertEqualsJsonFixture('tmp_data', $this->storageDriverClass->getTmpData());
$this->disk->assertMissing($this->tmpDocumentationFilePath);
}

public function testGetDocumentation()
Expand Down

0 comments on commit 76f09fc

Please sign in to comment.