OSDN Git Service

APIの変更にともなってテストケースを修正
[ethna/ethna.git] / test / Ethna_UrlHandler_Test.php
1 <?php
2 /**
3  *  Ethna_UrlHandler_Test.php
4  */
5
6 /**
7  *  Ethna_UrlHandlerクラスのテストケース
8  *
9  *  @access public
10  */
11 class Ethna_UrlHandler_Test extends Ethna_UnitTestBase
12 {
13     var $url_handler;
14
15     function setUp()
16     {
17         $this->url_handler =& new Ethna_UrlHandler_TestClass($this);
18     }
19
20     // {{{ $_simple_map
21     var $_simple_map = array(
22         'entrypoint' => array(
23             'test_foo_bar' => array(
24                 'path'          => 'foo/bar',
25                 'path_regexp'   => false,
26                 'path_ext'      => false,
27             ),
28         ),
29     );
30     // }}}
31
32     // {{{ $_complex_map
33     var $_complex_map = array(
34         'entrypoint' => array(
35             'test_foo_bar' => array(
36                 'path'          => 'foo/bar',
37                 'path_regexp'   => array(
38                     'dummy_index1' => '|foo/bar/([^/]*)$|',
39                     'dummy_index2' => '|foo/bar/([^/]*)/([^/]*)$|',
40                 ),
41                 'path_ext'      => array(
42                     'dummy_index1' => array(
43                         'param1'   => array(),
44                     ),
45                     'dummy_index2' => array(
46                         'param1'   => array(),
47                         'param2'   => array(),
48                     ),
49                 ),
50             ),
51         ),
52     );
53     // }}}
54
55     // {{{ $_prefix_suffix_map
56     var $_prefix_suffix_map = array(
57         'entrypoint' => array(
58             'test_foo_bar' => array(
59                 'path'          => 'foo/bar',
60                 'path_regexp'   => '|foo/bar/(.*)$|',
61                 'path_ext'      => array(
62                     'param1' => array(
63                         'url_prefix'    => 'URL-',
64                         'url_suffix'    => '-URL',
65                         'form_prefix'   => 'FORM-',
66                         'form_suffix'   => '-FORM',
67                     ),
68                 ),
69             ),
70         ),
71     );
72     // }}}
73
74     // {{{ test_requestToAction_simple
75     function test_requestToAction_simple()
76     {
77         // pathinfo から action 取得
78         $http_vars = array(
79             '__url_handler__'   => 'entrypoint',      // not empty
80             '__url_info__'      => '/foo/bar',  // null or not empty
81             'param3'            => 'value3',
82         );
83
84         $this->url_handler->action_map = $this->_simple_map;
85         $injected = $this->url_handler->requestToAction($http_vars);
86
87         // action を受け取る
88         $diff = array_diff($injected, $http_vars);
89         $this->assertEqual(count($diff), 1);
90         $this->assertEqual($diff['action_test_foo_bar'], true);
91
92         // action を受け取る以外の変化がないことを確認
93         $diff = array_diff($http_vars, $injected);
94         $this->assertEqual(count($diff), 0);
95
96
97         // action を受け取る
98         $this->url_handler->action_map = $this->_complex_map;
99         $injected = $this->url_handler->requestToAction($http_vars);
100
101         $diff = array_diff($injected, $http_vars);
102         $this->assertEqual(count($diff), 1);
103         $this->assertEqual($diff['action_test_foo_bar'], true);
104
105     }
106     // }}}
107
108     // {{{ test_requestToAction_nopathinfo
109     function test_requestToAction_nopathinfo()
110     {
111         // pathinfo なし
112         $http_vars = array(
113             '__url_handler__'   => 'entrypoint',
114             '__url_info__'      => null,
115         );
116
117         $this->url_handler->action_map = $this->_complex_map;
118         $injected = $this->url_handler->requestToAction($http_vars);
119
120         // 変化なし
121         $diff = array_diff($injected, $http_vars);
122         $this->assertEqual(count($diff), 0);
123     }
124     // }}}
125
126     // {{{ test_requestToAction_withparams1
127     function test_requestToAction_withparams1()
128     {
129         // pathinfo から action とパラメータを受け取る
130         $http_vars = array(
131             '__url_handler__'   => 'entrypoint',
132             '__url_info__'      => '/foo/bar/aaa',
133         );
134
135         // 一致する action_map がない: エラーとして array() を返す
136         $this->url_handler->action_map = $this->_simple_map;
137         $injected = $this->url_handler->requestToAction($http_vars);
138         $this->assertEqual(count($injected), 0);
139
140
141         // action とパラメータ param1 を受け取る
142         $this->url_handler->action_map = $this->_complex_map;
143         $injected = $this->url_handler->requestToAction($http_vars);
144
145         $diff = array_diff($injected, $http_vars);
146         $this->assertEqual(count($diff), 2);
147         $this->assertEqual($diff['action_test_foo_bar'], true);
148         $this->assertEqual($diff['param1'], 'aaa');
149     }
150     // }}}
151
152     // {{{ test_requestToAction_withparams2
153     function test_requestToAction_withparams2()
154     {
155         // pathinfo から action と複数のパラメータを受け取る
156         $http_vars = array(
157             '__url_handler__'   => 'entrypoint',
158             '__url_info__'      => '/foo/bar/aaa/bbb',
159         );
160
161         $this->url_handler->action_map = $this->_complex_map;
162         $injected = $this->url_handler->requestToAction($http_vars);
163
164         $diff = array_diff($injected, $http_vars);
165         $this->assertEqual(count($diff), 3);
166         $this->assertEqual($diff['action_test_foo_bar'], true);
167         $this->assertEqual($diff['param1'], 'aaa');
168         $this->assertEqual($diff['param2'], 'bbb');
169     }
170     // }}}
171
172     // {{{ test_requestToAction_withparams3
173     function test_requestToAction_withparams3()
174     {
175         // 定義された以上のパラメータがある場合
176         $http_vars = array(
177             '__url_handler__'   => 'entrypoint',
178             '__url_info__'      => '/foo/bar/aaa/bbb/ccc',
179         );
180
181         $this->url_handler->action_map = $this->_complex_map;
182         $injected = $this->url_handler->requestToAction($http_vars);
183         $this->assertEqual(count($injected), 0);
184     }
185     // }}}
186
187     // {{{ test_requestToAction_misc
188     function test_requestToAction_misc()
189     {
190         // 微妙な pathinfo のチェック
191         $http_vars = array(
192             '__url_handler__'   => 'entrypoint',
193         );
194         $this->url_handler->action_map = $this->_complex_map;
195
196         // 余分な slash が前後についている
197         $http_vars['__url_info__'] = '///foo///bar///value1///';
198         $injected = $this->url_handler->requestToAction($http_vars);
199         $diff = array_diff($injected, $http_vars);
200         $this->assertEqual($diff['action_test_foo_bar'], true);
201         $this->assertEqual($diff['param1'], 'value1');
202         $this->assertFalse(isset($diff['param2']));
203
204         // path が '/./' を含む
205         $http_vars['__url_info__'] = '/foo/bar/./value1';
206         $injected = $this->url_handler->requestToAction($http_vars);
207         $diff = array_diff($injected, $http_vars);
208         $this->assertEqual($diff['action_test_foo_bar'], true);
209         $this->assertEqual($diff['param1'], '.');
210         $this->assertEqual($diff['param2'], 'value1');
211
212         // path が '/../' を含む
213         $http_vars['__url_info__'] = '/foo/bar/../baz';
214         $injected = $this->url_handler->requestToAction($http_vars);
215         $diff = array_diff($injected, $http_vars);
216         $this->assertEqual($diff['action_test_foo_bar'], true);
217         $this->assertEqual($diff['param1'], '..');
218         $this->assertEqual($diff['param2'], 'baz');
219
220         // 長いリクエスト
221         $http_vars['__url_info__'] = '/foo/bar/' . str_repeat('a', 10000);
222         $injected = $this->url_handler->requestToAction($http_vars);
223         $diff = array_diff($injected, $http_vars);
224         $this->assertEqual($diff['action_test_foo_bar'], true);
225         $this->assertTrue(isset($diff['param1']));
226         $this->assertFalse(isset($diff['param2']));
227     }
228     // }}}
229
230     // {{{ test_requestToAction_prefix_suffix
231     function test_requestToAction_prefix_suffix()
232     {
233         $http_vars = array(
234             '__url_handler__'   => 'entrypoint',
235             '__url_info__'      => '/foo/bar/URL-value1-URL',
236             'param3'            => 'value3',
237         );
238
239         $this->url_handler->action_map = $this->_prefix_suffix_map;
240         $injected = $this->url_handler->requestToAction($http_vars);
241         $diff = array_diff($injected, $http_vars);
242         $this->assertEqual($diff['action_test_foo_bar'], true);
243         $this->assertEqual($diff['param1'], 'FORM-value1-FORM');
244     }
245     // }}}
246
247     // {{{ test_actionToRequest_simple
248     function test_actionToRequest_simple()
249     {
250         $action = 'test_foo_bar';
251         $param = array(
252             'hoge' => 'fuga',
253         );
254
255         $this->url_handler->action_map = $this->_simple_map;
256         $ret = $this->url_handler->actionToRequest($action, $param);
257         $this->assertFalse(is_null($ret));
258         list($path, $path_key) = $ret;
259
260         // action "test_foo_bar" に対応するのは "entrypoint" の "/foo/bar"
261         $this->assertEqual($path, 'entrypoint/foo/bar');
262         $this->assertTrue($path_key == array());
263     }
264     // }}}
265
266     // {{{ test_actionToRequest_unknownaction
267     function test_actionToRequest_unknownaction()
268     {
269         $action = 'test_unknown_action';
270         $param = array(
271             'hoge' => 'fuga',
272         );
273
274         $this->url_handler->action_map = $this->_simple_map;
275         $ret = $this->url_handler->actionToRequest($action, $param);
276         $this->assertTrue(is_null($ret));
277     }
278     // }}}
279
280     // {{{ test_actionToRequest_param1
281     function test_actionToRequest_param1()
282     {
283         $action = 'test_foo_bar';
284         $param = array(
285             'hoge' => 'fuga',
286             'param1' => 'value1',
287         );
288
289         $this->url_handler->action_map = $this->_complex_map;
290         list($path, $path_key) = $this->url_handler->actionToRequest($action, $param);
291         $this->assertEqual($path, 'entrypoint/foo/bar/value1');
292         $this->assertTrue($path_key == array('param1'));
293     }
294     // }}}
295
296     // {{{ test_actionToRequest_param2
297     function test_actionToRequest_param2()
298     {
299         $action = 'test_foo_bar';
300         $param = array(
301             'hoge' => 'fuga',
302             'param1' => 'value1',
303             'param2' => 'value2',
304         );
305
306         $this->url_handler->action_map = $this->_complex_map;
307         list($path, $path_key) = $this->url_handler->actionToRequest($action, $param);
308         $this->assertEqual($path, 'entrypoint/foo/bar/value1/value2');
309         $this->assertEqual($path_key, array('param1', 'param2'));
310     }
311     // }}}
312 }
313
314 class Ethna_UrlHandler_TestClass extends Ethna_UrlHandler
315 {
316     function _getPath_Entrypoint($action, $params)
317     {
318         return array('/entrypoint', array());
319     }
320
321     function _normalizerequest_Test($http_vars)
322     {
323         return $http_vars;
324     }
325
326     function _input_filter_Test($input)
327     {
328         return strtolower($input);
329     }
330
331     function _output_filter_Test($output)
332     {
333         return strtoupper($output);
334     }
335 }
336
337 ?>