/
assertions.php
169 lines (143 loc) · 5.28 KB
/
assertions.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @package tests
* @subpackage assertions
*/
# ============================================================================ #
# ASSERTIONS #
# ============================================================================ #
/**
* assert_true
*
* @param string $value
* @param string $message
* @return boolean
*/
function assert_true($value, $message = '<1> should be TRUE')
{
test_run_assertion();
return assert('$value === TRUE; //'.$message);
}
function assert_false($value, $message = '<1> should be FALSE')
{
test_run_assertion();
return assert('$value === FALSE; //'.$message);
}
function assert_null($value, $message = '<1> should be NULL')
{
test_run_assertion();
return assert('$value === NULL; //'.$message);
}
function assert_not_null($value, $message = '<1> should not be NULL')
{
test_run_assertion();
return assert('$value !== NULL; //'.$message);
}
function assert_empty($value, $message = '<1> should be empty')
{
test_run_assertion();
return assert('empty($value); //'.$message);
}
function assert_not_empty($value, $message = '<1> should not be empty')
{
test_run_assertion();
return assert('!empty($value); //'.$message);
}
function assert_equal($expected, $value, $message = '<1> should be equal to <2>')
{
test_run_assertion();
return assert('$expected == $value; //'.$message);
}
function assert_not_equal($expected, $value, $message = '<1> should not equal to <2>')
{
test_run_assertion();
return assert('$expected != $value; //'.$message);
}
function assert_identical($expected, $value, $message = '<1> should be identical to <2>')
{
test_run_assertion();
return assert('$expected === $value; //'.$message);
}
function assert_not_identical($expected, $value, $message = '<1> should not be identical to <2>')
{
test_run_assertion();
return assert('$expected !== $value; //'.$message);
}
function assert_match($pattern, $string, $message = '<2> expected to match regular expression <1>') {
test_run_assertion();
return assert('preg_match($pattern, $string); //'.$message);
}
function assert_no_match($pattern, $string, $message = '<2> expected to not match regular expression <1>') {
test_run_assertion();
return assert('!preg_match($pattern, $string); //'.$message);
}
function assert_type($type, $value, $message = '<1> is not of type <2>') {
test_run_assertion();
$predicate = 'is_' . strtolower(is_string($type) ? $type : gettype($type));
return assert('$predicate($value); //'.$message);
}
function assert_instance_of($class, $object, $message = '<2> is not an instance of class <1>') {
test_run_assertion();
return assert('$object instanceof $class; //'.$message);
}
function assert_length_of($value, $length, $message = '<1> expected to be of length <2>') {
test_run_assertion();
$count = is_string($value) ? 'strlen' : 'count';
return assert('$count($value) == $length; //'.$message);
}
function assert_trigger_error($callable, $args = array(), $message = '<1> should trigger an error') {
test_run_assertion();
$trigger_errors = count($GLOBALS["limonade"]["test_errors"]);
set_error_handler("test_error_handler");
$result = call_user_func_array($callable, $args);
restore_error_handler();
return assert('$trigger_errors < count($GLOBALS["limonade"]["test_errors"]); //'.$message);
}
# TODO add web browser assertions assert_http_get, assert_http_response... as in SimpleTest (http://www.simpletest.org/en/web_tester_documentation.html)
function assert_header($response, $expected_name, $expected_value = null, $message = "expected header '%s' to be equal to '%s' but received '%s: %s'")
{
# see assert_header in http://github.com/fnando/voodoo-test/blob/f3b0994ef138a6ba94d5e7cef6c1fb1720797a86/lib/assertions.php
$headers = preg_split("/^\s*$/ms", $response);
//var_dump($headers);
$headers = preg_replace("/\s*$/sm", "", $headers[0]);
//var_dump($headers);
$regex_header = str_replace("/", "\\/", $expected_name);
$regex_header = str_replace(".", "\\.", $regex_header);
$header = $expected_name;
# from http://www.faqs.org/rfcs/rfc2616
# Field names are case-insensitive
if ($expected_value) {
$regex = "/^{$regex_header}:(.*?)$/ism";
$header .= ": {$expected_value}";
} else {
$regex = "/^{$regex_header}(:.*?)?$/ism";
}
$has_header = preg_match($regex, $headers, $matches);
$sent_header = trim((string)$matches[1]);
if(empty($sent_header))
{
if(is_null($expected_value))
{
$message = "expected header '%s' but header has not been sent";
}
else
{
$message = "expected header '%s' to be equal to '%s' but header has not been sent";
}
$message = sprintf($message, $expected_name, $expected_value);
return assert("false; //".$message);
}
else if($expected_value)
{
$message = sprintf($message, $expected_name, $expected_value, $expected_name, $sent_header);
return assert('$expected_value && $sent_header == $expected_value; //'.$message);
}
return assert("true; //");
}
function assert_response($response, $expected_status, $message)
{
// if (preg_match('/HTTP\/(\d+\.\d+)\s+(\d+)/i', $header_line, $matches)) {
// $this->_http_version = $matches[1];
// $this->_response_code = $matches[2];
// }
}