Skip to content
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

feat: Increase config version add new config key for documentation saving and check directory existing #127

Closed
wants to merge 7 commits into from
8 changes: 5 additions & 3 deletions config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@
'drivers' => [
'local' => [
'class' => LocalDriver::class,
'production_path' => storage_path('documentation.json')
'file_path' => env('SWAGGER_DOCUMENTATION_PATH', 'documentation'),
'file_name' => '/documentation.json',
],
'remote' => [
'class' => RemoteDriver::class,
Expand All @@ -146,7 +147,8 @@
| One of the filesystems.disks config value
*/
'disk' => env('SWAGGER_STORAGE_DRIVER_DISK', 'public'),
'production_path' => 'documentation.json'
'file_name' => '/documentation.json',
'file_path' => env('SWAGGER_DOCUMENTATION_PATH', 'documentation'),
]
],

Expand Down Expand Up @@ -184,5 +186,5 @@
'development'
],

'config_version' => '2.7'
'config_version' => '2.8'
];
38 changes: 35 additions & 3 deletions src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,39 @@ public function __construct()
{
parent::__construct();

$this->prodFilePath = config('auto-doc.drivers.local.production_path');
$this->prodFilePath = storage_path(config('auto-doc.drivers.local.file_path').config('auto-doc.drivers.local.file_name'));

if (empty($this->prodFilePath)) {
if (empty(config('auto-doc.drivers.local.file_path'))
||
empty(config('auto-doc.drivers.local.file_name'))
||
empty($this->prodFilePath)
) {
throw new MissedProductionFilePathException();
}

if (!is_dir(dirname($this->prodFilePath))) {
mkdir(dirname($this->prodFilePath), 0777, true);
}
}

public function saveData(): void
{
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
$currentDocumentation = [];
$newData = $this->getTmpData();

if (file_exists($this->prodFilePath)) {
$currentDocumentation = $this->getDocumentation();
}

if (!empty($currentDocumentation) && $currentDocumentation !== $newData) {
$version = $this->getNextVersion();
$newFileName = str_replace('documentation.json', "documentation_$version.json", $this->prodFilePath);

copy($this->prodFilePath, $newFileName);
}

file_put_contents($this->prodFilePath, json_encode($newData));

$this->clearTmpData();
}
Expand All @@ -37,4 +60,13 @@ public function getDocumentation(): array

return json_decode($fileContent, true);
}

protected function getNextVersion(): string
{
$currentVersion = config('auto-doc.config_version', '0.1');

[$major, $minor] = explode('.', $currentVersion);

return "{$major}_{$minor}";
}
}
15 changes: 12 additions & 3 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ public function __construct()
parent::__construct();

$this->disk = Storage::disk(config('auto-doc.drivers.storage.disk'));
$this->prodFilePath = config('auto-doc.drivers.storage.production_path');

if (empty($this->prodFilePath)) {
$this->prodFilePath = config('auto-doc.drivers.local.file_path').config('auto-doc.drivers.local.file_name');

if (empty(config('auto-doc.drivers.local.file_path'))
||
empty(config('auto-doc.drivers.local.file_name'))
||
empty($this->prodFilePath)
) {
throw new MissedProductionFilePathException();
}

if (!Storage::exists(dirname($this->prodFilePath))) {
Storage::makeDirectory(dirname($this->prodFilePath));
}
}

public function saveData(): void
Expand Down
12 changes: 10 additions & 2 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RonasIT\Support\Tests;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use phpmock\phpunit\PHPMock;
use RonasIT\Support\Tests\Support\Traits\MockTrait;

Expand All @@ -13,17 +14,24 @@ class AutoDocControllerTest extends TestCase

protected $documentation;
protected $localDriverFilePath;
protected $fileName;
protected $filePath;

public function setUp(): void
{
parent::setUp();

$this->localDriverFilePath = __DIR__ . '/../storage/documentation.json';
$this->filePath = Storage::path('storage');
$this->fileName = '/documentation.json';
$this->localDriverFilePath = storage_path($this->filePath.$this->fileName);
$this->documentation = $this->getJsonFixture('tmp_data');

file_put_contents($this->localDriverFilePath, json_encode($this->documentation));

config(['auto-doc.drivers.local.production_path' => $this->localDriverFilePath]);
config([
'auto-doc.drivers.local.file_name' => $this->fileName,
'auto-doc.drivers.local.file_path' => $this->filePath,
]);
}

public function tearDown(): void
Expand Down
41 changes: 35 additions & 6 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@

