Skip to content

Commit 3a44f9b

Browse files
committed
Refactor and add test for RestAction
1 parent bbefd8e commit 3a44f9b

File tree

5 files changed

+83
-62
lines changed

5 files changed

+83
-62
lines changed

src/lib/items/FractalAction.php

+2-51
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
final class FractalAction extends BaseObject
3434
{
35+
use OptionsRouteTrait;
36+
3537
/**@var string* */
3638
public $id;
3739

@@ -102,27 +104,6 @@ public function getRoute():string
102104
return $this->controllerId.'/'.$this->id;
103105
}
104106

105-
public function getOptionsRoute():string
106-
{
107-
if ($this->prefix && !empty($this->prefixSettings)) {
108-
if (isset($this->prefixSettings['module'])) {
109-
$prefix = $this->prefixSettings['module'];
110-
return static::finalOptionsRoute($prefix, $this->controllerId);
111-
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) {
112-
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']);
113-
if ($prefix) {
114-
return static::finalOptionsRoute($prefix, $this->controllerId);
115-
}
116-
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) {
117-
$prefix = static::computeModule('/', $this->prefixSettings['path']);
118-
if ($prefix) {
119-
return static::finalOptionsRoute($prefix, $this->controllerId);
120-
}
121-
}
122-
}
123-
return $this->controllerId.'/options';
124-
}
125-
126107
public function getBaseModelName():string
127108
{
128109
return $this->modelFqn ? StringHelper::basename($this->modelFqn) : '';
@@ -262,34 +243,4 @@ public function getIdParamType(): string
262243
}
263244
return $this->params[$this->idParam]['type'] === 'integer' ? 'int' : 'string';
264245
}
265-
266-
/**
267-
* @param string $separator
268-
* @param string $entity path or namespace
269-
* @return void
270-
*/
271-
public static function computeModule(string $separator, string $entity): ?string
272-
{
273-
$parts = explode($separator . 'modules' . $separator, $entity);
274-
if (empty($parts[1])) {
275-
return null;
276-
}
277-
if (str_contains($parts[1], 'controller')) {
278-
$result = explode($separator . 'controller', $parts[1]);
279-
$result = array_map(function ($val) {
280-
return str_replace('\\', '/', $val);
281-
}, $result);
282-
} else {
283-
$result = explode($separator, $parts[1]);
284-
}
285-
if (empty($result[0])) {
286-
return null;
287-
}
288-
return $result[0];
289-
}
290-
291-
public static function finalOptionsRoute(string $prefix, string $controllerId): string
292-
{
293-
return trim($prefix, '/') . '/' . $controllerId . '/options';
294-
}
295246
}

src/lib/items/OptionsRouteTrait.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2018 Carsten Brandt <[email protected]> and contributors
5+
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
6+
*/
7+
8+
namespace cebe\yii2openapi\lib\items;
9+
10+
trait OptionsRouteTrait
11+
{
12+
public function getOptionsRoute():string
13+
{
14+
if ($this->prefix && !empty($this->prefixSettings)) {
15+
if (isset($this->prefixSettings['module'])) {
16+
$prefix = $this->prefixSettings['module'];
17+
return static::finalOptionsRoute($prefix, $this->controllerId);
18+
} elseif (isset($this->prefixSettings['namespace']) && str_contains($this->prefixSettings['namespace'], '\modules\\')) {
19+
$prefix = static::computeModule('\\', $this->prefixSettings['namespace']);
20+
if ($prefix) {
21+
return static::finalOptionsRoute($prefix, $this->controllerId);
22+
}
23+
} elseif (isset($this->prefixSettings['path']) && str_contains($this->prefixSettings['path'], '/modules/')) {
24+
$prefix = static::computeModule('/', $this->prefixSettings['path']);
25+
if ($prefix) {
26+
return static::finalOptionsRoute($prefix, $this->controllerId);
27+
}
28+
}
29+
}
30+
return $this->controllerId.'/options';
31+
}
32+
33+
/**
34+
* @param string $separator
35+
* @param string $entity path or namespace
36+
* @return void
37+
*/
38+
public static function computeModule(string $separator, string $entity): ?string
39+
{
40+
$parts = explode($separator . 'modules' . $separator, $entity); # /app/modules/forum/controllers => /forum/controllers
41+
if (empty($parts[1])) {
42+
return null;
43+
}
44+
if (str_contains($parts[1], 'controller')) {
45+
$result = explode($separator . 'controller', $parts[1]); // compute everything in between "modules" and "controllers" e.g. api/v1
46+
$result = array_map(function ($val) {
47+
return str_replace('\\', '/', $val);
48+
}, $result);
49+
} else {
50+
$result = explode($separator, $parts[1]); # forum/controllers => forum
51+
}
52+
if (empty($result[0])) {
53+
return null;
54+
}
55+
return $result[0];
56+
}
57+
58+
public static function finalOptionsRoute(string $prefix, string $controllerId): string
59+
{
60+
return trim($prefix, '/') . '/' . $controllerId . '/options';
61+
}
62+
}

src/lib/items/RestAction.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
final class RestAction extends BaseObject
3333
{
34+
use OptionsRouteTrait;
35+
3436
/**@var string* */
3537
public $id;
3638

@@ -77,16 +79,6 @@ public function getRoute():string
7779
return $this->controllerId.'/'.$this->id;
7880
}
7981

80-
public function getOptionsRoute():string
81-
{
82-
//@TODO: re-check
83-
if ($this->prefix && !empty($this->prefixSettings)) {
84-
$prefix = $this->prefixSettings['module'] ?? $this->prefix;
85-
return trim($prefix, '/').'/'.$this->controllerId.'/options';
86-
}
87-
return $this->controllerId.'/options';
88-
}
89-
9082
public function getBaseModelName():string
9183
{
9284
return $this->modelFqn ? StringHelper::basename($this->modelFqn) : '';

tests/specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
'generateControllers' => true,
1111
'generateMigrations' => false,
12-
'useJsonApi' => true,
12+
'useJsonApi' => false,
1313
'urlPrefixes' => [
1414
'animals' => '',
1515
'/info' => ['module' => 'petinfo', 'namespace' => '\app\modules\petinfo\controllers'],

tests/unit/IssueFixTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,22 @@ public function test35ResolveTodoReCheckOptionsRouteInFractalAction()
373373
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/mysql"), [
374374
// 'recursive' => true,
375375
// ]);
376+
// $this->checkFiles($actualFiles, $expectedFiles);
377+
}
378+
379+
// https://github.com/php-openapi/yii2-openapi/issues/35
380+
public function test35ResolveTodoReCheckOptionsRouteInRestAction()
381+
{
382+
$testFile = Yii::getAlias("@specs/issue_fix/35_resolve_todo_re_check_options_route_in_fractal_action/index.php");
383+
$content = str_replace("'useJsonApi' => true,", "'useJsonApi' => false,", file_get_contents($testFile));
384+
file_put_contents($testFile, $content);
385+
$this->runGenerator($testFile);
386+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
387+
// 'recursive' => true,
388+
// ]);
389+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/158_bug_giiapi_generated_rules_enum_with_trim/mysql"), [
390+
// 'recursive' => true,
391+
// ]);
376392
// $this->checkFiles($actualFiles, $expectedFiles);
377393
}
378394
}

0 commit comments

Comments
 (0)