From 748d42464db64d618bb2df71ad91d2fa5c65f6a1 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 13 Dec 2024 13:05:54 +0330 Subject: [PATCH 01/16] Add `documentation_directory` key to the config file; --- config/auto-doc.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index 1ed5fe93..74fd5821 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -15,6 +15,15 @@ */ 'route' => '', + /* + |-------------------------------------------------------------------------- + | Documentation Directory + |-------------------------------------------------------------------------- + | + | Documentation file will be stored in the determined directory + */ + 'documentation_directory' => storage_path('documentations'), + /* |-------------------------------------------------------------------------- | Global application prefix @@ -128,7 +137,7 @@ 'drivers' => [ 'local' => [ 'class' => LocalDriver::class, - 'production_path' => storage_path('documentation.json'), + 'production_path' => 'documentation.json', ], 'remote' => [ 'class' => RemoteDriver::class, From cc6d056997404363db481035dc526e5bc8a2fabf Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 13 Dec 2024 13:07:01 +0330 Subject: [PATCH 02/16] Update drivers; --- src/Drivers/BaseDriver.php | 6 +++++- src/Drivers/LocalDriver.php | 5 ++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Drivers/BaseDriver.php b/src/Drivers/BaseDriver.php index 0bef9821..def27e31 100644 --- a/src/Drivers/BaseDriver.php +++ b/src/Drivers/BaseDriver.php @@ -10,7 +10,11 @@ abstract class BaseDriver implements SwaggerDriverContract public function __construct() { - $this->tempFilePath = storage_path('temp_documentation.json'); + $prodDir = config('auto-doc.documentation_directory'); + if (!is_dir($prodDir)) { + mkdir($prodDir); + } + $this->tempFilePath = $prodDir.DIRECTORY_SEPARATOR.'temp_documentation.json'; } public function saveTmpData($data): void diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index f8b7f279..3a3a1a29 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -12,10 +12,9 @@ class LocalDriver extends BaseDriver public function __construct() { parent::__construct(); + $this->prodFilePath = config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR.config('auto-doc.drivers.local.production_path'); - $this->prodFilePath = config('auto-doc.drivers.local.production_path'); - - if (empty($this->prodFilePath)) { + if (!preg_match('/\/[\w]+\.json/ms',$this->prodFilePath)) { throw new MissedProductionFilePathException(); } } From 4e1970f99be38f9f92284803dfccf0cbe468dd54 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 13 Dec 2024 13:10:45 +0330 Subject: [PATCH 03/16] Fix tests; --- tests/AutoDocControllerTest.php | 6 ++++-- tests/LocalDriverTest.php | 24 +++++++++++++----------- tests/RemoteDriverTest.php | 14 ++++++++------ tests/StorageDriverTest.php | 14 ++++++++------ 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index daaf8cae..ea4b7500 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -12,16 +12,18 @@ class AutoDocControllerTest extends TestCase use PHPMock; protected static array $documentation; + protected static string $documentationDirectory; protected static string $localDriverFilePath; public function setUp(): void { parent::setUp(); - self::$localDriverFilePath ??= __DIR__ . '/../storage/documentation.json'; + self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; + self::$localDriverFilePath ??= 'documentation.json'; self::$documentation ??= $this->getJsonFixture('tmp_data'); - file_put_contents(self::$localDriverFilePath, json_encode(self::$documentation)); + file_put_contents(self::$documentationDirectory.self::$localDriverFilePath, json_encode(self::$documentation)); config(['auto-doc.drivers.local.production_path' => self::$localDriverFilePath]); } diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index d96a7db1..b5c61733 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -9,6 +9,7 @@ class LocalDriverTest extends TestCase { protected static LocalDriver $localDriverClass; + protected static string $documentationDirectory; protected static string $productionFilePath; protected static string $tmpDocumentationFilePath; protected static array $tmpData; @@ -17,8 +18,9 @@ public function setUp(): void { parent::setUp(); - self::$productionFilePath ??= __DIR__ . '/../storage/documentation.json'; - self::$tmpDocumentationFilePath ??= __DIR__ . '/../storage/temp_documentation.json'; + self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; + self::$productionFilePath ??= 'documentation.json'; + self::$tmpDocumentationFilePath ??= 'temp_documentation.json'; self::$tmpData ??= $this->getJsonFixture('tmp_data'); @@ -31,13 +33,13 @@ public function testSaveTmpData() { self::$localDriverClass->saveTmpData(self::$tmpData); - $this->assertFileExists(self::$tmpDocumentationFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); + $this->assertFileExists(self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$tmpDocumentationFilePath); } public function testGetTmpData() { - file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $result = self::$localDriverClass->getTmpData(); @@ -69,19 +71,19 @@ public function testGetAndSaveTmpData() public function testSaveData() { - file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); self::$localDriverClass->saveData(); - $this->assertFileExists(self::$productionFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$productionFilePath); + $this->assertFileExists(self::$documentationDirectory.self::$productionFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$productionFilePath); - $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); + $this->assertFileDoesNotExist(self::$documentationDirectory.self::$tmpDocumentationFilePath); } public function testGetDocumentation() { - file_put_contents(self::$productionFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$documentationDirectory.self::$productionFilePath, json_encode(self::$tmpData)); $documentation = self::$localDriverClass->getDocumentation(); @@ -92,7 +94,7 @@ public function testGetDocumentationFileNotExists() { $this->expectException(FileNotFoundException::class); - config(['auto-doc.drivers.local.production_path' => 'not_exists_file']); + config(['auto-doc.drivers.local.production_path' => 'not_exists_file.json']); (new LocalDriver())->getDocumentation(); } diff --git a/tests/RemoteDriverTest.php b/tests/RemoteDriverTest.php index 70a80971..2353b7ce 100755 --- a/tests/RemoteDriverTest.php +++ b/tests/RemoteDriverTest.php @@ -13,6 +13,7 @@ class RemoteDriverTest extends TestCase protected static array $tmpData; protected static RemoteDriver $remoteDriverClass; + protected static string $documentationDirectory; protected static string $tmpDocumentationFilePath; public function setUp(): void @@ -20,7 +21,8 @@ public function setUp(): void parent::setUp(); self::$tmpData ??= $this->getJsonFixture('tmp_data'); - self::$tmpDocumentationFilePath ??= __DIR__ . '/../storage/temp_documentation.json'; + self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; + self::$tmpDocumentationFilePath ??= 'temp_documentation.json'; self::$remoteDriverClass ??= new RemoteDriver(); } @@ -29,13 +31,13 @@ public function testSaveTmpData() { self::$remoteDriverClass->saveTmpData(self::$tmpData); - $this->assertFileExists(self::$tmpDocumentationFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); + $this->assertFileExists(self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$tmpDocumentationFilePath); } public function testGetTmpData() { - file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $result = self::$remoteDriverClass->getTmpData(); @@ -73,11 +75,11 @@ public function testSaveData() ]) ->willReturn(['', 204]); - file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $mock->saveData(); - $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); + $this->assertFileDoesNotExist(self::$documentationDirectory.self::$tmpDocumentationFilePath); } public function testSaveDataWithoutTmpFile() diff --git a/tests/StorageDriverTest.php b/tests/StorageDriverTest.php index a30da60a..9b1533e4 100755 --- a/tests/StorageDriverTest.php +++ b/tests/StorageDriverTest.php @@ -12,6 +12,7 @@ class StorageDriverTest extends TestCase { protected static StorageDriver $storageDriverClass; protected Filesystem $disk; + protected static string $documentationDirectory; protected static string $productionFilePath; protected static string $tmpDocumentationFilePath; protected static array $tmpData; @@ -22,8 +23,9 @@ public function setUp(): void $this->disk = Storage::fake('testing'); + self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; self::$productionFilePath ??= 'documentation.json'; - self::$tmpDocumentationFilePath ??= __DIR__ . '/../storage/temp_documentation.json'; + self::$tmpDocumentationFilePath ??= 'temp_documentation.json'; self::$tmpData ??= $this->getJsonFixture('tmp_data'); @@ -37,13 +39,13 @@ public function testSaveTmpData() { self::$storageDriverClass->saveTmpData(self::$tmpData); - $this->assertFileExists(self::$tmpDocumentationFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); + $this->assertFileExists(self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$tmpDocumentationFilePath); } public function testGetTmpData() { - file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $result = self::$storageDriverClass->getTmpData(); @@ -75,14 +77,14 @@ public function testGetAndSaveTmpData() public function testSaveData() { - file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); self::$storageDriverClass->saveData(); $this->disk->assertExists(self::$productionFilePath); $this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get(self::$productionFilePath)); - $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); + $this->assertFileDoesNotExist(self::$documentationDirectory.self::$tmpDocumentationFilePath); } public function testGetDocumentation() From 7d08de0634e1253ef905e811412b449d5e027173 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 13 Dec 2024 13:11:39 +0330 Subject: [PATCH 04/16] Increase config version; --- config/auto-doc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index 74fd5821..44c432ad 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -193,5 +193,5 @@ 'development', ], - 'config_version' => '2.8', + 'config_version' => '2.9', ]; From a97158ebf82da8395bbe042c25d1093f52cc7bd0 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 13 Dec 2024 13:22:39 +0330 Subject: [PATCH 05/16] Fix `No such file or directory` error on running tests; --- tests/AutoDocControllerTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index ea4b7500..fc455a2f 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -23,6 +23,9 @@ public function setUp(): void self::$localDriverFilePath ??= 'documentation.json'; self::$documentation ??= $this->getJsonFixture('tmp_data'); + if (!is_dir(self::$documentationDirectory)) { + mkdir(self::$documentationDirectory); + } file_put_contents(self::$documentationDirectory.self::$localDriverFilePath, json_encode(self::$documentation)); config(['auto-doc.drivers.local.production_path' => self::$localDriverFilePath]); From 10705eabb696dee8f1d9b16ead5d9d12aa8182bf Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 20 Dec 2024 19:41:45 +0330 Subject: [PATCH 06/16] The `directory` config key added to local and storage drivers; --- config/auto-doc.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index 44c432ad..eae0005d 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -15,15 +15,6 @@ */ 'route' => '', - /* - |-------------------------------------------------------------------------- - | Documentation Directory - |-------------------------------------------------------------------------- - | - | Documentation file will be stored in the determined directory - */ - 'documentation_directory' => storage_path('documentations'), - /* |-------------------------------------------------------------------------- | Global application prefix @@ -137,6 +128,7 @@ 'drivers' => [ 'local' => [ 'class' => LocalDriver::class, + 'directory' => 'documentations', 'production_path' => 'documentation.json', ], 'remote' => [ @@ -155,6 +147,7 @@ | One of the filesystems.disks config value */ 'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'), + 'directory' => 'documentations', 'production_path' => 'documentation.json', ], ], From 8d25c0fc3de1d2a43ca57e4d1ce35739ee86c529 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 20 Dec 2024 19:43:14 +0330 Subject: [PATCH 07/16] The `production_path` key renamed; The `production_path` key renamed to `base_file_name` for both local and storage drivers; --- config/auto-doc.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/auto-doc.php b/config/auto-doc.php index eae0005d..04fea7fc 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -129,7 +129,7 @@ 'local' => [ 'class' => LocalDriver::class, 'directory' => 'documentations', - 'production_path' => 'documentation.json', + 'base_file_name' => 'documentation', ], 'remote' => [ 'class' => RemoteDriver::class, @@ -148,7 +148,7 @@ */ 'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'), 'directory' => 'documentations', - 'production_path' => 'documentation.json', + 'base_file_name' => 'documentation', ], ], From db649e983458b53fb0a078a4b1c9b9b1415ab907 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 28 Dec 2024 10:42:38 +0330 Subject: [PATCH 08/16] Add `.json` extension to `base_file_name` attr and check target dir existance; --- src/Drivers/BaseDriver.php | 6 +----- src/Drivers/LocalDriver.php | 25 +++++++++++++++++++------ src/Drivers/StorageDriver.php | 20 +++++++++++++------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Drivers/BaseDriver.php b/src/Drivers/BaseDriver.php index def27e31..0bef9821 100644 --- a/src/Drivers/BaseDriver.php +++ b/src/Drivers/BaseDriver.php @@ -10,11 +10,7 @@ abstract class BaseDriver implements SwaggerDriverContract public function __construct() { - $prodDir = config('auto-doc.documentation_directory'); - if (!is_dir($prodDir)) { - mkdir($prodDir); - } - $this->tempFilePath = $prodDir.DIRECTORY_SEPARATOR.'temp_documentation.json'; + $this->tempFilePath = storage_path('temp_documentation.json'); } public function saveTmpData($data): void diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 3a3a1a29..0a7f6e6f 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -7,32 +7,45 @@ class LocalDriver extends BaseDriver { - protected ?string $prodFilePath; + protected ?string $baseFileName; + private ?array $config; public function __construct() { parent::__construct(); - $this->prodFilePath = config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR.config('auto-doc.drivers.local.production_path'); + $this->config = config('auto-doc.drivers.local'); - if (!preg_match('/\/[\w]+\.json/ms',$this->prodFilePath)) { + $directory = $this->config['directory']; + if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) { + $directory .= DIRECTORY_SEPARATOR; + } + + $this->baseFileName = storage_path($directory.$this->config['base_file_name'].'.json'); + + if (!preg_match('/\/[\w]+\.json/ms', $this->baseFileName)) { throw new MissedProductionFilePathException(); } } public function saveData(): void { - file_put_contents($this->prodFilePath, json_encode($this->getTmpData())); + $prodDir = storage_path($this->config['directory']); + if (!is_dir($prodDir)) { + mkdir($prodDir); + } + + file_put_contents($this->baseFileName, json_encode($this->getTmpData())); $this->clearTmpData(); } public function getDocumentation(): array { - if (!file_exists($this->prodFilePath)) { + if (!file_exists($this->baseFileName)) { throw new FileNotFoundException(); } - $fileContent = file_get_contents($this->prodFilePath); + $fileContent = file_get_contents($this->baseFileName); return json_decode($fileContent, true); } diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index e08646dd..a6922464 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -10,34 +10,40 @@ class StorageDriver extends BaseDriver { protected Filesystem $disk; - protected ?string $prodFilePath; + protected ?string $baseFileName; + protected array $config; public function __construct() { parent::__construct(); - $this->disk = Storage::disk(config('auto-doc.drivers.storage.disk')); - $this->prodFilePath = config('auto-doc.drivers.storage.production_path'); + $this->config = config('auto-doc.drivers.storage'); + $this->disk = Storage::disk($this->config['disk']); + $directory = $this->config['directory']; + if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) { + $directory .= DIRECTORY_SEPARATOR; + } + $this->baseFileName = $directory.$this->config['base_file_name'].'.json'; - if (empty($this->prodFilePath)) { + if (!preg_match('/\/[\w]+\.json/ms', $this->baseFileName)) { throw new MissedProductionFilePathException(); } } public function saveData(): void { - $this->disk->put($this->prodFilePath, json_encode($this->getTmpData())); + $this->disk->put($this->baseFileName, json_encode($this->getTmpData())); $this->clearTmpData(); } public function getDocumentation(): array { - if (!$this->disk->exists($this->prodFilePath)) { + if (!$this->disk->exists($this->baseFileName)) { throw new FileNotFoundException(); } - $fileContent = $this->disk->get($this->prodFilePath); + $fileContent = $this->disk->get($this->baseFileName); return json_decode($fileContent, true); } From 9dd4951df12c12ebc31dbe638dd7dfd44682a7bf Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 28 Dec 2024 10:44:11 +0330 Subject: [PATCH 09/16] Update tests; --- tests/AutoDocControllerTest.php | 21 ++++++++++++------- tests/LocalDriverTest.php | 37 +++++++++++++++++++-------------- tests/RemoteDriverTest.php | 14 ++++++------- tests/StorageDriverTest.php | 35 ++++++++++++++++++------------- 4 files changed, 60 insertions(+), 47 deletions(-) diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index fc455a2f..edbd4a0c 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -12,23 +12,28 @@ class AutoDocControllerTest extends TestCase use PHPMock; protected static array $documentation; - protected static string $documentationDirectory; - protected static string $localDriverFilePath; + protected static string $baseFileName; + protected static string $baseFile; public function setUp(): void { parent::setUp(); - self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; - self::$localDriverFilePath ??= 'documentation.json'; + $documentationDirectory = config('auto-doc.drivers.local.directory'); + if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { + $documentationDirectory .= DIRECTORY_SEPARATOR; + } + + self::$baseFileName ??= 'documentation'; + self::$baseFile ??= $documentationDirectory.self::$baseFileName.'.json'; self::$documentation ??= $this->getJsonFixture('tmp_data'); - if (!is_dir(self::$documentationDirectory)) { - mkdir(self::$documentationDirectory); + if (!is_dir(storage_path($documentationDirectory))) { + mkdir(storage_path($documentationDirectory)); } - file_put_contents(self::$documentationDirectory.self::$localDriverFilePath, json_encode(self::$documentation)); + file_put_contents(storage_path(self::$baseFile), json_encode(self::$documentation)); - config(['auto-doc.drivers.local.production_path' => self::$localDriverFilePath]); + config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]); } public function tearDown(): void diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index b5c61733..ec735415 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -9,8 +9,8 @@ class LocalDriverTest extends TestCase { protected static LocalDriver $localDriverClass; - protected static string $documentationDirectory; - protected static string $productionFilePath; + protected static string $baseFileName; + protected static string $baseFile; protected static string $tmpDocumentationFilePath; protected static array $tmpData; @@ -18,13 +18,18 @@ public function setUp(): void { parent::setUp(); - self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; - self::$productionFilePath ??= 'documentation.json'; - self::$tmpDocumentationFilePath ??= 'temp_documentation.json'; + $documentationDirectory ??= config('auto-doc.drivers.local.directory'); + if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { + $documentationDirectory .= DIRECTORY_SEPARATOR; + } + + self::$baseFileName ??= 'documentation'; + self::$baseFile ??= storage_path($documentationDirectory.self::$baseFileName.'.json'); + self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json'); self::$tmpData ??= $this->getJsonFixture('tmp_data'); - config(['auto-doc.drivers.local.production_path' => self::$productionFilePath]); + config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]); self::$localDriverClass ??= new LocalDriver(); } @@ -33,13 +38,13 @@ public function testSaveTmpData() { self::$localDriverClass->saveTmpData(self::$tmpData); - $this->assertFileExists(self::$documentationDirectory.self::$tmpDocumentationFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileExists(self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); } public function testGetTmpData() { - file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $result = self::$localDriverClass->getTmpData(); @@ -57,7 +62,7 @@ public function testCreateClassConfigEmpty() { $this->expectException(MissedProductionFilePathException::class); - config(['auto-doc.drivers.local.production_path' => null]); + config(['auto-doc.drivers.local.base_file_name' => null]); new LocalDriver(); } @@ -71,19 +76,19 @@ public function testGetAndSaveTmpData() public function testSaveData() { - file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); self::$localDriverClass->saveData(); - $this->assertFileExists(self::$documentationDirectory.self::$productionFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$productionFilePath); + $this->assertFileExists(self::$baseFile); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$baseFile); - $this->assertFileDoesNotExist(self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); } public function testGetDocumentation() { - file_put_contents(self::$documentationDirectory.self::$productionFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$baseFile, json_encode(self::$tmpData)); $documentation = self::$localDriverClass->getDocumentation(); @@ -94,7 +99,7 @@ public function testGetDocumentationFileNotExists() { $this->expectException(FileNotFoundException::class); - config(['auto-doc.drivers.local.production_path' => 'not_exists_file.json']); + config(['auto-doc.drivers.local.base_file_name' => 'not_exists_file.json']); (new LocalDriver())->getDocumentation(); } diff --git a/tests/RemoteDriverTest.php b/tests/RemoteDriverTest.php index 2353b7ce..62fb31d2 100755 --- a/tests/RemoteDriverTest.php +++ b/tests/RemoteDriverTest.php @@ -13,7 +13,6 @@ class RemoteDriverTest extends TestCase protected static array $tmpData; protected static RemoteDriver $remoteDriverClass; - protected static string $documentationDirectory; protected static string $tmpDocumentationFilePath; public function setUp(): void @@ -21,8 +20,7 @@ public function setUp(): void parent::setUp(); self::$tmpData ??= $this->getJsonFixture('tmp_data'); - self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; - self::$tmpDocumentationFilePath ??= 'temp_documentation.json'; + self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json'); self::$remoteDriverClass ??= new RemoteDriver(); } @@ -31,13 +29,13 @@ public function testSaveTmpData() { self::$remoteDriverClass->saveTmpData(self::$tmpData); - $this->assertFileExists(self::$documentationDirectory.self::$tmpDocumentationFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileExists(self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); } public function testGetTmpData() { - file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $result = self::$remoteDriverClass->getTmpData(); @@ -75,11 +73,11 @@ public function testSaveData() ]) ->willReturn(['', 204]); - file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $mock->saveData(); - $this->assertFileDoesNotExist(self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); } public function testSaveDataWithoutTmpFile() diff --git a/tests/StorageDriverTest.php b/tests/StorageDriverTest.php index 9b1533e4..2b0983f0 100755 --- a/tests/StorageDriverTest.php +++ b/tests/StorageDriverTest.php @@ -12,8 +12,8 @@ class StorageDriverTest extends TestCase { protected static StorageDriver $storageDriverClass; protected Filesystem $disk; - protected static string $documentationDirectory; - protected static string $productionFilePath; + protected static string $baseFileName; + protected static string $baseFile; protected static string $tmpDocumentationFilePath; protected static array $tmpData; @@ -23,14 +23,19 @@ public function setUp(): void $this->disk = Storage::fake('testing'); - self::$documentationDirectory ??= config('auto-doc.documentation_directory').DIRECTORY_SEPARATOR; - self::$productionFilePath ??= 'documentation.json'; - self::$tmpDocumentationFilePath ??= 'temp_documentation.json'; + $documentationDirectory = config('auto-doc.drivers.storage.directory'); + if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { + $documentationDirectory .= DIRECTORY_SEPARATOR; + } + + self::$baseFileName ??= 'documentation'; + self::$baseFile ??= $documentationDirectory.self::$baseFileName.'.json'; + self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json'); self::$tmpData ??= $this->getJsonFixture('tmp_data'); config(['auto-doc.drivers.storage.disk' => 'testing']); - config(['auto-doc.drivers.storage.production_path' => self::$productionFilePath]); + config(['auto-doc.drivers.storage.base_file_name' => self::$baseFileName]); self::$storageDriverClass = new StorageDriver(); } @@ -39,13 +44,13 @@ public function testSaveTmpData() { self::$storageDriverClass->saveTmpData(self::$tmpData); - $this->assertFileExists(self::$documentationDirectory.self::$tmpDocumentationFilePath); - $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileExists(self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); } public function testGetTmpData() { - file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); $result = self::$storageDriverClass->getTmpData(); @@ -63,7 +68,7 @@ public function testCreateClassConfigEmpty() { $this->expectException(MissedProductionFilePathException::class); - config(['auto-doc.drivers.storage.production_path' => null]); + config(['auto-doc.drivers.storage.base_file_name' => null]); new StorageDriver(); } @@ -77,19 +82,19 @@ public function testGetAndSaveTmpData() public function testSaveData() { - file_put_contents(self::$documentationDirectory.self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); + file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData)); self::$storageDriverClass->saveData(); - $this->disk->assertExists(self::$productionFilePath); - $this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get(self::$productionFilePath)); + $this->disk->assertExists(self::$baseFile); + $this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get(self::$baseFile)); - $this->assertFileDoesNotExist(self::$documentationDirectory.self::$tmpDocumentationFilePath); + $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); } public function testGetDocumentation() { - $this->disk->put(self::$productionFilePath, $this->getFixture('tmp_data_non_formatted.json')); + $this->disk->put(self::$baseFile, $this->getFixture('tmp_data_non_formatted.json')); $documentation = self::$storageDriverClass->getDocumentation(); From 0fa0b4bd5a60a9086fc13eeecee80377b81dd94f Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 28 Dec 2024 10:51:30 +0330 Subject: [PATCH 10/16] Wip; --- tests/LocalDriverTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index ec735415..b52397f0 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -18,7 +18,7 @@ public function setUp(): void { parent::setUp(); - $documentationDirectory ??= config('auto-doc.drivers.local.directory'); + $documentationDirectory = config('auto-doc.drivers.local.directory'); if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { $documentationDirectory .= DIRECTORY_SEPARATOR; } From 4a387c26b32662279baa19a6c859f2ff8b586cd6 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 21 Jan 2025 10:59:51 +0330 Subject: [PATCH 11/16] Making suggested changes; --- src/Drivers/LocalDriver.php | 26 +++++++++++++------------- src/Drivers/StorageDriver.php | 21 +++++++++++---------- tests/AutoDocControllerTest.php | 7 +++---- tests/LocalDriverTest.php | 2 +- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 0a7f6e6f..9fbc6ce6 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -7,45 +7,45 @@ class LocalDriver extends BaseDriver { - protected ?string $baseFileName; + protected ?string $mainFilePath; private ?array $config; public function __construct() { parent::__construct(); + $this->config = config('auto-doc.drivers.local'); - $directory = $this->config['directory']; - if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) { - $directory .= DIRECTORY_SEPARATOR; - } + $directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR) + ? $this->config['directory'] + : $this->config['directory'] . DIRECTORY_SEPARATOR; - $this->baseFileName = storage_path($directory.$this->config['base_file_name'].'.json'); + $this->mainFilePath = storage_path("$directory{$this->config['base_file_name']}.json"); - if (!preg_match('/\/[\w]+\.json/ms', $this->baseFileName)) { + if (!preg_match('/\/[\w]+\.json/ms', $this->mainFilePath)) { throw new MissedProductionFilePathException(); } } public function saveData(): void { - $prodDir = storage_path($this->config['directory']); - if (!is_dir($prodDir)) { - mkdir($prodDir); + $documentationDirectory = storage_path($this->config['directory']); + if (!is_dir($documentationDirectory)) { + mkdir($documentationDirectory); } - file_put_contents($this->baseFileName, json_encode($this->getTmpData())); + file_put_contents($this->mainFilePath, json_encode($this->getTmpData())); $this->clearTmpData(); } public function getDocumentation(): array { - if (!file_exists($this->baseFileName)) { + if (!file_exists($this->mainFilePath)) { throw new FileNotFoundException(); } - $fileContent = file_get_contents($this->baseFileName); + $fileContent = file_get_contents($this->mainFilePath); return json_decode($fileContent, true); } diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index a6922464..12fa9c45 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -10,7 +10,7 @@ class StorageDriver extends BaseDriver { protected Filesystem $disk; - protected ?string $baseFileName; + protected ?string $mainFilePath; protected array $config; public function __construct() @@ -19,31 +19,32 @@ public function __construct() $this->config = config('auto-doc.drivers.storage'); $this->disk = Storage::disk($this->config['disk']); - $directory = $this->config['directory']; - if (!str_ends_with($directory, DIRECTORY_SEPARATOR)) { - $directory .= DIRECTORY_SEPARATOR; - } - $this->baseFileName = $directory.$this->config['base_file_name'].'.json'; - if (!preg_match('/\/[\w]+\.json/ms', $this->baseFileName)) { + $directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR) + ? $this->config['directory'] + : $this->config['directory'] . DIRECTORY_SEPARATOR; + + $this->mainFilePath = "$directory{$this->config['base_file_name']}.json"; + + if (!preg_match('/\/[\w]+\.json/ms', $this->mainFilePath)) { throw new MissedProductionFilePathException(); } } public function saveData(): void { - $this->disk->put($this->baseFileName, json_encode($this->getTmpData())); + $this->disk->put($this->mainFilePath, json_encode($this->getTmpData())); $this->clearTmpData(); } public function getDocumentation(): array { - if (!$this->disk->exists($this->baseFileName)) { + if (!$this->disk->exists($this->mainFilePath)) { throw new FileNotFoundException(); } - $fileContent = $this->disk->get($this->baseFileName); + $fileContent = $this->disk->get($this->mainFilePath); return json_decode($fileContent, true); } diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index edbd4a0c..e0819b9f 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -12,7 +12,6 @@ class AutoDocControllerTest extends TestCase use PHPMock; protected static array $documentation; - protected static string $baseFileName; protected static string $baseFile; public function setUp(): void @@ -24,16 +23,16 @@ public function setUp(): void $documentationDirectory .= DIRECTORY_SEPARATOR; } - self::$baseFileName ??= 'documentation'; - self::$baseFile ??= $documentationDirectory.self::$baseFileName.'.json'; + self::$baseFile ??= $documentationDirectory.'documentation.json'; self::$documentation ??= $this->getJsonFixture('tmp_data'); if (!is_dir(storage_path($documentationDirectory))) { mkdir(storage_path($documentationDirectory)); } + file_put_contents(storage_path(self::$baseFile), json_encode(self::$documentation)); - config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]); + config(['auto-doc.drivers.local.base_file_name' => 'documentation']); } public function tearDown(): void diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index b52397f0..d8c47bd1 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -24,7 +24,7 @@ public function setUp(): void } self::$baseFileName ??= 'documentation'; - self::$baseFile ??= storage_path($documentationDirectory.self::$baseFileName.'.json'); + self::$baseFile ??= storage_path($documentationDirectory . self::$baseFileName . '.json'); self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json'); self::$tmpData ??= $this->getJsonFixture('tmp_data'); From b665010ce02429648b13f364e443ef357aeb8f6b Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 25 Jan 2025 08:46:23 +0330 Subject: [PATCH 12/16] Apply changes; --- src/Drivers/LocalDriver.php | 2 +- tests/AutoDocControllerTest.php | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 9fbc6ce6..5e7c6fd2 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -22,7 +22,7 @@ public function __construct() $this->mainFilePath = storage_path("$directory{$this->config['base_file_name']}.json"); - if (!preg_match('/\/[\w]+\.json/ms', $this->mainFilePath)) { + if (empty($this->config['base_file_name']) || !str_ends_with($this->mainFilePath, '.json')) { throw new MissedProductionFilePathException(); } } diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index e0819b9f..2dd4c7fb 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -19,11 +19,8 @@ public function setUp(): void parent::setUp(); $documentationDirectory = config('auto-doc.drivers.local.directory'); - if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { - $documentationDirectory .= DIRECTORY_SEPARATOR; - } - self::$baseFile ??= $documentationDirectory.'documentation.json'; + self::$baseFile ??= $documentationDirectory.'/documentation.json'; self::$documentation ??= $this->getJsonFixture('tmp_data'); if (!is_dir(storage_path($documentationDirectory))) { From ebef2398cd07e4c9e3defc21577691a09dea5a70 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sun, 2 Feb 2025 21:13:58 +0330 Subject: [PATCH 13/16] Apply changes; --- src/Drivers/LocalDriver.php | 5 +++-- src/Drivers/StorageDriver.php | 6 +++--- tests/AutoDocControllerTest.php | 10 +++------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 5e7c6fd2..59d79e36 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -16,11 +16,11 @@ public function __construct() $this->config = config('auto-doc.drivers.local'); - $directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR) + $directory = (str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)) ? $this->config['directory'] : $this->config['directory'] . DIRECTORY_SEPARATOR; - $this->mainFilePath = storage_path("$directory{$this->config['base_file_name']}.json"); + $this->mainFilePath = storage_path("{$directory}{$this->config['base_file_name']}.json"); if (empty($this->config['base_file_name']) || !str_ends_with($this->mainFilePath, '.json')) { throw new MissedProductionFilePathException(); @@ -30,6 +30,7 @@ public function __construct() public function saveData(): void { $documentationDirectory = storage_path($this->config['directory']); + if (!is_dir($documentationDirectory)) { mkdir($documentationDirectory); } diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index 12fa9c45..632a5ef6 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -20,13 +20,13 @@ public function __construct() $this->config = config('auto-doc.drivers.storage'); $this->disk = Storage::disk($this->config['disk']); - $directory = str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR) + $directory = (str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)) ? $this->config['directory'] : $this->config['directory'] . DIRECTORY_SEPARATOR; - $this->mainFilePath = "$directory{$this->config['base_file_name']}.json"; + $this->mainFilePath = "{$directory}{$this->config['base_file_name']}.json"; - if (!preg_match('/\/[\w]+\.json/ms', $this->mainFilePath)) { + if (empty($this->config['base_file_name']) || !str_ends_with($this->mainFilePath, '.json')) { throw new MissedProductionFilePathException(); } } diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index 2dd4c7fb..1bfe33d5 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -12,22 +12,18 @@ class AutoDocControllerTest extends TestCase use PHPMock; protected static array $documentation; - protected static string $baseFile; public function setUp(): void { parent::setUp(); - $documentationDirectory = config('auto-doc.drivers.local.directory'); - - self::$baseFile ??= $documentationDirectory.'/documentation.json'; self::$documentation ??= $this->getJsonFixture('tmp_data'); - if (!is_dir(storage_path($documentationDirectory))) { - mkdir(storage_path($documentationDirectory)); + if (!is_dir(storage_path('documentations'))) { + mkdir(storage_path('documentations')); } - file_put_contents(storage_path(self::$baseFile), json_encode(self::$documentation)); + file_put_contents(storage_path('documentations/documentation.json'), json_encode(self::$documentation)); config(['auto-doc.drivers.local.base_file_name' => 'documentation']); } From cef046bbbae32771fba946109173a3ae47805117 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sun, 2 Feb 2025 21:20:24 +0330 Subject: [PATCH 14/16] Set empty directory in AutoDocControllerTest; Force set documentation directory as empty for AutoDocControllerTest; --- tests/AutoDocControllerTest.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index 1bfe33d5..1aed46a6 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -19,12 +19,9 @@ public function setUp(): void self::$documentation ??= $this->getJsonFixture('tmp_data'); - if (!is_dir(storage_path('documentations'))) { - mkdir(storage_path('documentations')); - } - - file_put_contents(storage_path('documentations/documentation.json'), json_encode(self::$documentation)); + file_put_contents(storage_path('documentation.json'), json_encode(self::$documentation)); + config(['auto-doc.drivers.local.directory' => '']); config(['auto-doc.drivers.local.base_file_name' => 'documentation']); } @@ -44,6 +41,21 @@ public function testGetJSONDocumentation() $response->assertJson(self::$documentation); } + public function testGetJSONDocumentationWithFilledDirectory() + { + if (!is_dir(storage_path('documentations'))) { + mkdir(storage_path('documentations')); + } + file_put_contents(storage_path('documentations/documentation.json'), json_encode(self::$documentation)); + config(['auto-doc.drivers.local.directory' => 'documentations']); + + $response = $this->json('get', '/auto-doc/documentation'); + + $response->assertStatus(Response::HTTP_OK); + + $response->assertJson(self::$documentation); + } + public function testGetJSONDocumentationWithAdditionalPaths() { config([ From 83b43b35b2377aba497e17db6456df949d30a6fd Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sun, 2 Feb 2025 22:45:03 +0330 Subject: [PATCH 15/16] Cover new lines; --- tests/LocalDriverTest.php | 27 +++++++++++++++++++++++++++ tests/StorageDriverTest.php | 11 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index d8c47bd1..79164f2f 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -34,6 +34,18 @@ public function setUp(): void self::$localDriverClass ??= new LocalDriver(); } + public function testDirectoryEndsWithDirectorySeparator() + { + config(['auto-doc.drivers.local.directory' => config('auto-doc.drivers.local.directory').DIRECTORY_SEPARATOR]); + config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]); + + $driver = new LocalDriver(); + $driver->saveTmpData(self::$tmpData); + + $this->assertFileExists(self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); + } + public function testSaveTmpData() { self::$localDriverClass->saveTmpData(self::$tmpData); @@ -86,6 +98,21 @@ public function testSaveData() $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); } + public function testSaveDataWhenDirectoryNotExists() + { + $documentationDirectory = config('auto-doc.drivers.local.directory'); + rmdir(storage_path($documentationDirectory)); + + self::$localDriverClass->saveTmpData(self::$tmpData); + + self::$localDriverClass->saveData(); + + $this->assertFileExists(self::$baseFile); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$baseFile); + + $this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); + } + public function testGetDocumentation() { file_put_contents(self::$baseFile, json_encode(self::$tmpData)); diff --git a/tests/StorageDriverTest.php b/tests/StorageDriverTest.php index 2b0983f0..aa0437ae 100755 --- a/tests/StorageDriverTest.php +++ b/tests/StorageDriverTest.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Support\Facades\Storage; +use RonasIT\AutoDoc\Drivers\LocalDriver; use RonasIT\AutoDoc\Drivers\StorageDriver; use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException; @@ -39,6 +40,16 @@ public function setUp(): void self::$storageDriverClass = new StorageDriver(); } + public function testDirectoryEndsWithDirectorySeparator() + { + config(['auto-doc.drivers.storage.directory' => config('auto-doc.drivers.storage.directory').DIRECTORY_SEPARATOR]); + + $driver = new StorageDriver(); + $driver->saveTmpData(self::$tmpData); + + $this->assertFileExists(self::$tmpDocumentationFilePath); + $this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); + } public function testSaveTmpData() { From 999f2c26deaad5f9479357279a27e7ef885fe3af Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 8 Feb 2025 20:32:13 +0330 Subject: [PATCH 16/16] Apply new changes; --- src/Drivers/LocalDriver.php | 4 ++-- src/Drivers/StorageDriver.php | 4 +--- tests/AutoDocControllerTest.php | 1 + tests/LocalDriverTest.php | 18 +++++++----------- tests/StorageDriverTest.php | 12 +++--------- 5 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/Drivers/LocalDriver.php b/src/Drivers/LocalDriver.php index 59d79e36..b5ff7de4 100755 --- a/src/Drivers/LocalDriver.php +++ b/src/Drivers/LocalDriver.php @@ -8,13 +8,13 @@ class LocalDriver extends BaseDriver { protected ?string $mainFilePath; - private ?array $config; + protected array $config; public function __construct() { parent::__construct(); - $this->config = config('auto-doc.drivers.local'); + $this->config = config('auto-doc.drivers.local', []); $directory = (str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)) ? $this->config['directory'] diff --git a/src/Drivers/StorageDriver.php b/src/Drivers/StorageDriver.php index 632a5ef6..f25df823 100755 --- a/src/Drivers/StorageDriver.php +++ b/src/Drivers/StorageDriver.php @@ -20,9 +20,7 @@ public function __construct() $this->config = config('auto-doc.drivers.storage'); $this->disk = Storage::disk($this->config['disk']); - $directory = (str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)) - ? $this->config['directory'] - : $this->config['directory'] . DIRECTORY_SEPARATOR; + $directory = $this->config['directory'] . DIRECTORY_SEPARATOR; $this->mainFilePath = "{$directory}{$this->config['base_file_name']}.json"; diff --git a/tests/AutoDocControllerTest.php b/tests/AutoDocControllerTest.php index 1aed46a6..11f9792b 100644 --- a/tests/AutoDocControllerTest.php +++ b/tests/AutoDocControllerTest.php @@ -46,6 +46,7 @@ public function testGetJSONDocumentationWithFilledDirectory() if (!is_dir(storage_path('documentations'))) { mkdir(storage_path('documentations')); } + file_put_contents(storage_path('documentations/documentation.json'), json_encode(self::$documentation)); config(['auto-doc.drivers.local.directory' => 'documentations']); diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index 79164f2f..05c4b2c0 100755 --- a/tests/LocalDriverTest.php +++ b/tests/LocalDriverTest.php @@ -9,7 +9,6 @@ class LocalDriverTest extends TestCase { protected static LocalDriver $localDriverClass; - protected static string $baseFileName; protected static string $baseFile; protected static string $tmpDocumentationFilePath; protected static array $tmpData; @@ -19,25 +18,20 @@ public function setUp(): void parent::setUp(); $documentationDirectory = config('auto-doc.drivers.local.directory'); - if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { - $documentationDirectory .= DIRECTORY_SEPARATOR; - } - self::$baseFileName ??= 'documentation'; - self::$baseFile ??= storage_path($documentationDirectory . self::$baseFileName . '.json'); + self::$baseFile ??= storage_path("{$documentationDirectory}/documentation.json"); self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json'); self::$tmpData ??= $this->getJsonFixture('tmp_data'); - config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]); + config(['auto-doc.drivers.local.base_file_name' => 'documentation']); self::$localDriverClass ??= new LocalDriver(); } public function testDirectoryEndsWithDirectorySeparator() { - config(['auto-doc.drivers.local.directory' => config('auto-doc.drivers.local.directory').DIRECTORY_SEPARATOR]); - config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]); + config(['auto-doc.drivers.local.directory' => 'documentations'.DIRECTORY_SEPARATOR]); $driver = new LocalDriver(); $driver->saveTmpData(self::$tmpData); @@ -100,8 +94,10 @@ public function testSaveData() public function testSaveDataWhenDirectoryNotExists() { - $documentationDirectory = config('auto-doc.drivers.local.directory'); - rmdir(storage_path($documentationDirectory)); + $documentationDirectory = 'test_directory'; + if (is_dir($documentationDirectory)) { + rmdir(storage_path($documentationDirectory)); + } self::$localDriverClass->saveTmpData(self::$tmpData); diff --git a/tests/StorageDriverTest.php b/tests/StorageDriverTest.php index aa0437ae..4304e84e 100755 --- a/tests/StorageDriverTest.php +++ b/tests/StorageDriverTest.php @@ -5,7 +5,6 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Support\Facades\Storage; -use RonasIT\AutoDoc\Drivers\LocalDriver; use RonasIT\AutoDoc\Drivers\StorageDriver; use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException; @@ -13,7 +12,6 @@ class StorageDriverTest extends TestCase { protected static StorageDriver $storageDriverClass; protected Filesystem $disk; - protected static string $baseFileName; protected static string $baseFile; protected static string $tmpDocumentationFilePath; protected static array $tmpData; @@ -25,24 +23,20 @@ public function setUp(): void $this->disk = Storage::fake('testing'); $documentationDirectory = config('auto-doc.drivers.storage.directory'); - if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { - $documentationDirectory .= DIRECTORY_SEPARATOR; - } - self::$baseFileName ??= 'documentation'; - self::$baseFile ??= $documentationDirectory.self::$baseFileName.'.json'; + self::$baseFile ??= "{$documentationDirectory}/documentation.json"; self::$tmpDocumentationFilePath ??= storage_path('temp_documentation.json'); self::$tmpData ??= $this->getJsonFixture('tmp_data'); config(['auto-doc.drivers.storage.disk' => 'testing']); - config(['auto-doc.drivers.storage.base_file_name' => self::$baseFileName]); + config(['auto-doc.drivers.storage.base_file_name' => 'documentation']); self::$storageDriverClass = new StorageDriver(); } public function testDirectoryEndsWithDirectorySeparator() { - config(['auto-doc.drivers.storage.directory' => config('auto-doc.drivers.storage.directory').DIRECTORY_SEPARATOR]); + config(['auto-doc.drivers.storage.directory' => 'documentations'.DIRECTORY_SEPARATOR]); $driver = new StorageDriver(); $driver->saveTmpData(self::$tmpData);