forked from WP-API/WP-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testhelper.php
executable file
·82 lines (66 loc) · 2.15 KB
/
testhelper.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
<?php
/**
* Plugin Name: JSON REST API Testing Helper
* Description: Creates helpers for the testing framework (such as Code Coverage)
* Author: Ryan McCue
* Author URI: http://ryanmccue.info/
* Version: 0.1
*/
$GLOBALS['wp_json_server_testhelper'] = new WP_JSON_Server_TestHelper();
class WP_JSON_Server_TestHelper {
protected $current_test = null;
protected $coverage = null;
protected $reports = array();
public function __construct() {
add_action('init', array($this, 'startCoverage'));
add_filter('json_endpoints', array($this, 'addEndpoints'));
}
public function startCoverage() {
if ( ! isset( $_REQUEST['_jsoncurrenttest'] ) ) {
return;
}
if ( ! class_exists( 'PHP_CodeCoverage' ) ) {
return;
}
$this->reports = get_transient('json_testhelper_coverage');
if (empty($this->reports))
$this->reports = array();
$this->coverage = new PHP_CodeCoverage();
$current_test = preg_replace( '#[^\w-:]+#i', '', $_REQUEST['current_test'] );
$this->coverage->start( $current_test );
}
public function endCoverage() {
if ( ! $this->coverage ) {
return;
}
$this->coverage->end();
$this->reports[] = serialize($this->coverage);
set_transient('json_testhelper_coverage', $this->reports, 30 * MINUTE_IN_SECONDS);
}
public function addEndpoints($routes) {
$routes['/testhelper/report'] = array(
array( array( $this, 'getReports' ), WP_JSON_Server::METHOD_POST ),
);
return $routes;
}
public function getReports() {
$this->reports = get_transient('json_testhelper_coverage');
if (empty($this->reports)) {
return new WP_Error('json_testhelper_no_report', __('No report data available', 'json_testhelper'), array('status' => 400));
}
if ( ! class_exists( 'PHP_CodeCoverage' ) ) {
return new WP_Error('json_testhelper_missing_codecoverage', __('The CodeCoverage classes are missing', 'json_testhelper'), array('status' => 500));
}
$master = new PHP_CodeCoverage();
foreach ($this->reports as $report) {
$master->merge($report);
}
// Clean up
delete_transient('json_testhelper_coverage');
$data = array(
'reports' => count($this->reports),
'data' => serialize($master)
);
return $data;
}
}