2013-08-17 09:16:48 +00:00
|
|
|
<?php
|
|
|
|
|
2016-05-18 16:40:34 +00:00
|
|
|
namespace Test\AppFramework\Routing;
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
use OC\AppFramework\DependencyInjection\DIContainer;
|
2016-05-18 16:40:34 +00:00
|
|
|
use OC\AppFramework\Routing\RouteActionHandler;
|
|
|
|
use OC\AppFramework\Routing\RouteConfig;
|
2013-08-17 09:16:48 +00:00
|
|
|
|
2014-11-10 22:30:38 +00:00
|
|
|
class RoutingTest extends \Test\TestCase
|
2013-08-17 09:16:48 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
public function testSimpleRoute()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
public function testSimpleOCSRoute() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2016-08-01 14:37:48 +00:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2016-07-14 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
public function testSimpleRouteWithMissingVerb()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'folders#open', 'url' => '/folders/{folderId}/open')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
public function testSimpleOCSRouteWithMissingVerb() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2016-08-01 14:37:48 +00:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'GET', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2016-07-14 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
public function testSimpleRouteWithLowercaseVerb()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open');
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
public function testSimpleOCSRouteWithLowercaseVerb() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2016-08-01 14:37:48 +00:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open');
|
2016-07-14 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:57:32 +00:00
|
|
|
public function testSimpleRouteWithRequirements()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => array('something'))
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array('something'));
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
public function testSimpleOCSRouteWithRequirements() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => ['something']]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2016-08-01 14:37:48 +00:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', ['something']);
|
2016-07-14 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-07 10:20:48 +00:00
|
|
|
public function testSimpleRouteWithDefaults()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', array(), 'defaults' => array('param' => 'foobar'))
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array(), array('param' => 'foobar'));
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
|
|
|
|
public function testSimpleOCSRouteWithDefaults() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'defaults' => ['param' => 'foobar']]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2016-08-01 14:37:48 +00:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], ['param' => 'foobar']);
|
2016-07-14 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 11:00:41 +00:00
|
|
|
public function testSimpleRouteWithPostfix()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array(), array(), '_something');
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
public function testSimpleOCSRouteWithPostfix() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something']
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2016-08-01 14:37:48 +00:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'folders.open', 'DELETE', '/apps/app1/folders/{folderId}/open', 'FoldersController', 'open', [], [], '_something');
|
2016-07-14 18:45:19 +00:00
|
|
|
}
|
2014-04-09 19:57:32 +00:00
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \UnexpectedValueException
|
|
|
|
*/
|
|
|
|
public function testSimpleRouteWithBrokenName()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
|
|
|
|
));
|
|
|
|
|
|
|
|
// router mock
|
2016-07-10 12:17:26 +00:00
|
|
|
$router = $this->getMockBuilder('\OC\Route\Router')
|
|
|
|
->setMethods(['create'])
|
2016-07-10 12:19:55 +00:00
|
|
|
->setConstructorArgs([$this->getMockBuilder('\OCP\ILogger')->getMock()])
|
2016-07-10 12:17:26 +00:00
|
|
|
->getMock();
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \UnexpectedValueException
|
|
|
|
*/
|
|
|
|
public function testSimpleOCSRouteWithBrokenName() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]];
|
|
|
|
|
|
|
|
// router mock
|
|
|
|
$router = $this->getMockBuilder('\OC\Route\Router')
|
|
|
|
->setMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->getMockBuilder('\OCP\ILogger')->getMock()])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
public function testSimpleRouteWithUnderScoreNames()
|
|
|
|
{
|
|
|
|
$routes = array('routes' => array(
|
|
|
|
array('name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
|
|
|
|
}
|
|
|
|
|
2016-07-14 18:45:19 +00:00
|
|
|
public function testSimpleOCSRouteWithUnderScoreNames() {
|
|
|
|
$routes = ['ocs' => [
|
|
|
|
['name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete']
|
|
|
|
]];
|
|
|
|
|
2016-08-01 14:37:48 +00:00
|
|
|
$this->assertSimpleOCSRoute($routes, 'admin_folders.open_current', 'DELETE', '/apps/app1/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
|
2016-07-14 18:45:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
public function testResource()
|
|
|
|
{
|
2014-06-26 12:02:20 +00:00
|
|
|
$routes = array('resources' => array('account' => array('url' => '/accounts')));
|
2013-08-17 09:16:48 +00:00
|
|
|
|
2014-06-26 12:20:35 +00:00
|
|
|
$this->assertResource($routes, 'account', '/accounts', 'AccountController', 'id');
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testResourceWithUnderScoreName()
|
|
|
|
{
|
|
|
|
$routes = array('resources' => array('admin_accounts' => array('url' => '/admin/accounts')));
|
|
|
|
|
2014-06-26 12:20:35 +00:00
|
|
|
$this->assertResource($routes, 'admin_accounts', '/admin/accounts', 'AdminAccountsController', 'id');
|
2013-08-17 09:16:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $verb
|
|
|
|
* @param string $url
|
|
|
|
* @param string $controllerName
|
|
|
|
* @param string $actionName
|
|
|
|
*/
|
2014-11-19 11:00:41 +00:00
|
|
|
private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements=array(), array $defaults=array(), $postfix='')
|
2013-08-17 09:16:48 +00:00
|
|
|
{
|
2014-11-19 11:00:41 +00:00
|
|
|
if ($postfix) {
|
|
|
|
$name .= $postfix;
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
// route mocks
|
2015-07-24 11:43:50 +00:00
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
// router mock
|
2016-07-10 12:17:26 +00:00
|
|
|
$router = $this->getMockBuilder('\OC\Route\Router')
|
|
|
|
->setMethods(['create'])
|
2016-07-10 12:19:55 +00:00
|
|
|
->setConstructorArgs([$this->getMockBuilder('\OCP\ILogger')->getMock()])
|
2016-07-10 12:17:26 +00:00
|
|
|
->getMock();
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
// we expect create to be called once:
|
|
|
|
$router
|
|
|
|
->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $name), $this->equalTo($url))
|
|
|
|
->will($this->returnValue($route));
|
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
2016-07-14 18:45:19 +00: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,
|
|
|
|
array $requirements=array(),
|
|
|
|
array $defaults=array(),
|
|
|
|
$postfix='')
|
|
|
|
{
|
|
|
|
if ($postfix) {
|
|
|
|
$name .= $postfix;
|
|
|
|
}
|
|
|
|
|
|
|
|
// route mocks
|
|
|
|
$container = new DIContainer('app1');
|
|
|
|
$route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
|
|
|
|
|
|
|
|
// router mock
|
|
|
|
$router = $this->getMockBuilder('\OC\Route\Router')
|
|
|
|
->setMethods(['create'])
|
|
|
|
->setConstructorArgs([$this->getMockBuilder('\OCP\ILogger')->getMock()])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// we expect create to be called once:
|
|
|
|
$router
|
|
|
|
->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('ocs.app1.' . $name), $this->equalTo($url))
|
|
|
|
->will($this->returnValue($route));
|
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$config = new RouteConfig($container, $router, $routes);
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
2014-02-19 08:31:54 +00:00
|
|
|
/**
|
|
|
|
* @param string $resourceName
|
|
|
|
* @param string $url
|
|
|
|
* @param string $controllerName
|
|
|
|
* @param string $paramName
|
|
|
|
*/
|
2013-08-17 09:16:48 +00:00
|
|
|
private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName)
|
|
|
|
{
|
|
|
|
// router mock
|
2016-07-10 12:17:26 +00:00
|
|
|
$router = $this->getMockBuilder('\OC\Route\Router')
|
|
|
|
->setMethods(['create'])
|
2016-07-10 12:19:55 +00:00
|
|
|
->setConstructorArgs([$this->getMockBuilder('\OCP\ILogger')->getMock()])
|
2016-07-10 12:17:26 +00:00
|
|
|
->getMock();
|
2013-08-17 09:16:48 +00:00
|
|
|
|
|
|
|
// route mocks
|
2015-07-24 11:43:50 +00: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 09:16:48 +00: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))
|
|
|
|
->will($this->returnValue($indexRoute));
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam))
|
|
|
|
->will($this->returnValue($showRoute));
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.create'), $this->equalTo($url))
|
|
|
|
->will($this->returnValue($createRoute));
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(3))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam))
|
|
|
|
->will($this->returnValue($updateRoute));
|
|
|
|
|
|
|
|
$router
|
|
|
|
->expects($this->at(4))
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam))
|
|
|
|
->will($this->returnValue($destroyRoute));
|
|
|
|
|
|
|
|
// load route configuration
|
|
|
|
$config = new RouteConfig($container, $router, $yaml);
|
|
|
|
|
|
|
|
$config->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-24 11:43:50 +00:00
|
|
|
* @param DIContainer $container
|
2014-02-28 12:53:41 +00:00
|
|
|
* @param string $verb
|
|
|
|
* @param string $controllerName
|
|
|
|
* @param string $actionName
|
2015-07-24 11:43:50 +00:00
|
|
|
* @param array $requirements
|
|
|
|
* @param array $defaults
|
2013-08-17 09:16:48 +00:00
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject
|
|
|
|
*/
|
2015-07-24 11:43:50 +00:00
|
|
|
private function mockRoute(
|
|
|
|
DIContainer $container,
|
|
|
|
$verb,
|
|
|
|
$controllerName,
|
|
|
|
$actionName,
|
|
|
|
array $requirements=array(),
|
|
|
|
array $defaults=array()
|
|
|
|
) {
|
2016-07-10 12:17:26 +00:00
|
|
|
$route = $this->getMockBuilder('\OC\Route\Route')
|
|
|
|
->setMethods(['method', 'action', 'requirements', 'defaults'])
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2013-08-17 09:16:48 +00:00
|
|
|
$route
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('method')
|
|
|
|
->with($this->equalTo($verb))
|
|
|
|
->will($this->returnValue($route));
|
|
|
|
|
|
|
|
$route
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('action')
|
|
|
|
->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName)))
|
|
|
|
->will($this->returnValue($route));
|
2014-04-09 19:57:32 +00:00
|
|
|
|
|
|
|
if(count($requirements) > 0) {
|
|
|
|
$route
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('requirements')
|
|
|
|
->with($this->equalTo($requirements))
|
|
|
|
->will($this->returnValue($route));
|
|
|
|
}
|
2014-09-07 10:20:48 +00:00
|
|
|
|
|
|
|
if (count($defaults) > 0) {
|
|
|
|
$route
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('defaults')
|
|
|
|
->with($this->equalTo($defaults))
|
|
|
|
->will($this->returnValue($route));
|
|
|
|
}
|
|
|
|
|
2013-08-17 09:16:48 +00:00
|
|
|
return $route;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
#
|
|
|
|
# sample routes.yaml for ownCloud
|
|
|
|
#
|
|
|
|
# the section simple describes one route
|
|
|
|
|
|
|
|
routes:
|
|
|
|
- name: folders#open
|
|
|
|
url: /folders/{folderId}/open
|
|
|
|
verb: GET
|
|
|
|
# controller: name.split()[0]
|
|
|
|
# action: name.split()[1]
|
|
|
|
|
|
|
|
# for a resource following actions will be generated:
|
|
|
|
# - index
|
|
|
|
# - create
|
|
|
|
# - show
|
|
|
|
# - update
|
|
|
|
# - destroy
|
|
|
|
# - new
|
|
|
|
resources:
|
|
|
|
accounts:
|
|
|
|
url: /accounts
|
|
|
|
|
|
|
|
folders:
|
|
|
|
url: /accounts/{accountId}/folders
|
|
|
|
# actions can be used to define additional actions on the resource
|
|
|
|
actions:
|
|
|
|
- name: validate
|
|
|
|
verb: GET
|
|
|
|
on-collection: false
|
|
|
|
|
|
|
|
* */
|