Hoa central
ClassMethod.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Dispatcher as LUT;
40 use Hoa\Router;
41 use Hoa\Test;
42 
52 {
54  {
55  $this
56  ->given(
57  $router = new MockRouter(),
58  $router->get(
59  'a',
60  '(?<_call>' . preg_quote(__CLASS__) . ') ' .
61  '(?<_able>dispatchedMethod) ' .
62  '(?<foo>foo) ' .
63  '(?<bar>bar)'
64  ),
65  $this->route(
66  $router,
67  __CLASS__ . ' dispatchedMethod foo bar'
68  ),
69 
70  $dispatcher = new LUT\ClassMethod(),
71  $dispatcher->getParameters()->setParameters([
72  'synchronous.call' => '(:call:U:)',
73  'synchronous.able' => '(:able:)'
74  ])
75  )
76  ->when($dispatcher->dispatch($router));
77  }
78 
80  {
81  $this
82  ->given(
83  $router = new MockRouter(),
84  $router->get(
85  'a',
86  '(?<foo>foo) (?<bar>bar)',
87  __CLASS__,
88  'dispatchedMethod'
89  ),
90  $this->route($router, 'foo bar'),
91 
92  $dispatcher = new LUT\ClassMethod(),
93  $dispatcher->getParameters()->setParameters([
94  'synchronous.call' => '(:call:U:)',
95  'synchronous.able' => '(:able:)'
96  ])
97  )
98  ->when($dispatcher->dispatch($router));
99  }
100 
101  public function case_asynchronous()
102  {
103  $this
104  ->given(
105  $router = new MockRouter(),
106  $router->get(
107  'a',
108  '(?<foo>foo) (?<bar>bar)',
109  __CLASS__,
110  'dispatchedMethod'
111  ),
112  $router->asynchronous = true,
113  $this->route($router, 'foo bar'),
114 
115  $dispatcher = new LUT\ClassMethod(),
116  $dispatcher->getParameters()->setParameters([
117  'asynchronous.call' => '(:call:U:)',
118  'asynchronous.able' => '(:able:)Async'
119  ])
120  )
121  ->when($dispatcher->dispatch($router));
122  }
123 
124  public function case_class_not_found()
125  {
126  $this
127  ->given(
128  $router = new MockRouter(),
129  $router->get(
130  'a',
131  '(?<foo>foo) (?<bar>bar)',
132  __CLASS__ . '_NOT_FOUND',
133  'dispatchedMethod'
134  ),
135  $this->route($router, 'foo bar'),
136 
137  $dispatcher = new LUT\ClassMethod(),
138  $dispatcher->getParameters()->setParameters([
139  'synchronous.call' => '(:call:U:)',
140  'synchronous.able' => '(:able:)'
141  ])
142  )
143  ->exception(function () use ($dispatcher, $router) {
144 
145  $dispatcher->dispatch($router);
146  })
147  ->isInstanceOf('Hoa\Dispatcher\Exception');
148  }
149 
150  public function case_method_not_found()
151  {
152  $this
153  ->given(
154  $router = new MockRouter(),
155  $router->get(
156  'a',
157  'foo',
158  __CLASS__,
159  'dispatchedMethod_NOT_FOUND'
160  ),
161  $this->route($router, 'foo'),
162 
163  $dispatcher = new LUT\ClassMethod(),
164  $dispatcher->getParameters()->setParameters([
165  'synchronous.call' => '(:call:U:)',
166  'synchronous.able' => '(:able:)'
167  ])
168  )
169  ->exception(function () use ($dispatcher, $router) {
170 
171  $dispatcher->dispatch($router);
172  })
173  ->isInstanceOf('Hoa\Dispatcher\Exception');
174  }
175 
176  public function case_kit_dnew()
177  {
178  $this
179  ->given(
180  $router = new MockRouter(),
181  $router->get(
182  'a',
183  'foo',
184  __CLASS__,
185  'dispatchedMethodWithKit'
186  ),
187  $this->route($router, 'foo'),
188 
189  $dispatcher = new LUT\ClassMethod(),
190  $dispatcher->getParameters()->setParameters([
191  'synchronous.call' => '(:call:U:)',
192  'synchronous.able' => '(:able:)'
193  ]),
194 
195  $dispatcher->setKitName(__NAMESPACE__ . '\MockKit')
196  )
197  ->when($dispatcher->dispatch($router));
198  }
199 
200  public function case_an_argument_is_missing()
201  {
202  $this
203  ->given(
204  $router = new MockRouter(),
205  $router->get(
206  'a',
207  '(?<foo>foo)',
208  __CLASS__,
209  'dispatchedMethod'
210  ),
211  $this->route($router, 'foo'),
212 
213  $dispatcher = new LUT\ClassMethod(),
214  $dispatcher->getParameters()->setParameters([
215  'synchronous.call' => '(:call:U:)',
216  'synchronous.able' => '(:able:)'
217  ])
218  )
219  ->exception(function () use ($dispatcher, $router) {
220 
221  $dispatcher->dispatch($router);
222  })
223  ->isInstanceOf('Hoa\Dispatcher\Exception');
224  }
225 
227  {
228  $this
229  ->given(
230  $router = new MockRouter(),
231  $router->get(
232  'a',
233  '(?<foo>foo)',
234  __CLASS__,
235  'dispatchedMethodOptional'
236  ),
237  $this->route($router, 'foo'),
238 
239  $dispatcher = new LUT\ClassMethod(),
240  $dispatcher->getParameters()->setParameters([
241  'synchronous.call' => '(:call:U:)',
242  'synchronous.able' => '(:able:)'
243  ])
244  )
245  ->when($dispatcher->dispatch($router));
246  }
247 
248  protected function route(Router $router, $uri, Array $extraVariables = [])
249  {
250  $router->route($uri);
251  $theRule = &$router->getTheRule();
252  $theRule[$router::RULE_VARIABLES]['test'] = $this;
253 
254  foreach ($extraVariables as $name => $value) {
255  $theRule[$router::RULE_VARIABLES][$name] = $value;
256  }
257 
258  return $router;
259  }
260 
261  public function dispatchedMethod($test, $foo, $bar)
262  {
263  $test
264  ->string($foo)
265  ->isEqualTo('foo')
266  ->string($bar)
267  ->isEqualTo('bar');
268  }
269 
270  public function dispatchedMethodAsync($test, $foo, $bar)
271  {
272  $test
273  ->boolean(true)
274  ->isTrue();
275 
276  return $this->dispatchedMethod($test, $foo, $bar);
277  }
278 
279  public function dispatchedMethodOptional($test, $foo, $bar = null)
280  {
281  $test
282  ->string($foo)
283  ->isEqualTo('foo')
284  ->variable($bar)
285  ->isNull();
286  }
287 
288  public function dispatchedMethodWithKit($test, $_this)
289  {
290  $test
291  ->object($_this)
292  ->isInstanceOf(__NAMESPACE__ . '\MockKit')
293  ->boolean($_this->hasBeenConstructed)
294  ->isTrue();
295  }
296 }
297 
298 class MockRouter extends Router\Cli
299 {
300  public $asynchronous = false;
301 
302  public function isAsynchronous()
303  {
304  return $this->asynchronous;
305  }
306 }
307 
308 class MockKit extends LUT\Kit
309 {
310  public $hasBeenConstructed = false;
311 
312  public function construct()
313  {
314  $this->hasBeenConstructed = true;
315  }
316 }
route(Router $router, $uri, Array $extraVariables=[])
dispatchedMethodOptional($test, $foo, $bar=null)
$dispatcher
Definition: Hoa.php:98
dispatchedMethodAsync($test, $foo, $bar)