Skip to content

Commit 90b8fe5

Browse files
author
Kapitanov Andrey
committed
#104744
1 parent abac2b2 commit 90b8fe5

File tree

6 files changed

+81
-34
lines changed

6 files changed

+81
-34
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Ensi\LaravelOpenApiServerGenerator\Data\Controllers;
4+
5+
class ControllersStorage
6+
{
7+
/** @var array Recently created controllers */
8+
protected array $controllers = [];
9+
10+
public function markNewControllerMethod(
11+
string $serversUrl,
12+
string $path,
13+
string $method,
14+
array $responseCodes
15+
): void {
16+
$this->controllers[$serversUrl][$path][$method] = $responseCodes;
17+
}
18+
19+
public function isExistControllerMethod(
20+
string $serversUrl,
21+
string $path,
22+
string $method,
23+
int $responseCode
24+
): bool {
25+
$codes = $this->controllers[$serversUrl][$path][$method] ?? [];
26+
27+
return !in_array($responseCode, $codes);
28+
}
29+
}

src/Generators/BaseGenerator.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ensi\LaravelOpenApiServerGenerator\Generators;
44

5+
use Ensi\LaravelOpenApiServerGenerator\Data\Controllers\ControllersStorage;
56
use Ensi\LaravelOpenApiServerGenerator\Utils\ClassParser;
67
use Ensi\LaravelOpenApiServerGenerator\Utils\PhpDocGenerator;
78
use Ensi\LaravelOpenApiServerGenerator\Utils\PSR4PathConverter;
@@ -14,9 +15,6 @@
1415

