-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace SwaggerBake\Test\TestCase\Lib\Route; | ||
|
||
use Cake\Collection\Collection; | ||
use Cake\Routing\RouteBuilder; | ||
use Cake\Routing\Router; | ||
use Cake\TestSuite\TestCase; | ||
use SwaggerBake\Lib\Configuration; | ||
use SwaggerBake\Lib\Model\ModelScanner; | ||
use SwaggerBake\Lib\Route\RouteScanner; | ||
use SwaggerBake\Lib\Swagger; | ||
|
||
class PrefixRouteTest extends TestCase | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
public $fixtures = [ | ||
'plugin.SwaggerBake.DepartmentEmployees', | ||
'plugin.SwaggerBake.Departments', | ||
'plugin.SwaggerBake.Employees', | ||
]; | ||
|
||
/** | ||
* @var Router | ||
*/ | ||
private $router; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $config; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); // TODO: Change the autogenerated stub | ||
$router = new Router(); | ||
$router::scope('/', function (RouteBuilder $builder) { | ||
$builder->setExtensions(['json']); | ||
$builder->resources('Departments'); | ||
$builder->resources('Departments', [ | ||
'prefix' => 'Admin', | ||
'path' => 'admin/departments', | ||
'only' => ['index'] | ||
]); | ||
}); | ||
$this->router = $router; | ||
|
||
$this->config = [ | ||
'prefix' => '/', | ||
'yml' => '/config/swagger-bare-bones.yml', | ||
'json' => '/webroot/swagger.json', | ||
'webPath' => '/swagger.json', | ||
'hotReload' => false, | ||
'exceptionSchema' => 'Exception', | ||
'requestAccepts' => ['application/x-www-form-urlencoded'], | ||
'responseContentTypes' => ['application/json'], | ||
'namespaces' => [ | ||
'controllers' => ['\SwaggerBakeTest\App\\'], | ||
'entities' => ['\SwaggerBakeTest\App\\'], | ||
'tables' => ['\SwaggerBakeTest\App\\'], | ||
] | ||
]; | ||
} | ||
|
||
/** | ||
* Tests prefix routing by verifying that paths are added for: | ||
* - App\Controller\DepartmentsController | ||
* - App\Controller\Admin\DepartmentsController | ||
*/ | ||
public function test_prefix_routing_with_same_controller_shortname(): void | ||
{ | ||
$config = new Configuration($this->config, SWAGGER_BAKE_TEST_APP); | ||
$cakeRoute = new RouteScanner($this->router, $config); | ||
$openApi = (new Swagger(new ModelScanner($cakeRoute, $config)))->getArray(); | ||
$paths = new Collection(array_keys($openApi['paths'])); | ||
$this->assertTrue($paths->contains('/admin/departments')); | ||
$this->assertTrue($paths->contains('/departments')); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/test_app/src/Controller/Admin/DepartmentsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace SwaggerBakeTest\App\Controller\Admin; | ||
|
||
use SwaggerBakeTest\App\Controller\AppController; | ||
|
||
/** | ||
* Departments Controller | ||
* | ||
* @property \App\Model\Table\DepartmentsTable $Departments | ||
* | ||
* @method \App\Model\Entity\Department[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) | ||
*/ | ||
class DepartmentsController extends AppController | ||
{ | ||
/** | ||
* Just a test for prefix routing when two controllers share the same short name | ||
*/ | ||
public function index() | ||
{ | ||
$departments = $this->paginate($this->Departments); | ||
|
||
$this->set(compact('departments')); | ||
$this->viewBuilder()->setOption('serialize', 'departments'); | ||
} | ||
} |