2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
|
|
|
|
2016-05-18 19:40:34 +03:00
|
|
|
namespace Test\AppFramework\Routing;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
use OC\AppFramework\DependencyInjection\DIContainer;
|
2016-05-18 19:40:34 +03:00
|
|
|
use OC\AppFramework\Routing\RouteActionHandler;
|
|
|
|
use OC\AppFramework\Routing\RouteConfig;
|
2020-04-14 18:53:15 +03:00
|
|
|
use OC\Route\Route;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Route\Router;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\ILogger;
|
2019-01-21 14:02:36 +03:00
|
|
|
use OCP\Route\IRouter;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
class RoutingTest extends \Test\TestCase {
|
|
|
|
public function testSimpleRoute() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
|
|
|
|
]];
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleRouteWithUnderScoreNames() {
|
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'root' => '']
|
|
|
|
]];
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent', [], [], '', true);
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
public function testSimpleOCSRoute() {
|
|
|
|
$routes = ['ocs' => [
|
2020-04-09 10:22:29 +03:00
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
|
|
|
|
]
|
2016-07-14 21:45:19 +03:00
|
|
|
];
|
|
|
|
|
2016-08-01 17:37:48 +03:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2016-07-14 21:45:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testSimpleRouteWithMissingVerb() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
|
|
|
|
]];
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
public function testSimpleOCSRouteWithMissingVerb() {
|
|
|
|
$routes = ['ocs' => [
|
2020-04-09 10:22:29 +03:00
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
|
|
|
|
]
|
2016-07-14 21:45:19 +03:00
|
|
|
];
|
|
|
|
|
2016-08-01 17:37:48 +03:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2016-07-14 21:45:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testSimpleRouteWithLowercaseVerb() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]];
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
public function testSimpleOCSRouteWithLowercaseVerb() {
|
|
|
|
$routes = ['ocs' => [
|
2020-04-09 10:22:29 +03:00
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]
|
2016-07-14 21:45:19 +03:00
|
|
|
];
|
|
|
|
|
2016-08-01 17:37:48 +03:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2016-07-14 21:45:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testSimpleRouteWithRequirements() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
|
|
|
|
]];
|
2014-04-09 23:57:32 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
|
2014-04-09 23:57:32 +04:00
|
|
|
}
|
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
public function testSimpleOCSRouteWithRequirements() {
|
|
|
|
$routes = ['ocs' => [
|
2020-04-09 10:22:29 +03:00
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
|
|
|
|
]
|
2016-07-14 21:45:19 +03:00
|
|
|
];
|
|
|
|
|
2016-08-01 17:37:48 +03:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
|
2016-07-14 21:45:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testSimpleRouteWithDefaults() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', [], 'defaults' => ['param' => 'foobar']]
|
|
|
|
]];
|
2014-09-07 14:20:48 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
|
2014-09-07 14:20:48 +04:00
|
|
|
}
|
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
|
|
|
|
public function testSimpleOCSRouteWithDefaults() {
|
|
|
|
$routes = ['ocs' => [
|
2020-04-09 10:22:29 +03:00
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'defaults' => ['param' => 'foobar']]
|
|
|
|
]
|
2016-07-14 21:45:19 +03:00
|
|
|
];
|
|
|
|
|
2016-08-01 17:37:48 +03:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
|
2016-07-14 21:45:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testSimpleRouteWithPostfix() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
|
|
|
|
]];
|
2014-11-19 14:00:41 +03:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
|
2014-11-19 14:00:41 +03:00
|
|
|
}
|
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
public function testSimpleOCSRouteWithPostfix() {
|
|
|
|
$routes = ['ocs' => [
|
2020-04-09 10:22:29 +03:00
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
|
|
|
|
]
|
2016-07-14 21:45:19 +03:00
|
|
|
];
|
|
|
|
|
2016-08-01 17:37:48 +03:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
|
2016-07-14 21:45:19 +03:00
|
|
|
}
|
2014-04-09 23:57:32 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testSimpleRouteWithBrokenName() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\UnexpectedValueException::class);
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['routes' => [
|
|
|
|
['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]];
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
/** @var IRouter|MockObject $router */
|
|
|
|
$router = $this->getMockBuilder(Router::class)
|
|
|
|
->onlyMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->createMock(ILogger::class)])
|
2016-07-10 15:17:26 +03:00
|
|
|
->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
public function testSimpleOCSRouteWithBrokenName() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\UnexpectedValueException::class);
|
|
|
|
|
2016-07-14 21:45:19 +03:00
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]];
|
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
/** @var IRouter|MockObject $router */
|
|
|
|
$router = $this->getMockBuilder(Router::class)
|
|
|
|
->onlyMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->createMock(ILogger::class)])
|
2016-07-14 21:45:19 +03:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleOCSRouteWithUnderScoreNames() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]];
|
|
|
|
|
2016-08-01 17:37:48 +03:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'admin_folders.open_current', 'DELETE', '/apps/app1/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
|
2016-07-14 21:45:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testOCSResource() {
|
2019-01-21 14:02:36 +03:00
|
|
|
$routes = ['ocs-resources' => ['account' => ['url' => '/accounts']]];
|
|
|
|
|
|
|
|
$this->assertOCSResource($routes, 'account', '/apps/app1/accounts', 'AccountController', 'id');
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testOCSResourceWithUnderScoreName() {
|
2019-01-21 14:02:36 +03:00
|
|
|
$routes = ['ocs-resources' => ['admin_accounts' => ['url' => '/admin/accounts']]];
|
|
|
|
|
|
|
|
$this->assertOCSResource($routes, 'admin_accounts', '/apps/app1/admin/accounts', 'AdminAccountsController', 'id');
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testOCSResourceWithRoot() {
|
2019-01-21 14:02:36 +03:00
|
|
|
$routes = ['ocs-resources' => ['admin_accounts' => ['url' => '/admin/accounts', 'root' => '/core/endpoint']]];
|
|
|
|
|
|
|
|
$this->assertOCSResource($routes, 'admin_accounts', '/core/endpoint/admin/accounts', 'AdminAccountsController', 'id');
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testResource() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['resources' => ['account' => ['url' => '/accounts']]];
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-21 23:45:35 +03:00
|
|
|
$this->assertResource($routes, 'account', '/apps/app1/accounts', 'AccountController', 'id');
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function testResourceWithUnderScoreName() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$routes = ['resources' => ['admin_accounts' => ['url' => '/admin/accounts']]];
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-21 23:45:35 +03:00
|
|
|
$this->assertResource($routes, 'admin_accounts', '/apps/app1/admin/accounts', 'AdminAccountsController', 'id');
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements = [], array $defaults = [], $postfix = '', $allowRootUrl = false): void {
|
2014-11-19 14:00:41 +03:00
|
|
|
if ($postfix) {
|
|
|
|
$name .= $postfix;
|
|
|
|
}
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
// route mocks
|
2015-07-24 14:43:50 +03:00
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
/** @var IRouter|MockObject $router */
|
|
|
|
$router = $this->getMockBuilder(Router::class)
|
|
|
|
->onlyMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->createMock(ILogger::class)])
|
2016-07-10 15:17:26 +03:00
|
|
|
->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
// we expect create to be called once:
|
|
|
|
$router
|
|
|
|
->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $name), $this->equalTo($url))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($route);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
2020-04-14 18:53:15 +03:00
|
|
|
if ($allowRootUrl) {
|
|
|
|
self::invokePrivate($config, 'rootUrlApps', [['app1']]);
|
|
|
|
}
|
2016-07-14 21:45:19 +03:00
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $routes
|
|
|
|
* @param string $name
|
|
|
|
* @param string $verb
|
|
|
|
* @param string $url
|
|
|
|
* @param string $controllerName
|
|
|
|
* @param string $actionName
|
|
|
|
* @param array $requirements
|
|
|
|
* @param array $defaults
|
|
|
|
* @param string $postfix
|
|
|
|
*/
|
|
|
|
private function assertSimpleOCSRoute($routes,
|
|
|
|
$name,
|
|
|
|
$verb,
|
|
|
|
$url,
|
|
|
|
$controllerName,
|
|
|
|
$actionName,
|
2020-03-26 11:30:18 +03:00
|
|
|
array $requirements=[],
|
|
|
|
array $defaults=[],
|
2020-04-10 15:19:56 +03:00
|
|
|
$postfix='') {
|
2016-07-14 21:45:19 +03:00
|
|
|
if ($postfix) {
|
|
|
|
$name .= $postfix;
|
|
|
|
}
|
|
|
|
|
|
|
|
// route mocks
|
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
|
|
|
|
|
2020-04-14 18:53:15 +03:00
|
|
|
/** @var IRouter|MockObject $router */
|
|
|
|
$router = $this->getMockBuilder(Router::class)
|
|
|
|
->onlyMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->createMock(ILogger::class)])
|
2016-07-14 21:45:19 +03:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// we expect create to be called once:
|
|
|
|
$router
|
|
|
|
->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('ocs.app1.' . $name), $this->equalTo($url))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($route);
|
2016-07-14 21:45:19 +03:00
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
2019-01-21 14:02:36 +03:00
|
|
|
/**
|
|
|
|
* @param array $yaml
|
|
|
|
* @param string $resourceName
|
|
|
|
* @param string $url
|
|
|
|
* @param string $controllerName
|
|
|
|
* @param string $paramName
|
|
|
|
*/
|
|
|
|
private function assertOCSResource($yaml, $resourceName, $url, $controllerName, $paramName): void {
|
|
|
|
/** @var IRouter|MockObject $router */
|
|
|
|
$router = $this->getMockBuilder(Router::class)
|
2020-04-14 18:53:15 +03:00
|
|
|
->onlyMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->createMock(ILogger::class)])
|
2019-01-21 14:02:36 +03:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// route mocks
|
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
|
|
|
|
$showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
|
|
|
|
$createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
|
|
|
|
$updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
|
|
|
|
$destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
|
|
|
|
|
|
|
|
$urlWithParam = $url . '/{' . $paramName . '}';
|
|
|
|
|
|
|
|
// we expect create to be called once:
|
|
|
|
$router
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('ocs.app1.' . $resourceName . '.index'), $this->equalTo($url))
|
|
|
|
->willReturn($indexRoute);
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('ocs.app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam))
|
|
|
|
->willReturn($showRoute);
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('ocs.app1.' . $resourceName . '.create'), $this->equalTo($url))
|
|
|
|
->willReturn($createRoute);
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(3))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('ocs.app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam))
|
|
|
|
->willReturn($updateRoute);
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(4))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('ocs.app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam))
|
|
|
|
->willReturn($destroyRoute);
|
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$config = new RouteConfig($container, $router, $yaml);
|
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
|
|
|
* @param string $resourceName
|
|
|
|
* @param string $url
|
|
|
|
* @param string $controllerName
|
|
|
|
* @param string $paramName
|
|
|
|
*/
|
2020-04-10 15:19:56 +03:00
|
|
|
private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName) {
|
2020-04-14 18:53:15 +03:00
|
|
|
/** @var IRouter|MockObject $router */
|
|
|
|
$router = $this->getMockBuilder(Router::class)
|
|
|
|
->onlyMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->createMock(ILogger::class)])
|
2016-07-10 15:17:26 +03:00
|
|
|
->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
// route mocks
|
2015-07-24 14:43:50 +03:00
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
|
|
|
|
$showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
|
|
|
|
$createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
|
|
|
|
$updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
|
|
|
|
$destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$urlWithParam = $url . '/{' . $paramName . '}';
|
|
|
|
|
|
|
|
// we expect create to be called once:
|
|
|
|
$router
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.index'), $this->equalTo($url))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($indexRoute);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($showRoute);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.create'), $this->equalTo($url))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($createRoute);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(3))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($updateRoute);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(4))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($destroyRoute);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$config = new RouteConfig($container, $router, $yaml);
|
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-24 14:43:50 +03:00
|
|
|
* @param DIContainer $container
|
2014-02-28 16:53:41 +04:00
|
|
|
* @param string $verb
|
|
|
|
* @param string $controllerName
|
|
|
|
* @param string $actionName
|
2015-07-24 14:43:50 +03:00
|
|
|
* @param array $requirements
|
|
|
|
* @param array $defaults
|
2020-04-14 18:53:15 +03:00
|
|
|
* @return MockObject
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
2015-07-24 14:43:50 +03:00
|
|
|
private function mockRoute(
|
|
|
|
DIContainer $container,
|
|
|
|
$verb,
|
|
|
|
$controllerName,
|
|
|
|
$actionName,
|
2020-03-26 11:30:18 +03:00
|
|
|
array $requirements=[],
|
|
|
|
array $defaults=[]
|
2015-07-24 14:43:50 +03:00
|
|
|
) {
|
2020-04-14 18:53:15 +03:00
|
|
|
$route = $this->getMockBuilder(Route::class)
|
|
|
|
->onlyMethods(['method', 'action', 'requirements', 'defaults'])
|
2016-07-10 15:17:26 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
$route
|
2020-04-14 18:53:15 +03:00
|
|
|
->expects($this->once())
|
2013-08-17 13:16:48 +04:00
|
|
|
->method('method')
|
|
|
|
->with($this->equalTo($verb))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($route);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$route
|
2020-04-14 18:53:15 +03:00
|
|
|
->expects($this->once())
|
2013-08-17 13:16:48 +04:00
|
|
|
->method('action')
|
|
|
|
->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName)))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($route);
|
2014-04-09 23:57:32 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if (count($requirements) > 0) {
|
2014-04-09 23:57:32 +04:00
|
|
|
$route
|
2020-04-14 18:53:15 +03:00
|
|
|
->expects($this->once())
|
2014-04-09 23:57:32 +04:00
|
|
|
->method('requirements')
|
|
|
|
->with($this->equalTo($requirements))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($route);
|
2014-04-09 23:57:32 +04:00
|
|
|
}
|
2014-09-07 14:20:48 +04:00
|
|
|
|
|
|
|
if (count($defaults) > 0) {
|
|
|
|
$route
|
2020-04-14 18:53:15 +03:00
|
|
|
->expects($this->once())
|
2014-09-07 14:20:48 +04:00
|
|
|
->method('defaults')
|
|
|
|
->with($this->equalTo($defaults))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($route);
|
2014-09-07 14:20:48 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
return $route;
|
|
|
|
}
|
|
|
|
}
|