-
Notifications
You must be signed in to change notification settings - Fork 46
feat: Store documentation file on a configurable directory #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 15 commits
748d424
cc6d056
4e1970f
7d08de0
a97158e
10705ea
8d25c0f
db649e9
9dd4951
0fa0b4b
4a387c2
b665010
ebef239
cef046b
83b43b3
999f2c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,34 +10,41 @@ | |
class StorageDriver extends BaseDriver | ||
{ | ||
protected Filesystem $disk; | ||
protected ?string $prodFilePath; | ||
protected ?string $mainFilePath; | ||
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']); | ||
|
||
if (empty($this->prodFilePath)) { | ||
$directory = (str_ends_with($this->config['directory'], DIRECTORY_SEPARATOR)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this logic required for the Storage driver as it works correctly for all next cases:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That logic was preventing this case: Storage::put('documentations//documentation.json', 'content') However, it's handling this case correctly, too. |
||
? $this->config['directory'] | ||
: $this->config['directory'] . DIRECTORY_SEPARATOR; | ||
|
||
$this->mainFilePath = "{$directory}{$this->config['base_file_name']}.json"; | ||
|
||
if (empty($this->config['base_file_name']) || !str_ends_with($this->mainFilePath, '.json')) { | ||
throw new MissedProductionFilePathException(); | ||
} | ||
} | ||
|
||
public function saveData(): void | ||
{ | ||
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData())); | ||
$this->disk->put($this->mainFilePath, json_encode($this->getTmpData())); | ||
|
||
$this->clearTmpData(); | ||
} | ||
|
||
public function getDocumentation(): array | ||
{ | ||
if (!$this->disk->exists($this->prodFilePath)) { | ||
if (!$this->disk->exists($this->mainFilePath)) { | ||
throw new FileNotFoundException(); | ||
} | ||
|
||
$fileContent = $this->disk->get($this->prodFilePath); | ||
$fileContent = $this->disk->get($this->mainFilePath); | ||
|
||
return json_decode($fileContent, true); | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,24 +9,43 @@ | |||||
class LocalDriverTest extends TestCase | ||||||
{ | ||||||
protected static LocalDriver $localDriverClass; | ||||||
protected static string $productionFilePath; | ||||||
protected static string $baseFileName; | ||||||
protected static string $baseFile; | ||||||
protected static string $tmpDocumentationFilePath; | ||||||
protected static array $tmpData; | ||||||
|
||||||
public function setUp(): void | ||||||
{ | ||||||
parent::setUp(); | ||||||
|
||||||
self::$productionFilePath ??= __DIR__ . '/../storage/documentation.json'; | ||||||
self::$tmpDocumentationFilePath ??= __DIR__ . '/../storage/temp_documentation.json'; | ||||||
$documentationDirectory = config('auto-doc.drivers.local.directory'); | ||||||
if (!str_ends_with($documentationDirectory, DIRECTORY_SEPARATOR)) { | ||||||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
$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(); | ||||||
} | ||||||
|
||||||
public function testDirectoryEndsWithDirectorySeparator() | ||||||
{ | ||||||
config(['auto-doc.drivers.local.directory' => config('auto-doc.drivers.local.directory').DIRECTORY_SEPARATOR]); | ||||||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
config(['auto-doc.drivers.local.base_file_name' => self::$baseFileName]); | ||||||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
$driver = new LocalDriver(); | ||||||
$driver->saveTmpData(self::$tmpData); | ||||||
|
||||||
$this->assertFileExists(self::$tmpDocumentationFilePath); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These static properties are used several times in this class and I believe they are beneficial to keep tests simpler and improve the readability. |
||||||
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath); | ||||||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
public function testSaveTmpData() | ||||||
{ | ||||||
self::$localDriverClass->saveTmpData(self::$tmpData); | ||||||
|
@@ -55,7 +74,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(); | ||||||
} | ||||||
|
@@ -73,15 +92,30 @@ public function testSaveData() | |||||
|
||||||
self::$localDriverClass->saveData(); | ||||||
|
||||||
$this->assertFileExists(self::$productionFilePath); | ||||||
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$productionFilePath); | ||||||
$this->assertFileExists(self::$baseFile); | ||||||
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$baseFile); | ||||||
|
||||||
$this->assertFileDoesNotExist(self::$tmpDocumentationFilePath); | ||||||
} | ||||||
|
||||||
public function testSaveDataWhenDirectoryNotExists() | ||||||
{ | ||||||
$documentationDirectory = config('auto-doc.drivers.local.directory'); | ||||||
hans-thomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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::$productionFilePath, json_encode(self::$tmpData)); | ||||||
file_put_contents(self::$baseFile, json_encode(self::$tmpData)); | ||||||
|
||||||
$documentation = self::$localDriverClass->getDocumentation(); | ||||||
|
||||||
|
@@ -92,7 +126,7 @@ public function testGetDocumentationFileNotExists() | |||||
{ | ||||||
$this->expectException(FileNotFoundException::class); | ||||||
|
||||||
config(['auto-doc.drivers.local.production_path' => 'not_exists_file']); | ||||||
config(['auto-doc.drivers.local.base_file_name' => 'not_exists_file.json']); | ||||||
|
||||||
(new LocalDriver())->getDocumentation(); | ||||||
} | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.