Skip to content

Commit

Permalink
Merge 11a1d58 into 7187aa0
Browse files Browse the repository at this point in the history
  • Loading branch information
vitgrams committed Mar 9, 2023
2 parents 7187aa0 + 11a1d58 commit 6f895ae
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 51 deletions.
28 changes: 17 additions & 11 deletions src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,47 @@
namespace RonasIT\Support\AutoDoc\Drivers;

use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;

class LocalDriver implements SwaggerDriverInterface
{
public $prodFilePath;

protected static $data;
protected $prodFilePath;
protected $tempFilePath;

public function __construct()
{
$this->prodFilePath = config('auto-doc.drivers.local.production_path');
$this->tempFilePath = storage_path('temp_documentation.json');

if (empty($this->prodFilePath)) {
throw new MissedProductionFilePathException();
}
}

public function saveTmpData($tempData)
public function saveTmpData($data)
{
self::$data = $tempData;
file_put_contents($this->tempFilePath, json_encode($data));
}

public function getTmpData()
{
return self::$data;
if (file_exists($this->tempFilePath)) {
$content = file_get_contents($this->tempFilePath);

return json_decode($content, true);
}

return null;
}

public function saveData()
{
$content = json_encode(self::$data);
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));

file_put_contents($this->prodFilePath, $content);

self::$data = [];
if (file_exists($this->tempFilePath)) {
unlink($this->tempFilePath);
}
}

public function getDocumentation(): array
Expand Down
14 changes: 7 additions & 7 deletions src/Drivers/RemoteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ class RemoteDriver implements SwaggerDriverInterface
{
protected $key;
protected $remoteUrl;
protected $tempFileName;
protected $tempFilePath;

public function __construct()
{
$this->key = config('auto-doc.drivers.remote.key');
$this->remoteUrl = config('auto-doc.drivers.remote.url');
$this->tempFileName = storage_path('temp_documentation.json');
$this->tempFilePath = storage_path('temp_documentation.json');
}

public function saveTmpData($data)
{
file_put_contents($this->tempFileName, json_encode($data));
file_put_contents($this->tempFilePath, json_encode($data));
}

public function getTmpData()
{
if (file_exists($this->tempFileName)) {
$content = file_get_contents($this->tempFileName);
if (file_exists($this->tempFilePath)) {
$content = file_get_contents($this->tempFilePath);

return json_decode($content, true);
}
Expand All @@ -40,8 +40,8 @@ public function saveData()
'Content-Type: application/json'
]);

if (file_exists($this->tempFileName)) {
unlink($this->tempFileName);
if (file_exists($this->tempFilePath)) {
unlink($this->tempFilePath);
}
}

Expand Down
41 changes: 26 additions & 15 deletions src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,60 @@

namespace RonasIT\Support\AutoDoc\Drivers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;
use RonasIT\Support\AutoDoc\Interfaces\SwaggerDriverInterface;

class StorageDriver implements SwaggerDriverInterface
{
protected $disk;
protected $filePath;

protected static $data;
protected $prodFilePath;
protected $tempFilePath;

public function __construct()
{
$this->disk = config('auto-doc.drivers.storage.disk');
$this->filePath = config('auto-doc.drivers.storage.production_path');
$this->disk = Storage::disk(config('auto-doc.drivers.storage.disk'));
$this->prodFilePath = config('auto-doc.drivers.storage.production_path');
$this->tempFilePath = storage_path('temp_documentation.json');

if (empty($this->prodFilePath)) {
throw new MissedProductionFilePathException();
}
}

public function saveTmpData($tempData)
public function saveTmpData($data)
{
self::$data = $tempData;
file_put_contents($this->tempFilePath, json_encode($data));
}

public function getTmpData()
{
return self::$data;
if (file_exists($this->tempFilePath)) {
$content = file_get_contents($this->tempFilePath);

return json_decode($content, true);
}

return null;
}

public function saveData()
{
$content = json_encode(self::$data);
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));

Storage::disk($this->disk)->put($this->filePath, $content);

self::$data = [];
if ($this->disk->exists($this->tempFilePath)) {
$this->disk->delete($this->tempFilePath);
}
}

public function getDocumentation(): array
{
if (!Storage::disk($this->disk)->exists($this->filePath)) {
if (!$this->disk->exists($this->prodFilePath)) {
throw new FileNotFoundException();
}

$fileContent = Storage::disk($this->disk)->get($this->filePath);
$fileContent = $this->disk->get($this->prodFilePath);

return json_decode($fileContent, true);
}
Expand Down
37 changes: 32 additions & 5 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,49 @@

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

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

$this->tmpData = $this->getJsonFixture('tmp_data');
$this->productionFilePath = __DIR__ . '/../storage/documentation.json';
$this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json';

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

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

$this->localDriverClass = new LocalDriver();
}

