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: set up project to use Paratest #136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
18 changes: 14 additions & 4 deletions src/Contracts/SwaggerDriverContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
interface SwaggerDriverContract
{
/**
* Save temporary data
* Save current temporary data
*
* @param array $data
*/
public function saveTmpData($data);
public function saveTmpData(array $data): void;

/**
* Get temporary data
* Get current temporary data
*/
public function getTmpData();
public function getTmpData(): ?array;

/**
* Save shared (result) temporary data
*/
public function saveSharedTmpData(callable $callback): void;

/**
* Get shared (result) temporary data
*/
public function getSharedTmpData(): ?array;

/**
* Save production data
Expand Down
116 changes: 110 additions & 6 deletions src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,59 @@

namespace RonasIT\AutoDoc\Drivers;

use Illuminate\Support\Facades\ParallelTesting;
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract;
use RuntimeException;

abstract class BaseDriver implements SwaggerDriverContract
{
protected string $tempFilePath;
protected string $sharedTempFilePath;

public function __construct()
{
$this->tempFilePath = storage_path('temp_documentation.json');
$this->sharedTempFilePath = storage_path('temp_documentation.json');

$this->tempFilePath = ($token = ParallelTesting::token())
? storage_path("temp_documentation_{$token}.json")
: $this->sharedTempFilePath;
}

public function saveTmpData($data): void
public function saveTmpData(array $data): void
{
file_put_contents($this->tempFilePath, json_encode($data));
$this->saveJsonToFile($this->tempFilePath, $data);
}

public function getTmpData(): ?array
{
if (file_exists($this->tempFilePath)) {
$content = file_get_contents($this->tempFilePath);
return $this->getJsonFromFile($this->tempFilePath);
}

return json_decode($content, true);
public function saveSharedTmpData(callable $callback): void
{
$this->handleFileWithLock(
filePath: $this->sharedTempFilePath,
mode: 'c+',
operation: LOCK_EX | LOCK_NB,
callback: function ($handle) use ($callback) {
$data = $callback($this->readJsonFromStream($handle));

$this->writeJsonToStream($handle, $data);
},
vitgrams marked this conversation as resolved.
Show resolved Hide resolved
);
}

public function getSharedTmpData(): ?array
{
if (file_exists($this->sharedTempFilePath)) {
return $this->handleFileWithLock(
filePath: $this->sharedTempFilePath,
mode: 'r',
operation: LOCK_SH,
callback: function ($handle) {
return $this->readJsonFromStream($handle);
},
);
}

return null;
Expand All @@ -35,4 +66,77 @@ protected function clearTmpData(): void
unlink($this->tempFilePath);
}
}

protected function saveJsonToFile(string $filePath, array $data): void
{
file_put_contents($filePath, json_encode($data));
}

protected function getJsonFromFile(string $filePath): ?array
{
if (file_exists($filePath)) {
$content = file_get_contents($filePath);

return json_decode($content, true);
}

return null;
}

protected function handleFileWithLock(
string $filePath,
string $mode,
int $operation,
callable $callback,
): mixed
{
$handle = fopen($filePath, $mode);

try {
$this->acquireLock($handle, $operation);

return $callback($handle);
} finally {
flock($handle, LOCK_UN);
fclose($handle);
}
}

protected function writeJsonToStream($handle, array $data): void
{
ftruncate($handle, 0);
rewind($handle);
fwrite($handle, json_encode($data));
fflush($handle);
}

protected function readJsonFromStream($handle): ?array
{
$content = stream_get_contents($handle);

return ($content === false) ? null : json_decode($content, true);
}

/**
* @codeCoverageIgnore
*/
protected function acquireLock(
$handle,
int $operation,
int $maxRetries = 20,
int $minWaitTime = 100,
int $maxWaitTime = 1000,
): void {
$retryCounter = 0;

while (!flock($handle, $operation)) {
if ($retryCounter >= $maxRetries) {
throw new RuntimeException('Unable to lock file');
}

usleep(rand($minWaitTime, $maxWaitTime));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
usleep(rand($minWaitTime, $maxWaitTime));
usleep($waitTime);


$retryCounter++;
}
}
}
2 changes: 1 addition & 1 deletion src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct()

public function saveData(): void
{
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));
file_put_contents($this->prodFilePath, json_encode($this->getSharedTmpData()));

$this->clearTmpData();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/RemoteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct()