use RonasIT\Support\AutoDoc\Drivers\LocalDriver;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;

class LocalDriverTest extends TestCase
{
protected $localDriverClass;
protected $productionFilePath;
protected $filePath;
protected $fileName;
protected $tmpDocumentationFilePath;
protected $tmpData;

public function setUp(): void
{
parent::setUp();

$this->productionFilePath = __DIR__ . '/../storage/documentation.json';
$this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json';
$this->filePath = Storage::path('storage');
$this->fileName = '/documentation.json';
$this->productionFilePath = storage_path($this->filePath.$this->fileName);
$this->tmpDocumentationFilePath = storage_path('temp_documentation.json');

$this->tmpData = $this->getJsonFixture('tmp_data');

config(['auto-doc.drivers.local.production_path' => $this->productionFilePath]);
config([
'auto-doc.drivers.local.file_name' => $this->fileName,
'auto-doc.drivers.local.file_path' => $this->filePath,
]);

$this->localDriverClass = new LocalDriver();
}
Expand Down Expand Up @@ -55,11 +63,29 @@ public function testCreateClassConfigEmpty()
{
$this->expectException(MissedProductionFilePathException::class);

config(['auto-doc.drivers.local.production_path' => null]);

config([
'auto-doc.drivers.local.file_name' => null,
'auto-doc.drivers.local.file_path' => null,
]);
new LocalDriver();
}

public function testCreateDirectoryIfNotExists()
{
$productionPath = 'non_existent_directory/documentation.json';

Storage::makeDirectory($productionPath);

config([
'auto-doc.drivers.local.file_name' => '/documentation.json',
'auto-doc.drivers.local.file_path' => 'non_existent_directory',
]);

$this->assertTrue(Storage::exists($productionPath));

Storage::delete($productionPath);
}

public function testGetAndSaveTmpData()
{
$this->localDriverClass->saveTmpData($this->tmpData);
Expand Down Expand Up @@ -92,7 +118,10 @@ public function testGetDocumentationFileNotExists()
{
$this->expectException(FileNotFoundException::class);

config(['auto-doc.drivers.local.production_path' => 'not_exists_file']);
config([
'auto-doc.drivers.local.file_name' => 'not_exists_file',
'auto-doc.drivers.local.file_path' => 'not_exists_file',
]);

(new LocalDriver())->getDocumentation();
}
Expand Down
34 changes: 28 additions & 6 deletions tests/StorageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class StorageDriverTest extends TestCase
protected $storageDriverClass;
protected $disk;
protected $productionFilePath;
protected $fileName;
protected $filePath;
protected $tmpDocumentationFilePath;
protected $tmpData;

Expand All @@ -20,14 +22,18 @@ public function setUp(): void
parent::setUp();

$this->disk = Storage::fake('testing');

$this->productionFilePath = 'documentation.json';
$this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json';
$this->filePath = Storage::path('storage');
$this->fileName = '/documentation.json';
$this->productionFilePath = $this->filePath.$this->fileName;
$this->tmpDocumentationFilePath = storage_path('temp_documentation.json');

$this->tmpData = $this->getJsonFixture('tmp_data');

config(['auto-doc.drivers.storage.disk' => 'testing']);
config(['auto-doc.drivers.storage.production_path' => $this->productionFilePath]);
config([
'auto-doc.drivers.local.file_name' => $this->fileName,
'auto-doc.drivers.local.file_path' => $this->filePath,
]);

$this->storageDriverClass = new StorageDriver();
}
Expand Down Expand Up @@ -60,11 +66,27 @@ public function testCreateClassConfigEmpty()
{
$this->expectException(MissedProductionFilePathException::class);

config(['auto-doc.drivers.storage.production_path' => null]);

config([
'auto-doc.drivers.local.file_name' => null,
'auto-doc.drivers.local.file_path' => null,
]);
new StorageDriver();
}

public function testCreateDirectoryIfNotExists()
{
config([
'auto-doc.drivers.local.file_name' => '/documentation.json',
'auto-doc.drivers.local.file_path' => 'non_existent_directory',
]);

Storage::disk('testing')->makeDirectory('non_existent_directory');

$this->assertTrue(Storage::disk('testing')->exists('non_existent_directory'));

Storage::disk('testing')->deleteDirectory('non_existent_directory');
}

public function testGetAndSaveTmpData()
{
$this->storageDriverClass->saveTmpData($this->tmpData);
Expand Down
Loading