Blame view

sources/tests/lib/appframework/routing/RoutingTest.php 6.78 KB
a293d369c   Kload   Update sources to...
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
  <?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');
  	}
837968727   Kload   [enh] Upgrade to ...
38
39
40
41
42
43
44
45
  	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'));
  	}
a293d369c   Kload   Update sources to...
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
  	/**
  	 * @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');
  	}
837968727   Kload   [enh] Upgrade to ...
87
88
89
90
91
92
93
94
  	/**
  	 * @param string $name
  	 * @param string $verb
  	 * @param string $url
  	 * @param string $controllerName
  	 * @param string $actionName
  	 */
  	private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements=array())
a293d369c   Kload   Update sources to...
95
96
  	{
  		// route mocks
837968727   Kload   [enh] Upgrade to ...
97
  		$route = $this->mockRoute($verb, $controllerName, $actionName, $requirements);
a293d369c   Kload   Update sources to...
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
  
  		// 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
  	 */
837968727   Kload   [enh] Upgrade to ...
174
  	private function mockRoute($verb, $controllerName, $actionName, array $requirements=array())
a293d369c   Kload   Update sources to...
175
176
  	{
  		$container = new DIContainer('app1');
837968727   Kload   [enh] Upgrade to ...
177
  		$route = $this->getMock("\OC\Route", array('method', 'action', 'requirements'), array(), '', false);
a293d369c   Kload   Update sources to...
178
179
180
181
182
183
184
185
186
187
188
  		$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));
837968727   Kload   [enh] Upgrade to ...
189
190
191
192
193
194
195
196
  
  		if(count($requirements) > 0) {
  			$route
  				->expects($this->exactly(1))
  				->method('requirements')
  				->with($this->equalTo($requirements))
  				->will($this->returnValue($route));
  		}
a293d369c   Kload   Update sources to...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  		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
  
   * */