Skip to content

Commit

Permalink
feat: implement support for brianium/paratest package to run tests …
Browse files Browse the repository at this point in the history
…in different workers;
  • Loading branch information
vitgrams committed Oct 25, 2024
1 parent 6919f6a commit c64db73
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 37 deletions.
2 changes: 2 additions & 0 deletions config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
*/
'documentation_viewer' => env('SWAGGER_SPEC_VIEWER', 'swagger'),

'tmp_dir' => storage_path('tmp_documentation'),

'drivers' => [
'local' => [
'class' => LocalDriver::class,
Expand Down
18 changes: 13 additions & 5 deletions src/Drivers/BaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace RonasIT\AutoDoc\Drivers;

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

abstract class BaseDriver implements SwaggerDriverContract
Expand All @@ -10,11 +12,19 @@ abstract class BaseDriver implements SwaggerDriverContract

public function __construct()
{
$this->tempFilePath = storage_path('temp_documentation.json');
$tmpDir = config('auto-doc.tmp_dir');

$this->tempFilePath = ($token = ParallelTesting::token())
? $tmpDir . "/temp_documentation_{$token}.json"
: $tmpDir . '/temp_documentation.json';
}

public function saveTmpData($data): void
{
if (!is_dir(dirname($this->tempFilePath))) {
mkdir(dirname($this->tempFilePath));
}

file_put_contents($this->tempFilePath, json_encode($data));
}

Expand All @@ -29,10 +39,8 @@ public function getTmpData(): ?array
return null;
}

protected function clearTmpData(): void
protected function clearTmpDir(): void
{
if (file_exists($this->tempFilePath)) {
unlink($this->tempFilePath);
}
File::deleteDirectory(config('auto-doc.tmp_dir'));
}
}
2 changes: 1 addition & 1 deletion src/Drivers/LocalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function saveData(): void
{
file_put_contents($this->prodFilePath, json_encode($this->getTmpData()));

$this->clearTmpData();
$this->clearTmpDir();
}

public function getDocumentation(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/RemoteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function saveData(): void
'Content-Type: application/json',
]);

$this->clearTmpData();
$this->clearTmpDir();
}

public function getDocumentation(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/StorageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function saveData(): void
{
$this->disk->put($this->prodFilePath, json_encode($this->getTmpData()));

$this->clearTmpData();
$this->clearTmpDir();
}

public function getDocumentation(): array
Expand Down
7 changes: 6 additions & 1 deletion src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,11 @@ public function saveProductionData()
$this->driver->saveData();
}

public function saveTmpData(array $data): void
{
$this->driver->saveTmpData($data);
}

public function getDocFileContent()
{
$documentation = $this->driver->getDocumentation();
Expand Down Expand Up @@ -971,7 +976,7 @@ protected function getOpenAPIFileContent(string $filePath): array
return $fileContent;
}

protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocumentation): void
public function mergeOpenAPIDocs(array &$documentation, array $additionalDocumentation): void
{
$paths = array_keys($additionalDocumentation['paths']);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

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;

final class ApplicationFinishedSubscriber implements FinishedSubscriber
{
public function notify(Finished $event): void
{
$this->createApplication();

if ($this->isReadyToSaveProductionData()) {
app(SwaggerService::class)->saveProductionData();
}
}

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

$app->loadEnvironmentFrom('.env.testing');
$app->make(Kernel::class)->bootstrap();
}

protected function isReadyToSaveProductionData(): bool
{
if ($token = ParallelTesting::token()) {
unlink(config('auto-doc.tmp_dir') . "/worker_{$token}_in_progress.flag");

if (empty(glob(config('auto-doc.tmp_dir') . '/worker_*_in_progress.flag'))) {
$this->mergeTempDocumentation();

return true;
}
} else {
return true;
}

return false;
}

protected function mergeTempDocumentation(): void
{
$swaggerService = app(SwaggerService::class);

$resultDoc = [];

$tmpPaths = glob(config('auto-doc.tmp_dir') . '/temp_documentation_*.json');

foreach ($tmpPaths as $tmpDocFilePath) {
$tmpDoc = json_decode(file_get_contents($tmpDocFilePath), true);

if (empty($resultDoc)) {
$resultDoc = $tmpDoc;
} else {
$swaggerService->mergeOpenAPIDocs($resultDoc, $tmpDoc);
}
}

$swaggerService->saveTmpData($resultDoc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace RonasIT\AutoDoc\Support\PHPUnit\EventSubscribers;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\ParallelTesting;
use PHPUnit\Event\Application\Started;
use PHPUnit\Event\Application\StartedSubscriber;

final class ApplicationStartedSubscriber implements StartedSubscriber
{
public function notify(Started $event): void
{
$this->createApplication();

if ($token = ParallelTesting::token()) {
touch(config('auto-doc.tmp_dir') . "/worker_{$token}_in_progress.flag");
}
}

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

$app->loadEnvironmentFrom('.env.testing');
$app->make(Kernel::class)->bootstrap();
}
}

This file was deleted.

6 changes: 4 additions & 2 deletions src/Support/PHPUnit/Extensions/SwaggerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use PHPUnit\Runner\Extension\Facade as EventFacade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
use RonasIT\AutoDoc\Support\PHPUnit\EventSubscribers\SwaggerSaveDocumentationSubscriber;
use RonasIT\AutoDoc\Support\PHPUnit\EventSubscribers\ApplicationFinishedSubscriber;
use RonasIT\AutoDoc\Support\PHPUnit\EventSubscribers\ApplicationStartedSubscriber;

final class SwaggerExtension implements PhpunitExtension
{
public function bootstrap(Configuration $configuration, EventFacade $facade, ParameterCollection $parameters): void
{
$facade->registerSubscriber(new SwaggerSaveDocumentationSubscriber());
$facade->registerSubscriber(new ApplicationStartedSubscriber());
$facade->registerSubscriber(new ApplicationFinishedSubscriber());
}
}

0 comments on commit c64db73

Please sign in to comment.