forked from jeffturcotte/anchor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnchorTest.php
140 lines (117 loc) · 3.98 KB
/
AnchorTest.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
86
87
88
89
90
91
92
93
94
95
96
97
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
<?php
/**
* PHPUnit tests for Anchor.
*/
class AnchorTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass() {
require 'Anchor.php';
Anchor::authorize('AuthorizedController');
Anchor::authorize('Controller');
Anchor::hook('init', '*', 'TestController::init');
Anchor::hook('before', '*', 'TestController::before');
Anchor::hook('after', '*', 'TestController::after');
Anchor::hook('catch Exception', '*', 'TestController::catchException');
Anchor::hook('finish', '*', 'TestController::finish');
Anchor::add('/', 'TestController::home');
Anchor::add('/:class/^id/:method', '*::*');
Anchor::add('/:class/^id-:slug/:method', '*::*');
}
public function testAuthorization() {
$this->assertInternalType('object', Anchor::call('AuthorizedController::index'));
$this->assertInternalType('object', Anchor::call('TestController::index'));
$this->assertInternalType('object', Anchor::call('TestController::allowed'));
$this->assertFalse(Anchor::call('UnauthorizedController::index'));
}
public function testResolve() {
$this->assertEquals(
'TestController::home',
Anchor::resolve('/')
);
$this->assertEquals(
'TestController::dynamic',
Anchor::resolve('/test_controller/5/dynamic')
);
$this->assertEquals(
'TestController::dynamic',
Anchor::resolve('/test_controller/5-here_is_the_slug/dynamic')
);
}
public function testCall() {
$this->assertInternalType('object', Anchor::call('TestController::allowed'));
$this->assertFalse(Anchor::call('TestController::notAllowedPrivate'));
$this->assertFalse(Anchor::call('TestController::notAllowedStatic'));
$this->assertFalse(Anchor::call('TestController::__notAllowedMagic'));
}
public function testLink() {
$this->assertEquals(
'/',
Anchor::link('TestController::home')
);
$this->assertEquals(
'/test-controller/5/dynamic',
Anchor::link('TestController::dynamic id', 5)
);
$this->assertEquals(
'/test-controller/5-this_is_the_slug/dynamic',
Anchor::link('TestController::dynamic id slug', 5, 'this_is_the_slug')
);
}
/*public function testHooks() {
$data = Anchor::call('AuthorizedController::index');
print_r($data);
$this->assertEquals('ibaf', $data->test);
}*/
public function testFormat() {
$data = Anchor::call('AuthorizedController::index');
$this->assertEquals('AuthorizedController', $data->active_class);
$this->assertEquals('AuthorizedController', $data->active_short_class);
$this->assertEquals('AuthorizedController::index', $data->active_method);
$this->assertEquals('index', $data->active_short_method);
$this->assertEquals('authorized-controller/index', $data->active_full_path);
$this->assertEquals('authorized-controller', $data->active_class_path);
}
}
class UnauthorizedController {
public function index() {}
}
class AuthorizedController {
public function index($data) {
$data->active_class = Anchor::format('%c');
$data->active_short_class = Anchor::format('%C');
$data->active_method = Anchor::format('%m');
$data->active_short_method = Anchor::format('%M');
$data->active_full_path = Anchor::format('%p');
$data->active_class_path = Anchor::format('%P');
}
public function throwException() {
throw new Exception();
}
public function throwExtendingException() {
throw new ExtendingException();
}
public static function catchException() {
$data->test = 'e';
}
public static function init($data) {
$data->test = 'i';
}
public static function before($data) {
$data->test .= 'b';
}
public static function after($data) {
$data->test .= 'a';
}
public static function finish($data) {
print_r("data is now $data\n");
$data->test .= 'f';
}
}
class TestController extends AuthorizedController {
public function allowed() {}
private function notAllowedPrivate() {}
public static function notAllowedStatic() {}
public function __notAllowedMagic() {}
public function home() {}
public function dynamic() {}
}
class ExtendingException extends Exception {}