forked from noodlehaus/dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatch-tests.php
More file actions
112 lines (100 loc) · 2.62 KB
/
dispatch-tests.php
File metadata and controls
112 lines (100 loc) · 2.62 KB
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
<?php
require __DIR__.'/../dispatch.php';
test_response();
test_redirect();
test_action();
test_match();
test_serve();
test_context();
test_phtml();
test_page();
test_route();
test_dispatch();
# response()
function test_response() {
is_callable(response('foo', 200, ['content-type' => 'text/plain']));
}
# redirect()
function test_redirect() {
assert(is_callable(redirect('/index')));
assert(is_callable(redirect('/index', 301)));
}
# action()
function test_action() {
$f1 = action('GET', '/index', function () {
return 'index';
});
$f2 = action('GET', '/:name/:location', function ($args) {
return $args['name'];
});
assert(is_callable($f1) && is_callable($f2));
assert(
empty($f1('POST', '/index')) &&
empty($f1('GET', '/about')) &&
empty($f2('POST', '/about')) &&
empty($f2('GET', '/about/bleh/moo'))
);
list($c1, $v1) = $f1('GET', '/index');
list($c2, $v2) = $f2('GET', '/dispatch/singapore');
assert($c1() === 'index' && empty($v1));
assert($c2($v2) === 'dispatch' && isset($v2['name'], $v2['location']));
}
# match()
function test_match() {
$v = [
action('GET', '/index', function () { return 'GET index'; }),
action('POST', '/index', function () { return 'POST index'; })
];
assert(empty(match($v, 'GET', '/about')));
list($f, $c) = match($v, 'POST', '/index');
assert(empty($c) && $f() === 'POST index');
}
# serve()
function test_serve() {
$v = [
action('GET', '/index', function () { return response('GET index'); }),
action('POST', '/index', function ($d) { return response("POST {$d}"); })
];
$r = serve($v, 'POST', '/index', 'foo');
assert(is_callable($r));
}
# context()
function test_context() {
$v = &context();
$v['actions'][] = 'foo';
$x = &context();
assert($x['actions'] === ['foo']);
array_shift($x['actions']);
assert(empty(context()['actions']));
}
# phtml
function test_phtml() {
$t = phtml(__DIR__.'/test-phtml', ['name' => 'world']);
assert($t === 'hello, world!');
}
# page()
function test_page() {
$f = page(__DIR__.'/test-phtml', ['name' => 'world']);
assert(is_callable($f) && is_callable($f()));
}
# route()
function test_route() {
route('GET', '/index', function () {
return response('hello world!', 201, ['X-Custom-Value' => 'foo']);
});
$c = context();
$m = $c['actions'][0];
assert(is_callable($m));
list($f, $v) = $m('GET', '/index');
assert(empty($v) && is_callable($f()));
}
# dispatch() - minus header and status check
function test_dispatch() {
$_SERVER = [
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/index'
];
ob_start();
dispatch();
assert(trim(ob_get_clean()) === 'hello world!');
}