Skip to content

Commit 2955172

Browse files
Merge pull request #38 from RonasIT/37-php-unit-9-support
#feat: implement compatibility with PHPUnit 9
2 parents 2a83eb2 + 8a8c897 commit 2955172

File tree

6 files changed

+87
-43
lines changed

6 files changed

+87
-43
lines changed

readme.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,26 @@ to display the generated documentation for a config.
2626
### Plugin
2727
1. Add middleware **\RonasIT\Support\AutoDoc\Http\Middleware\AutoDocMiddleware::class** to *Http/Kernel.php*.
2828
1. Use **\RonasIT\Support\AutoDoc\Tests\AutoDocTestCaseTrait** in your TestCase in *tests/TestCase.php*
29-
1. Call `saveDocumentation` method in the `TearDown` method of your base TestCase class
3029
1. In *config/auto-doc.php* you can specify enabling of plugin, info of your project,
3130
some defaults descriptions and route for rendering of documentation.
3231
1. In *.env* file you should add following lines
3332
`
3433
LOCAL_DATA_COLLECTOR_PROD_PATH=/example-folder/documentation.json
3534
LOCAL_DATA_COLLECTOR_TEMP_PATH=/tmp/documentation.json
3635
`
36+
1. Configure documentation saving, using one of the next way:
37+
- Add `SwaggerExtension` to the `<extensions>` block of your `phpunit.xml`. Please note that this way will be removed after updating PHPUnit up to 10 version (https://github.com/sebastianbergmann/phpunit/issues/4676)
38+
```
39+
<extensions>
40+
<extension class="RonasIT\Support\AutoDoc\Tests\PhpUnitExtensions\SwaggerExtension"/>
41+
</extensions>
42+
<testsuites>
43+
<testsuite name="Feature">
44+
<directory suffix="Test.php">./tests/Feature</directory>
45+
</testsuite>
46+
</testsuites>
47+
```
48+
- Call `php artisan swagger:push-documentation` console command after the `tests` stage in your CI/CD configuration
3749

3850
## Usages
3951
For correct working of plugin you have to dispose all the validation rules in the rules() method of class YourRequest,

src/AutoDocServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RonasIT\Support\AutoDoc;
44

55
use Illuminate\Support\ServiceProvider;
6+
use RonasIT\Support\AutoDoc\Commands\PushDocumentationCommand;
67

78
class AutoDocServiceProvider extends ServiceProvider
89
{
@@ -24,6 +25,10 @@ public function boot()
2425
require __DIR__ . '/Http/routes.php';
2526
}
2627

28+
$this->commands([
29+
PushDocumentationCommand::class
30+
]);
31+
2732
$this->loadViewsFrom(__DIR__ . '/Views', 'auto-doc');
2833
}
2934

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace RonasIT\Support\AutoDoc\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use RonasIT\Support\AutoDoc\Services\SwaggerService;
7+
8+
class PushDocumentationCommand extends Command
9+
{
10+
protected $signature = 'swagger:push-documentation';
11+
protected $description = 'Push swagger collected documentation';
12+
13+
public function handle(): int
14+
{
15+
app(SwaggerService::class)->saveProductionData();
16+
17+
return 0;
18+
}
19+
}

src/Services/SwaggerService.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -543,24 +543,23 @@ protected function parseRequestName($request)
543543

544544
protected function getResponseDescription($code)
545545
{
546+
$defaultDescription = Response::$statusTexts[$code];
547+
546548
$request = $this->getConcreteRequest();
547549

548-
return elseChain(
549-
function () use ($request, $code) {
550-
return empty($request) ? Response::$statusTexts[$code] : null;
551-
},
552-
function () use ($request, $code) {
553-
$annotations = $this->getClassAnnotations($request);
554-
555-
return Arr::get($annotations, "_{$code}");
556-
},
557-
function () use ($code) {
558-
return config("auto-doc.defaults.code-descriptions.{$code}");
559-
},
560-
function () use ($code) {
561-
return Response::$statusTexts[$code];
562-
}
563-
);
550+
if (empty($request)) {
551+
return $defaultDescription;
552+
}
553+
554+
$annotations = $this->getClassAnnotations($request);
555+
556+
$localDescription = Arr::get($annotations, "_{$code}");
557+
558+
if (!empty($localDescription)) {
559+
return $localDescription;
560+
}
561+
562+
return config("auto-doc.defaults.code-descriptions.{$code}", $defaultDescription);
564563
}
565564

566565
protected function getActionName($uri)

src/Tests/AutoDocTestCaseTrait.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,10 @@
22

33
namespace RonasIT\Support\AutoDoc\Tests;
44

5-
use RonasIT\Support\AutoDoc\Services\SwaggerService;
65
use RonasIT\Support\AutoDoc\Http\Middleware\AutoDocMiddleware;
76

87
trait AutoDocTestCaseTrait
98
{
10-
public $docService;
11-
12-
public function createApplication()
13-
{
14-
parent::createApplication();
15-
}
16-
17-
public function tearDown(): void
18-
{
19-
$this->saveDocumentation();
20-
21-
parent::tearDown();
22-
}
23-
24-
public function saveDocumentation()
25-
{
26-
$currentTestCount = $this->getTestResultObject()->count();
27-
$allTestCount = $this->getTestResultObject()->topTestSuite()->count();
28-
29-
if (($currentTestCount == $allTestCount) && (!$this->hasFailed())) {
30-
$docService = $this->docService ?? app(SwaggerService::class);
31-
$docService->saveProductionData();
32-
}
33-
}
34-
359
/**
3610
* Disabling documentation collecting on current test
3711
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace RonasIT\Support\AutoDoc\Tests\PhpUnitExtensions;
4+
5+
use PHPUnit\Runner\AfterLastTestHook;
6+
use Illuminate\Foundation\Application;
7+
use Illuminate\Contracts\Console\Kernel;
8+
use RonasIT\Support\AutoDoc\Services\SwaggerService;
9+
10+
/**
11+
* This interface, as well as the associated mechanism for extending PHPUnit,
12+
* will be removed in PHPUnit 10. There is no alternative available in this
13+
* version of PHPUnit.
14+
*
15+
* @see https://github.com/sebastianbergmann/phpunit/issues/4676
16+
*/
17+
class SwaggerExtension implements AfterLastTestHook
18+
{
19+
public function executeAfterLastTest(): void
20+
{
21+
$this->createApplication();
22+
23+
app(SwaggerService::class)->saveProductionData();
24+
}
25+
26+
protected function createApplication(): Application
27+
{
28+
$app = require __DIR__ . '/../../../../../../bootstrap/app.php';
29+
30+
$app->loadEnvironmentFrom('.env.testing');
31+
$app->make(Kernel::class)->bootstrap();
32+
33+
return $app;
34+
}
35+
}

0 commit comments

Comments
 (0)