public function saveData(): void
{
$this->makeHttpRequest('post', $this->getUrl(), $this->getTmpData(), [
$this->makeHttpRequest('post', $this->getUrl(), $this->getSharedTmpData(), [
'Content-Type: application/json',
]);

Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct()

public function saveData(): void
{
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));
$this->disk->put($this->prodFilePath, json_encode($this->getSharedTmpData()));

$this->clearTmpData();
}
Expand Down
13 changes: 12 additions & 1 deletion src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use ReflectionClass;
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract;
use RonasIT\AutoDoc\Exceptions\DocFileNotExistsException;
use RonasIT\AutoDoc\Exceptions\EmptyContactEmailException;
use RonasIT\AutoDoc\Exceptions\EmptyDocFileException;
Expand All @@ -17,7 +18,6 @@
use RonasIT\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
use RonasIT\AutoDoc\Exceptions\UnsupportedDocumentationViewerException;
use RonasIT\AutoDoc\Exceptions\WrongSecurityConfigException;
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract;
use RonasIT\AutoDoc\Traits\GetDependenciesTrait;
use RonasIT\AutoDoc\Validators\SwaggerSpecValidator;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -1002,4 +1002,15 @@ protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocu
);
}
}

public function mergeTempDocumentation(): void
{
$this->driver->saveSharedTmpData(function ($sharedTmpData) {
$resultDocContent = $sharedTmpData ?? $this->generateEmptyData();

$this->mergeOpenAPIDocs($resultDocContent, $this->data);

return $resultDocContent;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace RonasIT\AutoDoc\Support\PHPUnit\EventSubscribers;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\ParallelTesting;
use PHPUnit\Event\Application\Finished;
use PHPUnit\Event\Application\FinishedSubscriber;
use RonasIT\AutoDoc\Services\SwaggerService;
Expand All @@ -13,12 +15,18 @@ public function notify(Finished $event): void
{
$this->createApplication();

app(SwaggerService::class)->saveProductionData();
$swaggerService = app(SwaggerService::class);

if (ParallelTesting::token()) {
$swaggerService->mergeTempDocumentation();
}
Comment on lines +20 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move it into the saveProductionData


$swaggerService->saveProductionData();
}

protected function createApplication(): void
{
$app = require base_path('bootstrap/app.php');
$app = require Application::inferBasePath() . '/bootstrap/app.php';

$app->loadEnvironmentFrom('.env.testing');
$app->make(Kernel::class)->bootstrap();
Expand Down
39 changes: 39 additions & 0 deletions tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RonasIT\AutoDoc\Tests;

use Illuminate\Support\Facades\ParallelTesting;
use RonasIT\AutoDoc\Drivers\LocalDriver;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use RonasIT\AutoDoc\Exceptions\MissedProductionFilePathException;
Expand Down Expand Up @@ -35,6 +36,28 @@ public function testSaveTmpData()
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath);
}

public function testSaveTmpDataCheckTokenBasedPath()
{
$token = 'workerID';

ParallelTesting::resolveTokenUsing(fn () => $token);

$tmpDocPath = __DIR__ . "/../storage/temp_documentation_{$token}.json";

app(LocalDriver::class)->saveTmpData(self::$tmpData);

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

public function testSaveSharedTmpData()
{
self::$localDriverClass->saveSharedTmpData(fn () => self::$tmpData);

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

public function testGetTmpData()
{
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));
Expand All @@ -51,6 +74,22 @@ public function testGetTmpDataNoFile()
$this->assertNull($result);
}

public function testGetSharedTmpData()
{
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));

$result = self::$localDriverClass->getSharedTmpData();

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

public function testGetSharedTmpDataNoFile()
{
$result = self::$localDriverClass->getSharedTmpData();

$this->assertNull($result);
}

public function testCreateClassConfigEmpty()
{
$this->expectException(MissedProductionFilePathException::class);
Expand Down
24 changes: 24 additions & 0 deletions tests/RemoteDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public function testSaveTmpData()
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath);
}

public function testSaveSharedTmpData()
{
self::$remoteDriverClass->saveSharedTmpData(fn () => self::$tmpData);

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

public function testGetTmpData()
{
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));
Expand All @@ -49,6 +57,22 @@ public function testGetTmpDataNoFile()
$this->assertNull($result);
}

public function testGetSharedTmpData()
{
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));

$result = self::$remoteDriverClass->getSharedTmpData();

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

public function testGetSharedTmpDataNoFile()
{
$result = self::$remoteDriverClass->getSharedTmpData();

$this->assertNull($result);
}

public function testCreateClassConfigEmpty()
{
$this->expectException(MissedRemoteDocumentationUrlException::class);
Expand Down
24 changes: 24 additions & 0 deletions tests/StorageDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function testSaveTmpData()
$this->assertFileEquals($this->generateFixturePath('tmp_data_non_formatted.json'), self::$tmpDocumentationFilePath);
}

public function testSaveSharedTmpData()
{
self::$storageDriverClass->saveSharedTmpData(fn () => self::$tmpData);

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

public function testGetTmpData()
{
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));
Expand All @@ -57,6 +65,22 @@ public function testGetTmpDataNoFile()
$this->assertNull($result);
}

public function testGetSharedTmpData()
{
file_put_contents(self::$tmpDocumentationFilePath, json_encode(self::$tmpData));

$result = self::$storageDriverClass->getSharedTmpData();

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

public function testGetSharedTmpDataNoFile()
{
$result = self::$storageDriverClass->getSharedTmpData();

$this->assertNull($result);
}

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