Commit | Line | Data |
---|---|---|
41e571e3 FB |
1 | <?php |
2 | /*************************************************************************** | |
e92ecb8c | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
41e571e3 FB |
4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | require_once dirname(__FILE__) . '/../include/test.inc.php'; | |
23 | __autoload('platal'); | |
24 | ||
25 | class TestPage extends PlPage | |
26 | { | |
27 | public function run() { | |
28 | } | |
29 | } | |
30 | ||
31 | class EngineTest extends PlTestCase | |
32 | { | |
ed4f7de0 | 33 | public static function blahCallback(PlPage $page) |
41e571e3 FB |
34 | { |
35 | return 'blah'; | |
36 | } | |
37 | ||
ed4f7de0 | 38 | public static function blihCallback(PlPage $page) |
41e571e3 FB |
39 | { |
40 | $args = func_get_args(); | |
41 | array_shift($args); | |
42 | return 'blih-' . implode('-', $args); | |
43 | } | |
44 | ||
ed4f7de0 | 45 | public static function fooCallback(PlPage $page, $arg1 = null) |
41e571e3 FB |
46 | { |
47 | if (is_null($arg1)) { | |
48 | return 'foo'; | |
49 | } else { | |
50 | return 'foo-' . $arg1; | |
51 | } | |
52 | } | |
53 | ||
ed4f7de0 | 54 | public static function barCallback(PlPage $page, $arg1 = null, $arg2 = null) |
41e571e3 FB |
55 | { |
56 | if (is_null($arg1)) { | |
57 | return 'bar'; | |
58 | } else if (is_null($arg2)) { | |
59 | return 'bar-' . $arg1; | |
60 | } else { | |
61 | return 'bar-' . $arg1 . '-' . $arg2; | |
62 | } | |
63 | } | |
64 | ||
65 | ||
66 | public static function hookProvider() | |
67 | { | |
68 | return array( | |
69 | array(PL_WIKI, new PlWikiHook(), array('a')), | |
70 | array(PL_WIKI, new PlWikiHook(), array('a', 'b')), | |
71 | array(PL_WIKI, new PlWikiHook(), array('a', 'b', 'c')), | |
72 | ||
73 | array('blah', new PlStdHook(array('EngineTest', 'blahCallback')), array('a')), | |
74 | array('blah', new PlStdHook(array('EngineTest', 'blahCallback')), array('a', 'b')), | |
75 | array('blah', new PlStdHook(array('EngineTest', 'blahCallback')), array('a', 'b', 'c')), | |
76 | ||
77 | array('blih-', new PlStdHook(array('EngineTest', 'blihCallback')), array('a')), | |
78 | array('blih-b', new PlStdHook(array('EngineTest', 'blihCallback')), array('a', 'b')), | |
79 | array('blih-b-c', new PlStdHook(array('EngineTest', 'blihCallback')), array('a', 'b', 'c')), | |
80 | ||
81 | array('foo', new PlStdHook(array('EngineTest', 'fooCallback')), array('a')), | |
82 | array('foo-b', new PlStdHook(array('EngineTest', 'fooCallback')), array('a', 'b')), | |
83 | array('foo-b', new PlStdHook(array('EngineTest', 'fooCallback')), array('a', 'b', 'c')), | |
84 | ||
85 | array('bar', new PlStdHook(array('EngineTest', 'barCallback')), array('a')), | |
86 | array('bar-b', new PlStdHook(array('EngineTest', 'barCallback')), array('a', 'b')), | |
87 | array('bar-b-c', new PlStdHook(array('EngineTest', 'barCallback')), array('a', 'b', 'c')), | |
88 | ); | |
89 | } | |
90 | ||
91 | /** | |
92 | * @dataProvider hookProvider | |
93 | */ | |
94 | public function testHook($res, $hook, $args) | |
95 | { | |
96 | $page = new TestPage(); | |
97 | $this->assertEquals($res, $hook->call($page, $args)); | |
98 | } | |
99 | ||
100 | ||
101 | public static function dispatchProvider() | |
102 | { | |
103 | return array( | |
104 | array('blih-', 'test', 'test'), | |
105 | array('blih-', 'test', 'test/'), | |
106 | array('blih-machin', 'test', 'test/machin'), | |
107 | array('blih-machin-truc', 'test', 'test/machin/truc'), | |
108 | ||
109 | array('blih-hiboo', 'test', 'test/hiboo'), | |
110 | array('foo', 'test/coucou', 'test/coucou'), | |
111 | array('foo-', 'test/coucou', 'test/coucou/'), | |
112 | array('foo-blah', 'test/coucou', 'test/coucou/blah'), | |
113 | array('foo-blah', 'test/coucou', 'test/coucou/blah/truc') | |
114 | ); | |
115 | } | |
116 | ||
117 | /** | |
118 | * @dataProvider dispatchProvider | |
119 | */ | |
120 | public function testDispatch($res, $expmatched, $path) | |
121 | { | |
122 | $tree = new PlHookTree(); | |
123 | $tree->addChild(array('test'), new PlStdHook(array('EngineTest', 'blihCallback'))); | |
124 | $tree->addChild(array('test1'), new PlStdHook(array('EngineTest', 'blahCallback'))); | |
125 | $tree->addChild(array('tes'), new PlStdHook(array('EngineTest', 'blahCallback'))); | |
126 | $tree->addChild(array('test', 'coucou'), new PlStdHook(array('EngineTest', 'fooCallback'))); | |
127 | $tree->addChild(array('test', 'hook'), new PlStdHook(array('EngineTest', 'barCallback'))); | |
128 | ||
129 | $page = new TestPage(); | |
9e394323 | 130 | $p = explode('/', $path); |
41e571e3 FB |
131 | list($hook, $matched, $remain, $aliased) = $tree->findChild($p); |
132 | $matched = join('/', $matched); | |
133 | $this->assertEquals($expmatched, $matched); | |
134 | array_unshift($remain, $matched); | |
135 | $this->assertEquals($res, $hook->call($page, $remain)); | |
136 | } | |
137 | } | |
138 | ||
139 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
140 | ?> |