1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
<?php
namespace OC\AppFramework\Routing;
use OC\AppFramework\DependencyInjection\DIContainer;
use OC\AppFramework\routing\RouteConfig;
class RouteConfigTest extends \PHPUnit_Framework_TestCase
{
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');
}
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');
}
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');
}
/**
* @expectedException \UnexpectedValueException
*/
public function testSimpleRouteWithBrokenName()
{
$routes = array('routes' => array(
array('name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
));
// router mock
$router = $this->getMock("\OC_Router", array('create'));
// load route configuration
$container = new DIContainer('app1');
$config = new RouteConfig($container, $router, $routes);
$config->register();
}
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');
}
public function testResource()
{
$routes = array('resources' => array('accounts' => array('url' => '/accounts')));
$this->assertResource($routes, 'accounts', '/accounts', 'AccountsController', 'accountId');
}
public function testResourceWithUnderScoreName()
{
$routes = array('resources' => array('admin_accounts' => array('url' => '/admin/accounts')));
$this->assertResource($routes, 'admin_accounts', '/admin/accounts', 'AdminAccountsController', 'adminAccountId');
}
private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName)
{
// route mocks
$route = $this->mockRoute($verb, $controllerName, $actionName);
// router mock
$router = $this->getMock("\OC_Router", array('create'));
// 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
$container = new DIContainer('app1');
$config = new RouteConfig($container, $router, $routes);
$config->register();
}
private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName)
{
// router mock
$router = $this->getMock("\OC_Router", array('create'));
// route mocks
$indexRoute = $this->mockRoute('GET', $controllerName, 'index');
$showRoute = $this->mockRoute('GET', $controllerName, 'show');
$createRoute = $this->mockRoute('POST', $controllerName, 'create');
$updateRoute = $this->mockRoute('PUT', $controllerName, 'update');
$destroyRoute = $this->mockRoute('DELETE', $controllerName, 'destroy');
$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
$container = new DIContainer('app1');
$config = new RouteConfig($container, $router, $yaml);
$config->register();
}
/**
* @param $verb
* @param $controllerName
* @param $actionName
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function mockRoute($verb, $controllerName, $actionName)
{
$container = new DIContainer('app1');
$route = $this->getMock("\OC_Route", array('method', 'action'), array(), '', false);
$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));
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
* */
|