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: Store documentation file on a configurable directory #152

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
*/
'route' => '',

/*
|--------------------------------------------------------------------------
| Documentation Directory
|--------------------------------------------------------------------------
|
| Documentation file will be stored in the determined directory
*/
'documentation_directory' => storage_path('documentations'),

/*
|--------------------------------------------------------------------------
| Global application prefix
Expand Down Expand Up @@ -128,7 +137,7 @@
'drivers' => [
'local' => [
'class' => LocalDriver::class,
'production_path' => storage_path('documentation.json'),
'production_path' => 'documentation.json',
],
'remote' => [
'class' => RemoteDriver::class,
Expand Down Expand Up @@ -184,5 +193,5 @@
'development',
],

'config_version' => '2.8',
'config_version' => '2.9',
];
6 changes: 5 additions & 1 deletion src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
9 changes: 7 additions & 2 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ 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));
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]);
}
Expand Down
24 changes: 13 additions & 11 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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();
}
Expand Down
14 changes: 8 additions & 6 deletions tests/RemoteDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ class RemoteDriverTest extends TestCase

protected static array $tmpData;
protected static RemoteDriver $remoteDriverClass;
protected static string $documentationDirectory;
protected static string $tmpDocumentationFilePath;

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();
}
Expand All @@ -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();

Expand Down Expand Up @@ -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()
Expand Down
14 changes: 8 additions & 6 deletions tests/StorageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');

Expand All @@ -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();

Expand Down Expand Up @@ -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()
Expand Down
Loading