-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.class.php
598 lines (526 loc) · 19.7 KB
/
test.class.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?php
/**
* This is a simple API Test-Engine. With simple JSON files you can run tests for all your RESTful API's.
*
* @author Kristof Friess
* @version 0.1
* @copyright Copyright (c) since 2014 by Kristof Friess
*/
namespace com\bp;
class APITestEngine
{
private $count = 0; // used to count the tests
private $countFails = 0; // used to count the failed tests
private $tmpPath = null; // is a writeable tmp directory
private $mockDir = null;
private $url = 'http://localhost'; // without '/' at the end
private $startTime = null; // saved the start time for time tracking
private $endTime = null; // saved the end time for time tracking
private $rounds = null; // number of rounds the test will run
private $concurrency = null; // number of request run at the same time
/**
* Initialize the test engine.
*/
public function __construct()
{
$this->tmpPath = tempnam("/tmp", "COOKIE"); //sys_get_temp_dir();
}
/**
* Set the API URL, this url is use for all tests.
* @param string $url
*/
public function setAPIUrl($url)
{
$this->url = rtrim($url, '/');
}
/**
* Set the mock dir path.
* @param string $path
*/
public function setMockDir($path)
{
if (is_dir($path))
{
$this->mockDir = rtrim($path, '/');
}
else
{
$this->logMsg('ERROR', 'The given mock path is not a directory.');
}
}
/**
* This method will return the current fails of tests
* @return int
*/
public function fails()
{
return $this->countFails;
}
/**
* This method will start the tests readed out of the given directory.
* @param string $path Directory Path with tests
*/
public function run($path, $n = 1, $c = 1)
{
$path = rtrim($path, DIRECTORY_SEPARATOR);
if (is_dir($path))
{
$this->rounds = $n;
$this->concurrency = $c;
$this->startTime = microtime(true);
for ($i=0; $i < $n; $i++)
{
if ($handle = opendir($path))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$this->testParser($path.DIRECTORY_SEPARATOR.$file);
}
}
}
closedir($handle);
}
$this->endTime = microtime(true);
}
else
{
$this->logMsg('ERROR', 'The test path should be a directory with test files.');
}
}
/**
* This method will log message ot the terminal with color
* @param string $t START, ERROR, SUCCESS or DEBUG
* @param string $msg
*/
private function logMsg($t, $msg)
{
global $colors;
if($t === 'START')
{
// create a log message with white/black colors
echo "\033[1;38m\033[40m".$msg."\033[0m"."\n";
}
else if($t === 'ERROR')
{
// create a log message with red/black colors
echo "\033[0;31m\033[40m".$msg."\033[0m"."\n";
}
else if($t === 'SUCCESS')
{
// create a log message with green/black colors
echo "\033[0;32m\033[40m".$msg."\033[0m"."\n";
}
else
{
// create a log message with light_gray/black colors
echo "\033[0;37m\033[40m".$msg."\033[0m"."\n";
}
}
/**
* This method will load a json file located in '<root_dir>/mock'.
* If the loading will fail. A empty array returns.
*
* @param string $name Name of the moc object
* @return array Dictionary of the object or an empty array
*/
private function mockObject($name)
{
try
{
$objc = file_get_contents($this->mockDir.DIRECTORY_SEPARATOR.$name.'.json');
return json_decode($objc, true);
}
catch (Exception $e)
{
$this->logMsg('ERROR', $e->getMessage());
}
return [];
}
/**
* This method will run the test and do the request to the API.
* @param string $name
* @param string $url
* @param string $method GET; POST; PUT; DELETE; ...
* @param array $data
* @param array $header Can be use to extend the default request header with other variables
* @param function $cb functoin($header, $response) { ... }
*/
private function test($name, $url, $method, $data, $header, $cb = null)
{
if (is_callable($data))
{
$cb = $data;
$data = null;
}
$this->logMsg('DEBUG', ' ');
$this->logMsg('START', "Start Test: {$name}."); ++$this->count;
$this->logMsg('DEBUG', "\tURL: {$url}");
$this->logMsg('DEBUG', "\tMETHOD: {$method}");
try
{
$mh = curl_multi_init();
$curls = array();
for ($curlIndex=0; $curlIndex < $this->concurrency; $curlIndex++)
{
// Get cURL resource
$curls[$curlIndex] = curl_init();
$curl = &$curls[$curlIndex];
$data_string = '';
// set data
if ($data !== null && (strtolower($method) == 'post' || strtolower($method) == 'put'))
{
// convert data to str
$data_string = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
}
else if($data !== null && strtolower($method) == 'get')
{
$url = $url.'?';
foreach($data as $key => $value)
{
$url = $url.$key.'='.$data[$key].'&';
}
// remove last AND
$url = substr($url, -1);
}
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Zebresel Terminal Tests',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => 1,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => $this->tmpPath,
CURLOPT_COOKIEJAR => $this->tmpPath,
CURLOPT_FOLLOWLOCATION => 1,
));
// set the correct method
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
// set header
$defaultHeader = [
'Content-Type' => 'application/json; charset=utf-8',
'Content-Length' => mb_strlen($data_string)
];
if ($header !== null && is_array($header))
{
$defaultHeader = array_merge($defaultHeader, $header);
}
$finalHeader = [];
foreach ($defaultHeader as $key => $value)
{
$finalHeader[] = "{$key}: {$value}";
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $finalHeader);
//curl_setopt($curl, CURLOPT_HTTPHEADER,array("Expect:"));
// save curl in array and add to multi request
curl_multi_add_handle($mh, $curls[$curlIndex]);
}
$before = microtime(true);
$running = NULL;
do
{
curl_multi_exec($mh,$running);
} while($running > 0);
$res = array();
foreach($curls as $index => $curl)
{
$res[] = curl_multi_getcontent($curls[$index]);
}
foreach($curls as $index => $curl)
{
curl_multi_remove_handle($mh, $curls[$index]);
}
//curl_multi_close($mh);
//return $res;
// Send the request & save response to $resp
$resp = $res[0];
// request finished
$after = microtime(true);
// Then, after your curl_exec call:
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headerStr = substr($resp, 0, $headerSize);
$headers = array();
foreach (explode("\r\n", $headerStr) as $i => $line)
{
// check line starts with HTTP (status), we will save only the last status code
if ('HTTP' === substr($line, 0, 4))
{
$headers['HTTP-Status'] = $line;
$tmpLine = explode(' ', $line);
$headers['HTTP-Code'] = $tmpLine[1];
}
else
{
$expVal = preg_split('/:\s*/', $line);
if(count($expVal) >= 2 && isset($expVal[0]) && isset($expVal[1]) )
{
$headers[$expVal[0]] = $expVal[1];
}
}
}
$body = substr($resp, $headerSize);
$json = json_decode($body, true);
$headers['body'] = $body;
if (json_last_error() != JSON_ERROR_NONE)
{
$json = array('error' => json_last_error_msg());
}
$this->logMsg('DEBUG', "\tResponse-Status: {$headers['HTTP-Status']}");
$this->logMsg('DEBUG', "\tResponse-Time: ". (($after-$before) . " sec\n"));
$result = $cb($headers, $json);
// Close request to clear up some resources
//curl_close($curl);
usleep(50);
if ($result === false)
{
++$this->countFails;
$body = "\t".str_replace("\n", "\n\t", $body)."\n";
$this->logMsg('DEBUG', $body);
$this->logMsg('ERROR', "Test: {$name} failed.");
}
else
{
$this->logMsg('SUCCESS', "Test: {$name} was successful.");
}
}
catch (Exception $e)
{
++$this->countFails;
$this->logMsg('ERROR', "Test: {$name} failed.");
}
$this->logMsg('DEBUG', ' ');
}
/**
* This mehtod will print the full test result to the terminal.
*/
public function printResult()
{
if ($this->count > 0)
{
$this->logMsg('DEBUG', ' ');
$this->logMsg('DEBUG', '------------------------------------------------------------------------------------------');
$this->logMsg('DEBUG', ' ');
$this->logMsg(($this->countFails!=0?'ERROR':'SUCCESS'), "\tTest finished with {$this->countFails} fails of {$this->count} tests.");
$this->logMsg('DEBUG', ' ');
$this->logMsg('DEBUG', "\tTest-Time: ". (($this->endTime - $this->startTime) . " sec\n"));
$this->logMsg('DEBUG', ' ');
$this->logMsg('DEBUG', '------------------------------------------------------------------------------------------');
$this->logMsg('DEBUG', ' ');
}
}
/**
* [recrusiveJsonValidation description]
* @param [type] $resp [description]
* @param [type] $valid [description]
* @param [type] &$errCount [description]
* @return [type] [description]
*/
private function recrusiveJsonValidation($resp, $valid, &$errCount)
{
foreach ($valid as $key => $value)
{
if (isset($resp[$key]))
{
if( is_array($value) && is_array($resp[$key]) )
{
$this->recrusiveJsonValidation( $resp[$key], $value, $errCount );
}
else if ( isset($resp[$key]) )
{
if (is_string($value) && strlen($value) > 0 && $value[0] === '$')
{
$operator = explode(' ', $value);
if(isset($operator[1]))
{
$value = $operator[1];
}
$operator = $operator[0];
if ($operator === '$nn')
{
if (!isset($resp[$key]))
{
++$errCount;
$this->logMsg('ERROR', "Key {$key} is null.");
}
}
elseif($operator === '$eq')
{
$value = $this->valueForKeyPath($resp, $key);
if($value != $resp[$key])
{
++$errCount;
$this->logMsg('ERROR', "Key {$key} is not equal {$value} != {$resp[$key]}.");
}
}
elseif($operator === '$ia')
{
$value = $this->valueForKeyPath($resp, $key);
if(is_array($resp[$key]))
{
++$errCount;
$this->logMsg('ERROR', "Key {$key} is not equal {$value} != {$resp[$key]}.");
}
}
}
else if($value != $resp[$key])
{
++$errCount;
$this->logMsg('ERROR', "Key {$key} is not equal {$value} != {$resp[$key]}.");
}
}
else
{
++$errCount;
$this->logMsg('ERROR', "Key {$key} is not equal {$value} != {$resp[$key]}.");
}
}
else
{
++$errCount;
$this->logMsg('ERROR', "Key {$key} not found.");
}
}
}
/**
* This method will scan a object (type array) recrusive for global saved variables and will replace them.
* @param Array &$object
*/
private function replaceGlobalVariables(&$object)
{
foreach ($object as $key => &$value)
{
if (is_string($value))
{
$value = strtr($value, $GLOBALS['params']);
}
elseif(is_array($value))
{
$this->replaceGlobalVariables($value);
}
}
}
/**
* This method will search the value inside the given dict using a keypath
* @param array &$dict
* @param string $keypath e.g. 'account.id'
* @return mix
*/
private function valueForKeyPath(&$dict, $keypath)
{
$path = explode('.', $keypath);
$result = &$dict;
foreach($path as $key)
{
// what retrieve the count of the path
if($key === '$c' && is_array($result))
{
$result = count($result);
}
else
{
$result = &$result[$key];
}
}
return $result;
}
/**
* This method will parse a given test (format json) and run them.
* @param string $path File path with the test json.
*/
private function testParser($path)
{
// check gloab params already initialized
if (!isset($GLOBALS['params']) || !is_array($GLOBALS['params']))
{
$GLOBALS['params'] = [];
}
try
{
$filecontent = file_get_contents($path);
$tests = json_decode($filecontent, true);
// no json error start tests
if (json_last_error() === JSON_ERROR_NONE)
{
foreach ($tests['tests'] as $test)
{
$requestParams = null;
if (isset($test['request_params']))
{
$requestParams = $test['request_params'];
// is string? then a mock is required
if (is_string($requestParams))
{
$requestParams = $this->mockObject($requestParams);
}
// parse request params and replace globals
$this->replaceGlobalVariables($requestParams);
}
$path = strtr($test['path'], $GLOBALS['params']);
$name = strtr($test['name'], $GLOBALS['params']);
// check is there a header?
$extendedHeader = null;
if (isset($test['header']))
{
$extendedHeader = $test['header'];
foreach ($extendedHeader as $key => $value)
{
$extendedHeader[$key] = strtr($extendedHeader[$key], $GLOBALS['params']);
}
}
$this->test($name, $this->url.$path, $test['method'], $requestParams, $extendedHeader, function($header, $resp) use ($test) {
$validation = $test['validation'];
// first check http code
if ($header['HTTP-Code'] != $validation['http_code'])
{
return false;
}
// check response values
if (isset($validation['response_params']))
{
$errCount = 0;
$this->recrusiveJsonValidation($resp, $validation['response_params'], $errCount);
if ($errCount > 0)
{
return false;
}
}
// check response values using mock
if (isset($validation['mock']))
{
$params = $this->mockObject($validation['mock']);
$errCount = 0;
$this->recrusiveJsonValidation($resp, $params, $errCount);
if ($errCount > 0)
{
return false;
}
}
// if all fine retrieve globales and save them
if (isset($test['save_global']))
{
foreach ($test['save_global'] as $value)
{
$GLOBALS['params']['{$'.$value['key'].'}'] = $this->valueForKeyPath($resp, $value['keypath']);
}
}
return true;
});
}
}
else
{
$this->logMsg('DEBUG', ' ');
$this->logMsg('ERROR', 'Can not read the json inside: '.$path);
$this->logMsg('DEBUG', json_last_error_msg());
$this->logMsg('DEBUG', ' ');
}
}
catch (Exception $e)
{
$this->logMsg('ERROR', $e->getMessage());
}
}
};