Skip to content

Commit e08b802

Browse files
committed
Add tests and fix failing tests
1 parent 75cafa9 commit e08b802

File tree

19 files changed

+309
-23
lines changed

19 files changed

+309
-23
lines changed

src/lib/items/RouteData.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,18 @@ public function getPrefixSettings():array
310310
protected function detectUrlPattern():void
311311
{
312312
if ($this->path === '/') {
313-
$this->type = self::TYPE_DEFAULT;
313+
$this->type = self::TYPE_DEFAULT; // issue is here https://github.com/php-openapi/yii2-openapi/issues/102
314314
$this->action = '';
315315
$this->controller = 'default';
316+
if (isset($this->operation->{CustomSpecAttr::ROUTE})) { # https://github.com/cebe/yii2-openapi/issues/144
317+
$customRoute = $this->operation->{CustomSpecAttr::ROUTE};
318+
$parts = explode('/', $customRoute);
319+
$this->action = array_pop($parts);
320+
$this->controller = array_pop($parts);
321+
}
316322
return;
317323
}
324+
318325
foreach ($this->urlPrefixes as $prefix => $rule) {
319326
if (!str_starts_with(trim($this->path, '/'), trim($prefix, '/'))) {
320327
continue;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* OpenAPI UrlRules
4+
*
5+
* This file is auto generated.
6+
*/
7+
return [
8+
'GET tasks' => 'fruit/mango/alphonso/list',
9+
'GET task/<id:\d+>' => 'fruit2/mango/alphonso/view',
10+
'tasks' => 'fruit/mango/alphonso/options',
11+
'task/<id:\d+>' => 'fruit2/mango/alphonso/options',
12+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Task extends \app\models\base\Task
6+
{
7+
8+
9+
}
10+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* This file is generated by Gii, do not change manually!
5+
*/
6+
7+
namespace app\models\base;
8+
9+
/**
10+
* This is the model class for table "tasks".
11+
*
12+
* @property int $id
13+
* @property string $title
14+
*
15+
*/
16+
abstract class Task extends \yii\db\ActiveRecord
17+
{
18+
public static function tableName()
19+
{
20+
return '{{%tasks}}';
21+
}
22+
23+
public function rules()
24+
{
25+
return [
26+
'trim' => [['title'], 'trim'],
27+
'title_string' => [['title'], 'string'],
28+
];
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace app\fruit;
4+
5+
class Module extends \yii\base\Module
6+
{
7+
8+
public function init()
9+
{
10+
parent::init();
11+
$this->modules = [
12+
'mango' => [
13+
'class' => \app\fruit\mango\Module::class,
14+
],
15+
];
16+
}
17+
18+
19+
}
20+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace app\fruit\mango;
4+
5+
class Module extends \yii\base\Module
6+
{
7+
8+
public function init()
9+
{
10+
parent::init();
11+
}
12+
13+
14+
}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace app\fruit\mango\controllers;
4+
5+
class AlphonsoController extends \app\fruit\mango\controllers\base\AlphonsoController
6+
{
7+
8+
public function actions()
9+
{
10+
$actions = parent::actions();
11+
return $actions;
12+
}
13+
14+
public function checkAccess($action, $model = null, $params = [])
15+
{
16+
//TODO implement checkAccess
17+
}
18+
19+
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace app\fruit\mango\controllers\base;
3+
4+
use insolita\fractal\JsonApiController;
5+
use Yii;
6+
7+
abstract class AlphonsoController extends JsonApiController
8+
{
9+
public function actions()
10+
{
11+
return [
12+
'list' => [
13+
'class' => \insolita\fractal\actions\ListAction::class,
14+
'checkAccess' => [$this, 'checkAccess'],
15+
'transformer' => \app\transformers\TaskTransformer::class,
16+
'modelClass' => \app\models\Task::class,
17+
'resourceKey' => 'tasks',
18+
'dataFilter' => null,
19+
'prepareDataProvider' => null
20+
],
21+
'options' => [
22+
'class' => \yii\rest\OptionsAction::class,
23+
],
24+
];
25+
}
26+
27+
/**
28+
* Checks the privilege of the current user.
29+
*
30+
* This method checks whether the current user has the privilege
31+
* to run the specified action against the specified data model.
32+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
33+
*
34+
* @param string $action the ID of the action to be executed
35+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
36+
* @param array $params additional parameters
37+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
38+
*/
39+
abstract public function checkAccess($action, $model = null, $params = []);
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace app\fruit2;
4+
5+
class Module extends \yii\base\Module
6+
{
7+
8+
public function init()
9+
{
10+
parent::init();
11+
$this->modules = [
12+
'mango' => [
13+
'class' => \app\fruit2\mango\Module::class,
14+
],
15+
];
16+
}
17+
18+
19+
}
20+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace app\fruit2\mango;
4+
5+
class Module extends \yii\base\Module
6+
{
7+
8+
public function init()
9+
{
10+
parent::init();
11+
}
12+
13+
14+
}
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace app\fruit2\mango\controllers;
4+
5+
class AlphonsoController extends \app\fruit2\mango\controllers\base\AlphonsoController
6+
{
7+
8+
public function actions()
9+
{
10+
$actions = parent::actions();
11+
return $actions;
12+
}
13+
14+
public function checkAccess($action, $model = null, $params = [])
15+
{
16+
//TODO implement checkAccess
17+
}
18+
19+
20+
}
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace app\fruit2\mango\controllers\base;
3+
4+
use insolita\fractal\JsonApiController;
5+
use Yii;
6+
7+
abstract class AlphonsoController extends JsonApiController
8+
{
9+
public function actions()
10+
{
11+
return [
12+
'view' => [
13+
'class' => \insolita\fractal\actions\ViewAction::class,
14+
'checkAccess' => [$this, 'checkAccess'],
15+
'transformer' => \app\transformers\TaskTransformer::class,
16+
'modelClass' => \app\models\Task::class,
17+
'resourceKey' => 'tasks',
18+
'findModel' => null
19+
],
20+
'options' => [
21+
'class' => \yii\rest\OptionsAction::class,
22+
],
23+
];
24+
}
25+
26+
/**
27+
* Checks the privilege of the current user.
28+
*
29+
* This method checks whether the current user has the privilege
30+
* to run the specified action against the specified data model.
31+
* If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
32+
*
33+
* @param string $action the ID of the action to be executed
34+
* @param object $model the model to be accessed. If null, it means no specific model is being accessed.
35+
* @param array $params additional parameters
36+
* @throws \yii\web\ForbiddenHttpException if the user does not have access
37+
*/
38+
abstract public function checkAccess($action, $model = null, $params = []);
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\transformers;
4+
5+
class TaskTransformer extends \app\transformers\base\TaskTransformer
6+
{
7+
8+
9+
}
10+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace app\transformers\base;
3+
4+
use League\Fractal\TransformerAbstract;
5+
use app\models\Task;
6+
7+
class TaskTransformer extends TransformerAbstract
8+
{
9+
protected array $availableIncludes = [];
10+
protected array $defaultIncludes = [];
11+
12+
public function transform(Task $model)
13+
{
14+
return $model->getAttributes();
15+
}
16+
}

tests/specs/issue_fix/14_nested_module_in_x_route/index.yml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ info:
44
title: Issue14Test testNestedModuleInXRoute \#14
55

66
paths:
7-
/:
7+
'/tasks':
88
get:
9-
x-route: fruit/mango/alphonso/view
10-
summary: List
11-
operationId: list
9+
x-route: fruit/mango/alphonso/list # action ID `list` is mandatory for fractal standalone action generation
10+
operationId: listTasks
1211
responses:
1312
'200':
14-
$ref: '#/components/responses/Task'
13+
$ref: '#/components/responses/Tasks'
1514

1615
'/task/{id}':
1716
parameters:
@@ -22,7 +21,7 @@ paths:
2221
schema:
2322
type: integer
2423
get:
25-
x-route: fruit2/mango/alphonso/view
24+
x-route: fruit2/mango/alphonso/view # action ID `view` is mandatory for fractal standalone action generation
2625
operationId: getTask
2726
summary: Get a Task by ID
2827
description: Returns a single task by its unique ID.
@@ -84,3 +83,16 @@ components:
8483
$ref: '#/components/schemas/_TaskResource'
8584
links:
8685
$ref: '#/components/schemas/JSONAPI_links'
86+
Tasks:
87+
description: 'An array of Tasks'
88+
content:
89+
application/vnd.api+json:
90+
schema:
91+
type: object
92+
properties:
93+
data:
94+
type: array
95+
items:
96+
$ref: '#/components/schemas/_TaskResource'
97+
links:
98+
$ref: '#/components/schemas/JSONAPI_links'

tests/specs/issue_fix/14_nested_module_in_x_route/mysql/config/urls.rest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* This file is auto generated.
66
*/
77
return [
8-
'GET ' => 'fruit/mango/alphonso/view',
8+
'GET tasks' => 'fruit/mango/alphonso/list',
99
'GET task/<id:\d+>' => 'fruit2/mango/alphonso/view',
10-
'' => 'fruit/mango/alphonso/options',
10+
'tasks' => 'fruit/mango/alphonso/options',
1111
'task/<id:\d+>' => 'fruit2/mango/alphonso/options',
1212
];

tests/specs/issue_fix/14_nested_module_in_x_route/mysql/modules/fruit/modules/mango/controllers/AlphonsoController.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ public function checkAccess($action, $model = null, $params = [])
1010
//TODO implement checkAccess
1111
}
1212

13-
public function actionView()
14-
{
15-
//TODO implement actionView
16-
}
17-
1813

1914
}
2015

tests/specs/issue_fix/14_nested_module_in_x_route/mysql/modules/fruit/modules/mango/controllers/base/AlphonsoController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ abstract class AlphonsoController extends \yii\rest\Controller
77
public function actions()
88
{
99
return [
10+
'list' => [
11+
'class' => \yii\rest\IndexAction::class,
12+
'modelClass' => \app\models\Task::class,
13+
'checkAccess' => [$this, 'checkAccess'],
14+
],
1015
'options' => [
1116
'class' => \yii\rest\OptionsAction::class,
1217
],
@@ -27,6 +32,4 @@ public function actions()
2732
*/
2833
abstract public function checkAccess($action, $model = null, $params = []);
2934

30-
abstract public function actionView();
31-
3235
}

0 commit comments

Comments
 (0)