| 1 | #! /usr/bin/env php |
|---|
| 2 | <?php |
|---|
| 3 | |
|---|
| 4 | require_once(dirname($argv[0]) . "/utilities.php"); |
|---|
| 5 | require_once(IA_ROOT_DIR.'common/array_schema.php'); |
|---|
| 6 | require_once(IA_ROOT_DIR.'common/array_path.php'); |
|---|
| 7 | |
|---|
| 8 | test_cleanup(); |
|---|
| 9 | test_prepare(); |
|---|
| 10 | |
|---|
| 11 | // Test json_validate. |
|---|
| 12 | // $expected_errors contains expected errors. Errors are matched in order. |
|---|
| 13 | // If a string in $expected_errors starts with a / then it's interprested as a regular expression. |
|---|
| 14 | // |
|---|
| 15 | // FIXME: Error ordering logic is confusing. |
|---|
| 16 | function test_array_validate($data, $schema, $expected_errors) |
|---|
| 17 | { |
|---|
| 18 | $schema_errors = array_validate($schema, array_schema_get_schema()); |
|---|
| 19 | if ($schema_errors != null) { |
|---|
| 20 | log_print("Invalid schema"); |
|---|
| 21 | log_print_r($schema_errors); |
|---|
| 22 | log_die(); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | $passed = true; |
|---|
| 26 | $real_errors = array_validate($data, $schema); |
|---|
| 27 | $real_error_messages = array(); |
|---|
| 28 | for ($i = 0; $i < count($real_errors); ++$i) { |
|---|
| 29 | $real_error_messages[$i] = $real_errors[$i]['message']; |
|---|
| 30 | $real_errors[$i] = $real_errors[$i]['path']; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | // PHP is retarded and needs a strictly positive number of elements. |
|---|
| 34 | if (count($expected_errors)) { |
|---|
| 35 | $matched = array_fill(0, count($expected_errors), false); |
|---|
| 36 | } else { |
|---|
| 37 | $matched = array(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | for ($i = 0; $i < count($real_errors); ++$i) { |
|---|
| 41 | $found = false; |
|---|
| 42 | for ($j = 0; $j < count($expected_errors); ++$j) { |
|---|
| 43 | if ($matched[$j]) { |
|---|
| 44 | continue; |
|---|
| 45 | } |
|---|
| 46 | if (count($real_errors[$i]) == count($expected_errors[$j])) { |
|---|
| 47 | for ($k = 0; $k < count($real_errors[$i]); ++$k) { |
|---|
| 48 | if ($real_errors[$i][$k] != $expected_errors[$j][$k]) { |
|---|
| 49 | break; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | if ($k == count($real_errors[$i])) { |
|---|
| 53 | $matched[$j] = $found = true; |
|---|
| 54 | break; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | if (!$found) { |
|---|
| 59 | log_print("ERROR: Unmatched real error path " . array_path_join($real_errors[$i]) . |
|---|
| 60 | " Message: " . $real_error_messages[$i]); |
|---|
| 61 | $passed = false; |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | for ($j = 0; $j < count($expected_errors); ++$j) { |
|---|
| 65 | if (!$matched[$j]) { |
|---|
| 66 | log_print("ERROR: Unmatched expected error path " . |
|---|
| 67 | array_path_join($expected_errors[$j])); |
|---|
| 68 | $passed = false; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | if ($passed) { |
|---|
| 72 | log_print("OK: Test passed."); |
|---|
| 73 | } |
|---|
| 74 | return $passed; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | log_print("TEST: Sequence of string."); |
|---|
| 79 | $schema = array( |
|---|
| 80 | 'type' => 'sequence', |
|---|
| 81 | 'values' => array('type' => 'string'), |
|---|
| 82 | ); |
|---|
| 83 | test_array_validate( |
|---|
| 84 | array( |
|---|
| 85 | 'foo', 'bar', 'baz' |
|---|
| 86 | ), |
|---|
| 87 | $schema, |
|---|
| 88 | array( |
|---|
| 89 | ) |
|---|
| 90 | ); |
|---|
| 91 | test_array_validate( |
|---|
| 92 | array( |
|---|
| 93 | 'foo', 123, 'baz' |
|---|
| 94 | ), |
|---|
| 95 | $schema, |
|---|
| 96 | array( |
|---|
| 97 | array(1), |
|---|
| 98 | ) |
|---|
| 99 | ); |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | log_print("TEST: mapping of scalar"); |
|---|
| 103 | $schema = array( |
|---|
| 104 | 'type' => 'struct', |
|---|
| 105 | 'fields' => array( |
|---|
| 106 | 'name' => array('type' => 'string', 'null' => false), |
|---|
| 107 | 'email' => array('type' => 'string', 'pattern' => '/@/'), |
|---|
| 108 | 'age' => array('type' => 'int'), |
|---|
| 109 | 'birth' => array('type' => 'date'), |
|---|
| 110 | ), |
|---|
| 111 | ); |
|---|
| 112 | test_array_validate( |
|---|
| 113 | array( |
|---|
| 114 | 'name' => 'foo', |
|---|
| 115 | 'email' => 'foo@mail.com', |
|---|
| 116 | 'age' => 20, |
|---|
| 117 | 'birth' => '1985-01-01', |
|---|
| 118 | ), |
|---|
| 119 | $schema, |
|---|
| 120 | array( |
|---|
| 121 | ) |
|---|
| 122 | ); |
|---|
| 123 | test_array_validate( |
|---|
| 124 | array( |
|---|
| 125 | 'name' => 'foo', |
|---|
| 126 | 'email' => 'foo(at)mail.com', |
|---|
| 127 | 'age' => 'twenty', |
|---|
| 128 | 'birth' => 'Jun 01, 1985', |
|---|
| 129 | ), |
|---|
| 130 | $schema, |
|---|
| 131 | array( |
|---|
| 132 | array('email'), |
|---|
| 133 | array('age'), |
|---|
| 134 | array('birth'), |
|---|
| 135 | ) |
|---|
| 136 | ); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | log_print("TEST: sequence of mapping."); |
|---|
| 140 | $schema = array( |
|---|
| 141 | 'type' => 'sequence', |
|---|
| 142 | 'values' => array( |
|---|
| 143 | 'type' => 'struct', |
|---|
| 144 | 'sealed' => true, |
|---|
| 145 | 'fields' => array( |
|---|
| 146 | 'name' => array('type' => 'string', 'null' => false), |
|---|
| 147 | 'email' => array('type' => 'string', 'null' => true), |
|---|
| 148 | ), |
|---|
| 149 | ), |
|---|
| 150 | ); |
|---|
| 151 | test_array_validate( |
|---|
| 152 | array( |
|---|
| 153 | array('name' => 'foo', 'email' => 'foo@mail.com'), |
|---|
| 154 | array('name' => 'bar', 'email' => 'bar@mail.net'), |
|---|
| 155 | array('name' => 'baz', 'email' => 'foo@mail.org'), |
|---|
| 156 | ), |
|---|
| 157 | $schema, |
|---|
| 158 | array( |
|---|
| 159 | ) |
|---|
| 160 | ); |
|---|
| 161 | test_array_validate( |
|---|
| 162 | array( |
|---|
| 163 | array('name' => 'foo', 'email' => 'foo@mail.com'), |
|---|
| 164 | array('naem' => 'bar', 'email' => 'bar@mail.net'), |
|---|
| 165 | array('name' => 'baz', 'mail' => 'foo@mail.org'), |
|---|
| 166 | ), |
|---|
| 167 | $schema, |
|---|
| 168 | array( |
|---|
| 169 | array(1, 'naem'), |
|---|
| 170 | array(1, 'name'), |
|---|
| 171 | array(2, 'mail'), |
|---|
| 172 | ) |
|---|
| 173 | ); |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | log_print("TEST: mapping of sequence of mapping, valid"); |
|---|
| 177 | $schema = array( |
|---|
| 178 | 'type' => 'struct', |
|---|
| 179 | 'fields' => array( |
|---|
| 180 | 'company' => array('type' => 'string', 'null' => false), |
|---|
| 181 | 'email' => array('type' => 'string'), |
|---|
| 182 | 'employees' => array( |
|---|
| 183 | 'type' => 'sequence', |
|---|
| 184 | 'values' => array( |
|---|
| 185 | 'type' => 'struct', |
|---|
| 186 | 'sealed' => 'true', |
|---|
| 187 | 'fields' => array( |
|---|
| 188 | 'code' => array('type' => 'int', 'null' => false), |
|---|
| 189 | 'name' => array('type' => 'string', 'null' => false), |
|---|
| 190 | 'email' => array('type' => 'string', 'null' => true), |
|---|
| 191 | ), |
|---|
| 192 | ), |
|---|
| 193 | ), |
|---|
| 194 | ), |
|---|
| 195 | ); |
|---|
| 196 | test_array_validate( |
|---|
| 197 | array( |
|---|
| 198 | 'company' => 'Kuwata lab.', |
|---|
| 199 | 'email' => 'webmaster@kuwata-lab.com', |
|---|
| 200 | 'employees' => array( |
|---|
| 201 | array('code' => 101, 'name' => 'foo', 'email' => 'foo@kuwata-lab.com'), |
|---|
| 202 | array('code' => 102, 'name' => 'bar', 'email' => 'bar@kuwata-lab.com'), |
|---|
| 203 | ) |
|---|
| 204 | ), |
|---|
| 205 | $schema, |
|---|
| 206 | array( |
|---|
| 207 | ) |
|---|
| 208 | ); |
|---|
| 209 | test_array_validate( |
|---|
| 210 | array( |
|---|
| 211 | 'company' => 'Kuwata lab.', |
|---|
| 212 | 'email' => 'webmaster@kuwata-lab.com', |
|---|
| 213 | 'employees' => array( |
|---|
| 214 | array('code' => 'A101', 'name' => 'foo', 'email' => 'foo@kuwata-lab.com'), |
|---|
| 215 | array('code' => 102, 'name' => 'bar', 'mail' => 'bar@kuwata-lab.com'), |
|---|
| 216 | ) |
|---|
| 217 | ), |
|---|
| 218 | $schema, |
|---|
| 219 | array( |
|---|
| 220 | array('employees', 0, 'code'), |
|---|
| 221 | array('employees', 1, 'mail'), |
|---|
| 222 | ) |
|---|
| 223 | ); |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | log_print('TEST: Rule examples'); |
|---|
| 227 | $schema = array( |
|---|
| 228 | 'type' => 'sequence', |
|---|
| 229 | 'values' => array( |
|---|
| 230 | 'type' => 'struct', |
|---|
| 231 | 'sealed' => true, |
|---|
| 232 | 'fields' => array( |
|---|
| 233 | 'name' => array('type' => 'string', 'null' => false), |
|---|
| 234 | 'email' => array('type' => 'string', 'null' => false, 'pattern' => '/@/'), |
|---|
| 235 | 'password' => array( |
|---|
| 236 | 'type' => 'string', |
|---|
| 237 | 'length' => array('max' => 16, 'min' => 8), |
|---|
| 238 | ), |
|---|
| 239 | 'age' => array( |
|---|
| 240 | 'type' => 'int', |
|---|
| 241 | 'range' => array('max' => 30, 'min' => 18), |
|---|
| 242 | ), |
|---|
| 243 | 'blood' => array( |
|---|
| 244 | 'type' => 'string', |
|---|
| 245 | 'enum' => array('A', 'B', 'O', 'AB'), |
|---|
| 246 | ), |
|---|
| 247 | 'birth' => array('type' => 'date'), |
|---|
| 248 | 'memo' => array('type' => 'any', 'null' => 'true'), |
|---|
| 249 | ), |
|---|
| 250 | ), |
|---|
| 251 | ); |
|---|
| 252 | test_array_validate( |
|---|
| 253 | array( |
|---|
| 254 | array( |
|---|
| 255 | 'name' => 'foo', |
|---|
| 256 | 'email' => 'foo@mail.com', |
|---|
| 257 | 'password' => 'xxx123456', |
|---|
| 258 | 'age' => 20, |
|---|
| 259 | 'blood' => 'A', |
|---|
| 260 | 'birth' => '1985-01-01', |
|---|
| 261 | ), |
|---|
| 262 | array( |
|---|
| 263 | 'name' => 'bar', |
|---|
| 264 | 'email' => 'bar@mail.net', |
|---|
| 265 | 'password' => 'xxx123456', |
|---|
| 266 | 'age' => 25, |
|---|
| 267 | 'blood' => 'AB', |
|---|
| 268 | 'birth' => '1980-01-01', |
|---|
| 269 | ), |
|---|
| 270 | ), |
|---|
| 271 | $schema, |
|---|
| 272 | array( |
|---|
| 273 | ) |
|---|
| 274 | ); |
|---|
| 275 | test_array_validate( |
|---|
| 276 | array( |
|---|
| 277 | array( |
|---|
| 278 | 'name' => 'foo', |
|---|
| 279 | 'email' => 'foo(at)mail.com', |
|---|
| 280 | 'password' => 'xxx123', |
|---|
| 281 | 'age' => 'twenty', |
|---|
| 282 | 'blood' => 'a', |
|---|
| 283 | 'birth' => '1985-01-01', |
|---|
| 284 | ), |
|---|
| 285 | array( |
|---|
| 286 | 'given-name' => 'bar', |
|---|
| 287 | 'family-name' => 'Bar', |
|---|
| 288 | 'email' => 'bar@mail.net', |
|---|
| 289 | 'password' => 'xxx123456', |
|---|
| 290 | 'age' => 15, |
|---|
| 291 | 'blood' => 'AB', |
|---|
| 292 | 'birth' => '1980/01/01', |
|---|
| 293 | ), |
|---|
| 294 | ), |
|---|
| 295 | $schema, |
|---|
| 296 | array( |
|---|
| 297 | array(0, 'email'), |
|---|
| 298 | array(0, 'password'), |
|---|
| 299 | array(0, 'age'), |
|---|
| 300 | array(0, 'blood'), |
|---|
| 301 | array(1, 'given-name'), |
|---|
| 302 | array(1, 'family-name'), |
|---|
| 303 | array(1, 'age'), |
|---|
| 304 | array(1, 'birth'), |
|---|
| 305 | array(1, 'name'), |
|---|
| 306 | ) |
|---|
| 307 | ); |
|---|
| 308 | |
|---|
| 309 | test_cleanup(); |
|---|
| 310 | |
|---|