From 76f09fc1549c345d3ea9a80fa3e6e918de1debc0 Mon Sep 17 00:00:00 2001 From: Vitaly Grams Date: Tue, 7 Mar 2023 13:41:26 +0600 Subject: [PATCH] fix: remove tmp data after saving production data; --- src/Drivers/LocalDriver.php | 4 ++++ src/Drivers/StorageDriver.php | 18 +++++++++++------- tests/LocalDriverTest.php | 4 ++-- tests/StorageDriverTest.php | 7 +++---- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 8c14a99..6332dba 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -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 diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index 2525fcf..d259f74 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -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'; @@ -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); } @@ -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); } diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index 6ce29f0..f938a09 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -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() diff --git a/tests/StorageDriverTest.php b/tests/StorageDriverTest.php index c59fc2b..e1a94f4 100755 --- a/tests/StorageDriverTest.php +++ b/tests/StorageDriverTest.php @@ -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() @@ -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()