Skip to content

Commit

Permalink
Fix tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed Dec 13, 2024
1 parent cc6d056 commit 4e1970f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
6 changes: 4 additions & 2 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
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

0 comments on commit 4e1970f

Please sign in to comment.