1516
class BaseGenerator
1617
{
17-
/** @var array Recently created controllers */
18-
public static array $controllers = [];
19-
2018
protected array $options = [];
2119

2220
public function __construct(
@@ -27,29 +25,10 @@ public function __construct(
2725
protected TypesMapper $typesMapper,
2826
protected PhpDocGenerator $phpDocGenerator,
2927
protected ClassParser $classParser,
28+
protected ControllersStorage $controllersStorage,
3029
) {
3130
}
3231

33-
public static function markNewControllerMethod(
34-
string $serversUrl,
35-
string $path,
36-
string $method,
37-
array $responseCodes
38-
): void {
39-
static::$controllers[$serversUrl][$path][$method] = $responseCodes;
40-
}
41-
42-
public static function isExistControllerMethod(
43-
string $serversUrl,
44-
string $path,
45-
string $method,
46-
int $responseCode
47-
): bool {
48-
$codes = static::$controllers[$serversUrl][$path][$method] ?? [];
49-
50-
return !in_array($responseCode, $codes);
51-
}
52-
5332
public function setOptions(array $options): static
5433
{
5534
$this->options = $options;

src/Generators/ControllersGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private function convertMethodsToString(ClassParser $class, array $methods, arra
188188
]
189189
);
190190

191-
static::markNewControllerMethod(
191+
$this->controllersStorage->markNewControllerMethod(
192192
serversUrl: $this->serversUrl,
193193
path: $method['route']['path'],
194194
method: $method['route']['method'],

src/Generators/PestTestsGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function convertRoutesToTestsString(array $routes, string $serversUrl,
4444
continue;
4545
}
4646

47-
$methodExists = static::isExistControllerMethod(
47+
$methodExists = $this->controllersStorage->isExistControllerMethod(
4848
serversUrl: $serversUrl,
4949
path: $route['path'],
5050
method: $route['method'],
@@ -57,7 +57,7 @@ protected function convertRoutesToTestsString(array $routes, string $serversUrl,
5757

5858
$url = $serversUrl . $route['path'];
5959
$testName = strtoupper($route['method']) . ' ' . $url. ' ' . $responseCode;
60-
$phpHttpMethod = $this->getPhpHttpTestMethod($route['method'], $route['responseContentType']);
60+
$phpHttpMethod = $this->getPhpHttpTestMethod($route['method'], $route['responseContentType']);
6161
$testsFunctions[] = <<<FUNC
6262
6363
test('{$testName}', function () {

src/LaravelOpenApiServerGeneratorServiceProvider.php

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

55
use Ensi\LaravelOpenApiServerGenerator\Commands\GenerateServer;
6+
use Ensi\LaravelOpenApiServerGenerator\Data\Controllers\ControllersStorage;
67
use Ensi\LaravelOpenApiServerGenerator\Utils\PSR4PathConverter;
78
use Ensi\LaravelOpenApiServerGenerator\Utils\TemplatesManager;
89
use Illuminate\Support\ServiceProvider;
@@ -28,6 +29,10 @@ public function register()
2829
$this->app->when(PSR4PathConverter::class)
2930
->needs('$mappings')
3031
->give(config('openapi-server-generator.namespaces_to_directories_mapping', []));
32+
33+
$this->app->singleton(ControllersStorage::class, function () {
34+
return new ControllersStorage();
35+
});
3136
}
3237

3338
/**

tests/GenerateServerTest.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Filesystem\Filesystem;
66
use Illuminate\Support\Facades\Config;
77
use function Pest\Laravel\artisan;
8+
use function PHPUnit\Framework\assertEquals;
89
use function PHPUnit\Framework\assertEqualsCanonicalizing;
910
use function PHPUnit\Framework\assertNotTrue;
1011
use function PHPUnit\Framework\assertStringContainsString;
@@ -162,19 +163,22 @@
162163
);
163164
});
164165

165-
test("Update tests success", function () {
166+
test("Update tests success", function (array $parameters, bool $withControllerEntity) {
166167
/** @var TestCase $this */
167168
$mapping = Config::get('openapi-server-generator.api_docs_mappings');
168169
$mappingValue = current($mapping);
169170
$mapping = [$this->makeFilePath(__DIR__ . '/resources/index.yaml') => $mappingValue];
170171
Config::set('openapi-server-generator.api_docs_mappings', $mapping);
171172

173+
$appRoot = realpath($this->makeFilePath(__DIR__ . '/../vendor/orchestra/testbench-core/laravel/'));
174+
172175
$existTest = $this->makeFilePath('/app/Http/Tests/ResourcesComponentTest.php');
173176

174177
$filesystem = $this->mock(Filesystem::class);
175-
$filesystem->shouldReceive('exists')->andReturnUsing(function ($path) {
176-
// todo
177-
return false;
178+
$filesystem->shouldReceive('exists')->andReturnUsing(function ($path) use ($appRoot, $existTest) {
179+
$filePath = $this->makeFilePath(str_replace($appRoot, '', $path));
180+
181+
return $filePath === $existTest;
178182
});
179183

180184
$filesystem->shouldReceive('get')->withArgs(function ($path) {
@@ -183,7 +187,7 @@
183187
return file_get_contents($path);
184188
});
185189
$filesystem->shouldReceive('cleanDirectory', 'ensureDirectoryExists');
186-
$appRoot = realpath($this->makeFilePath(__DIR__ . '/../vendor/orchestra/testbench-core/laravel/'));
190+
187191
$putFiles = [];
188192
$filesystem->shouldReceive('put')->withArgs(function ($path, $content) use (&$putFiles, $appRoot) {
189193
$filePath = $this->makeFilePath(str_replace($appRoot, '', $path));
@@ -192,8 +196,38 @@
192196
return true;
193197
});
194198

195-
artisan(GenerateServer::class);
199+
$appendFiles = [];
200+
$filesystem->shouldReceive('append')->withArgs(function ($filePath, $data) use (&$appendFiles, $appRoot, $existTest) {
201+
$filePath = $this->makeFilePath(str_replace($appRoot, '', $filePath));
202+
$appendFiles[$filePath] = $data;
196203

197-
// todo: check exist test
204+
return true;
205+
});
198206

199-
});
207+
artisan(GenerateServer::class, $parameters);
208+
209+
$appendData = [
210+
'POST /resources:test-generate-without-properties 200',
211+
'POST /resources:test-empty-rename-request 200',
212+
'POST /resources:test-rename-request 200',
213+
'POST /resources:test-laravel-validations-application-json-request 200',
214+
'POST /resources:test-laravel-validations-multipart-form-data-request 200',
215+
'POST /resources:test-laravel-validations-non-available-content-type 200',
216+
'POST /resources:test-generate-resource-bad-response-key 200',
217+
'POST /resources:test-generate-without-properties 200',
218+
];
219+
220+
assertEquals(isset($appendFiles[$existTest]), $withControllerEntity);
221+
222+
if ($withControllerEntity) {
223+
$appendTestData = $appendFiles[$existTest];
224+
foreach ($appendData as $data) {
225+
assertStringContainsString($data, $appendTestData);
226+
}
227+
}
228+
})->with([
229+
[['-e' => 'pest_tests'], false],
230+
[['-e' => 'controllers,pest_tests'], true],
231+
[['-e' => 'pest_tests,controllers'], true],
232+
[[], true],
233+
]);

0 commit comments

Comments
 (0)