From 748d42464db64d618bb2df71ad91d2fa5c65f6a1 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Fri, 13 Dec 2024 13:05:54 +0330 Subject: [PATCH 01/10] 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 1ed5fe9..74fd582 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/10] 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 0bef982..def27e3 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 f8b7f27..3a3a1a2 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/10] 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 daaf8ca..ea4b750 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 d96a7db..b5c6173 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 70a8097..2353b7c 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 a30da60..9b1533e 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/10] 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 74fd582..44c432a 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/10] 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 ea4b750..fc455a2 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/10] 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 44c432a..eae0005 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/10] 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 eae0005..04fea7f 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/10] 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 def27e3..0bef982 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 3a3a1a2..0a7f6e6 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 e08646d..a692246 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/10] 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 fc455a2..edbd4a0 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 b5c6173..ec73541 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 2353b7c..62fb31d 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 9b1533e..2b0983f 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/10] Wip; --- tests/LocalDriverTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/LocalDriverTest.php b/tests/LocalDriverTest.php index ec73541..b52397f 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; }