-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.php
More file actions
146 lines (114 loc) · 5.11 KB
/
test.php
File metadata and controls
146 lines (114 loc) · 5.11 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
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
141
142
143
144
145
146
<?php
use mindplay\readable;
use function mindplay\testies\{ test, run, configure, eq, ok };
ini_set('zend.exception_ignore_args', '0'); // enable capture of exception arguments
require dirname(__DIR__) . '/vendor/autoload.php';
require __DIR__ . '/fixtures.php';
test(
"can format values",
function () {
$unknown = fopen(__FILE__, "r");
fclose($unknown); // closed file resources become "unknown types" in php
$file = fopen(__FILE__, "r");
eq(readable::value(array(1, 2, 3)), "[1, 2, 3]");
eq(readable::value(array('foo' => 'bar', 'baz' => 'bat')), '["foo" => "bar", "baz" => "bat"]');
eq(readable::value(true), "true");
eq(readable::value(false), "false");
eq(readable::value(null), "null");
eq(readable::value(123), "123");
eq(readable::value(0.42), "0.42");
eq(readable::value(0.12345678), "~0.123457");
eq(readable::value("hello"), '"hello"');
eq(readable::value("hell\"o"), '"hell\"o"');
eq(readable::value(new \stdClass()), '{object}');
eq(readable::value(new TestClass()), '{TestClass}');
eq(readable::value($file), '{stream}');
eq(readable::value([new TestClass(), 'instanceMethod']), '{TestClass}->instanceMethod()');
eq(readable::value(['TestClass', 'staticMethod']), 'TestClass::staticMethod()');
eq(readable::value(empty_closure()), '{Closure in test/fixtures.php(22)}');
eq(readable::value(new InvokableTestClass()), '{InvokableTestClass}');
eq(readable::value($unknown), '{unknown type}');
eq(readable::values(["foo", true, 1]), '"foo", true, 1');
eq(readable::values(["hello" => "world"]), '"hello" => "world"');
eq(readable::typeof(true), "bool");
eq(readable::typeof(false), "bool");
eq(readable::typeof(null), "null");
eq(readable::typeof(null), "null");
eq(readable::typeof(123), "int");
eq(readable::typeof(1.23), "float");
eq(readable::typeof("foo"), "string");
eq(readable::typeof([]), "array");
eq(readable::typeof(['TestClass', 'staticMethod']), "callable");
eq(readable::typeof(new \stdClass), "object");
eq(readable::typeof(new TestClass()), "TestClass");
eq(readable::typeof($file), "resource");
eq(readable::typeof($unknown), "unknown");
eq(readable::callback(new InvokableTestClass()), '{InvokableTestClass}->__invoke()');
eq(readable::callback([new TestClass(), 'instanceMethod']), '{TestClass}->instanceMethod()');
eq(readable::callback(['TestClass', 'staticMethod']), 'TestClass::staticMethod()');
eq(readable::callback(empty_closure()), '{Closure in test/fixtures.php(22)}');
eq(readable::callback('is_array'), 'is_array()');
fclose($file);
readable::$max_string_length = 10;
eq(readable::value('0123456789'), '"0123456789"');
eq(readable::value('01234567890'), '"0123456789...[11]"');
eq(readable::path(__FILE__), "test/test.php");
eq(readable::path("/somewhere/else"), "/somewhere/else");
foreach (readable::$severity_names as $value => $name) {
eq(readable::severity($value), $name);
eq(readable::error($value), $name);
}
eq(readable::severity(0), "{unknown error-code}");
$test = new TraceTest();
try {
$test->outer("hello");
} catch (Exception $e) {
// caught
}
/**
* @var Exception $e
*/
ok(isset($e));
ok($e instanceof Exception);
$summary = readable::error($e);
ok(
preg_match('/^Exception with message: got hello in .*fixtures.php\(\d+\)$/', $summary) === 1,
"exception summary has expected format",
$summary
);
$trace = array_slice($e->getTrace(), 0, 3);
$closure_trace_format = PHP_VERSION_ID >= 80400
? '{closure:TraceTest::inner():34}' // PHP 8.4 has an improved closure trace format
: '{closure}';
$expected_trace = <<<TRACE
1. test/fixtures.php:38 TraceTest->{$closure_trace_format}()
2. test/fixtures.php:29 TraceTest->inner("hello")
3. test/test.php:83 TraceTest->outer("hello")
TRACE;
eq(
readable::trace($trace, true, true),
$expected_trace,
"stack-trace has expected format"
);
$long_trace = array_fill(0, 10, $trace[2]);
$expected_long_trace = <<<TRACE
1. test/test.php:83 TraceTest->outer()
2. test/test.php:83 TraceTest->outer()
3. test/test.php:83 TraceTest->outer()
4. test/test.php:83 TraceTest->outer()
5. test/test.php:83 TraceTest->outer()
6. test/test.php:83 TraceTest->outer()
7. test/test.php:83 TraceTest->outer()
8. test/test.php:83 TraceTest->outer()
9. test/test.php:83 TraceTest->outer()
10. test/test.php:83 TraceTest->outer()
TRACE;
eq(
readable::trace($long_trace, false, true),
$expected_long_trace,
"long stack-traces are corrently indented"
);
}
);
configure()->enableCodeCoverage(__DIR__ . '/build/clover.xml', dirname(__DIR__) . '/src');
exit(run());