-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDispatcherTest.php
85 lines (64 loc) · 2.11 KB
/
DispatcherTest.php
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
<?php
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao <[email protected]>
* @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/classes/Hello.php';
class DispatcherTest extends PHPUnit_Framework_TestCase
{
/**
* @var \flight\core\Dispatcher
*/
private $dispatcher;
function setUp(){
$this->dispatcher = new \flight\core\Dispatcher();
}
// Map a closure
function testClosureMapping(){
$this->dispatcher->set('map1', function(){
return 'hello';
});
$result = $this->dispatcher->run('map1');
$this->assertEquals('hello', $result);
}
// Map a function
function testFunctionMapping(){
$this->dispatcher->set('map2', function(){
return 'hello';
});
$result = $this->dispatcher->run('map2');
$this->assertEquals('hello', $result);
}
// Map a class method
function testClassMethodMapping(){
$h = new Hello();
$this->dispatcher->set('map3', array($h, 'sayHi'));
$result = $this->dispatcher->run('map3');
$this->assertEquals('hello', $result);
}
// Map a static class method
function testStaticClassMethodMapping(){
$this->dispatcher->set('map4', array('Hello', 'sayBye'));
$result = $this->dispatcher->run('map4');
$this->assertEquals('goodbye', $result);
}
// Run before and after filters
function testBeforeAndAfter() {
$this->dispatcher->set('hello', function($name){
return "Hello, $name!";
});
$this->dispatcher->hook('hello', 'before', function(&$params, &$output){
// Manipulate the parameter
$params[0] = 'Fred';
});
$this->dispatcher->hook('hello', 'after', function(&$params, &$output){
// Manipulate the output
$output .= " Have a nice day!";
});
$result = $this->dispatcher->run('hello', array('Bob'));
$this->assertEquals('Hello, Fred! Have a nice day!', $result);
}
}