public function testSaveTmpData()
{
$this->localDriverClass->saveTmpData($this->tmpData);

$this->assertFileExists($this->tmpDocumentationFilePath);
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), $this->tmpDocumentationFilePath);
}

public function testGetTmpData()
{
file_put_contents($this->tmpDocumentationFilePath, json_encode($this->tmpData));

$result = $this->localDriverClass->getTmpData();

$this->assertEquals($this->tmpData, $result);
}

public function testGetTmpDataNoFile()
{
$result = $this->localDriverClass->getTmpData();

$this->assertNull($result);
}

public function testCreateClassConfigEmpty()
{
$this->expectException(MissedProductionFilePathException::class);
Expand All @@ -37,19 +64,19 @@ public function testGetAndSaveTmpData()
{
$this->localDriverClass->saveTmpData($this->tmpData);

$this->assertEquals($this->tmpData, $this->localDriverClass->getTmpData());
$this->assertEqualsJsonFixture('tmp_data', $this->localDriverClass->getTmpData());
}

public function testSaveData()
{
$this->localDriverClass->saveTmpData($this->tmpData);
file_put_contents($this->tmpDocumentationFilePath, json_encode($this->tmpData));

$this->localDriverClass->saveData();

$this->assertFileExists($this->productionFilePath);
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), $this->productionFilePath);

$this->assertEquals([], $this->localDriverClass->getTmpData());
$this->assertFileDoesNotExist($this->tmpDocumentationFilePath);
}

public function testGetDocumentation()
Expand Down
61 changes: 49 additions & 12 deletions tests/StorageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,88 @@
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Support\Facades\Storage;
use RonasIT\Support\AutoDoc\Drivers\StorageDriver;
use RonasIT\Support\AutoDoc\Exceptions\MissedProductionFilePathException;

class StorageDriverTest extends TestCase
{
protected $storageDriverClass;
protected $disk;
protected $productionFilePath;
protected $tmpDocumentationFilePath;
protected $tmpData;
protected $filePath;
protected $storageDriverClass;

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

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

$this->productionFilePath = 'documentation.json';
$this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json';

$this->tmpData = $this->getJsonFixture('tmp_data');
$this->filePath = __DIR__ . '/storage/documentation.json';

config(['auto-doc.drivers.storage.disk' => 'testing']);
config(['auto-doc.drivers.storage.production_path' => $this->filePath]);

$this->disk = Storage::fake('testing');
config(['auto-doc.drivers.storage.production_path' => $this->productionFilePath]);

$this->storageDriverClass = new StorageDriver();
}

public function testSaveTmpData()
{
$this->storageDriverClass->saveTmpData($this->tmpData);

$this->assertFileExists($this->tmpDocumentationFilePath);
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), $this->tmpDocumentationFilePath);
}

public function testGetTmpData()
{
file_put_contents($this->tmpDocumentationFilePath, json_encode($this->tmpData));

$result = $this->storageDriverClass->getTmpData();

$this->assertEquals($this->tmpData, $result);
}

public function testGetTmpDataNoFile()
{
$result = $this->storageDriverClass->getTmpData();

$this->assertNull($result);
}

public function testCreateClassConfigEmpty()
{
$this->expectException(MissedProductionFilePathException::class);

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

new StorageDriver();
}

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

$this->assertEquals($this->tmpData, $this->storageDriverClass->getTmpData());
$this->assertEqualsJsonFixture('tmp_data', $this->storageDriverClass->getTmpData());
}

public function testSaveData()
{
$this->storageDriverClass->saveTmpData($this->tmpData);
file_put_contents($this->tmpDocumentationFilePath, json_encode($this->tmpData));

$this->storageDriverClass->saveData();

$this->disk->assertExists($this->filePath);
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get($this->filePath));
$this->disk->assertExists($this->productionFilePath);
$this->assertEqualsFixture('tmp_data_non_formatted.json', $this->disk->get($this->productionFilePath));

$this->assertEquals([], $this->storageDriverClass->getTmpData());
$this->disk->assertMissing($this->tmpDocumentationFilePath);
}

public function testGetDocumentation()
{
$this->disk->put($this->filePath, $this->getFixture('tmp_data_non_formatted.json'));
$this->disk->put($this->productionFilePath, $this->getFixture('tmp_data_non_formatted.json'));

$documentation = $this->storageDriverClass->getDocumentation();

Expand Down
1 change: 0 additions & 1 deletion tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use RonasIT\Support\AutoDoc\Services\SwaggerService;
use RonasIT\Support\Tests\Support\Mock\TestNotificationSetting;
use RonasIT\Support\Tests\Support\Traits\SwaggerServiceMockTrait;
use Symfony\Component\HttpFoundation\Response;

class SwaggerServiceTest extends TestCase
{
Expand Down

0 comments on commit 6f895ae

Please sign in to comment.