-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.php
60 lines (52 loc) · 1.82 KB
/
test.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
<?php
error_reporting(-1);
require __DIR__.'/vendor/autoload.php';
function Ω($d){static$n=1;echo'['.($n++)."] {$d}\n";}
function ø($m,$r){$_SERVER['REQUEST_METHOD']=$m;$_SERVER['REQUEST_URI']=$r;}
echo "Testing the µ PHP framework.\n";
Ω('Instances of µ can be created.');
$µ = new µ;
assert($µ instanceof µ);
Ω('Method calls on the µ are chainable.');
$µ´ = $µ->cfg('greeting', 'howdy');
assert($µ´ === $µ);
Ω('Values can be stored/retrieved in the DIC.');
assert($µ->cfg('greeting') === "howdy");
Ω('Closure values in the DIC have access to the µ and are resolved when retrieved.');
$called = 0;
$µ->cfg('greeting.upper', function (µ$app) use (&$called) {
$called++;
return strtoupper($app->cfg('greeting'));
});
$ñ = $µ->cfg('greeting.upper');
assert($called == 1) and assert($ñ === "HOWDY");
Ω('Resolved closure values in the DIC are memoized.');
$ñ = $µ->cfg('greeting.upper');
assert($called == 1) and assert($ñ === "HOWDY");
Ω('Router allows for regex expressing named params, including optional ones.');
$ç = false;
$µ = (new µ)->get('/foo/(?<bar>\w+)(?:/(?<baz>\w+))?', function ($µ, $π) use (&$ç) {
assert($µ instanceof µ);
assert($π["bar"] === "one");
assert(!isset($π["baz"]));
$ç = true;
});
ø('GET', '/foo/one');
$µ->run();
assert($ç === true);
Ω('Nothing happens when µ is run if no routes match.');
$ç = false;
$µ = (new µ)->post('/foo/(?<bar>\w+)', function () use (&$ç) {
$ç = true;
});
ø('GET', '/foo/one');
assert($ç === false);
Ω('Templating (view) system replaces variables with provided values.');
$dir = sys_get_temp_dir().'/mu-view-test';
$file = $dir.'/tpl.php';
!is_dir($dir) and mkdir($dir);
file_put_contents($file, '[<?=$foo?>]');
$ñ = (new µ)->view($dir, 'tpl', ['foo' => 'bar']);
unlink($file);
rmdir($dir);
assert($ñ === "[bar]");