This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestsManager.class.php
103 lines (93 loc) · 3.23 KB
/
TestsManager.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
<?php
/**
* Class TestManager
* Manages the addition of iformation for javascript testing.
* For example with zombiejs and node.
* @version 1.0.0
*/
class TestManager{
/**
* @var array stores the key\value pair for the current tests set
*/
private $tests;
/**
* @var TestManager stores the singleton instance
*/
private static $instance = null;
/**
* Initializes a new object
*/
private function __construct(){
add_action('wp_footer', array(&$this, 'on_print_footer_script'), 9999);
}
/**
* Retrieves the singleton instance
* @return TestManager the instance
*/
public static function get_instance(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
/**
* Adds a test with given key and value
* @param $key string the key
* @param $value mixed the value
* @return $this for chainability
*/
public function add_parameter($key, $value){
$this->tests[$key] = $value;
return $this;
}
/**
* Adds tests always useful: page generation time, number of queries and requested url
* @return $this for chainability
*/
public function add_defaults(){
add_action('wp_footer', array(&$this, 'add_page_generation_time'), 9998, 0);
add_action('wp_footer', array(&$this, 'add_number_of_queries'), 9998, 0);
add_action('wp_footer', array(&$this, 'add_requested_uri'), 9998, 0);
return $this;
}
/**
* Adds the page generation time to the current set of tests
* Must be hooked to the end of process (for example wp_footer)
* @param string $key the key, default pageGenerationTime
* @return $this for chainability
*/
public function add_page_generation_time($key='pageGenerationTime'){
global $timestart, $timeend;
$timeend = microtime( true );
$timetotal = $timeend - $timestart;
$this->add_parameter($key, $timetotal);
return $this;
}
/**
* Adds the number of queries used to generate the current page.
* Must be hooked to the end of process (for example wp_footer)
* @param string $key the key, default numberOfQueries
* @return $this for chainability
*/
public function add_number_of_queries($key='numberOfQueries'){
global $wpdb;
$this->add_parameter($key, $wpdb->num_queries);
return $this;
}
/**
* Add the requested url to the tests set
* @param string $key the key, default requestedUrl
* @return $this for chainability
*/
public function add_requested_uri($key='requestedUrl'){
$this->add_parameter($key, 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
return $this;
}
/**
* Callback for wp_footer, prints the window.tests variable
* Useful to read the information on a external node.js environment (ex zombiejs)
*/
public function on_print_footer_script(){
echo HtmlHelper::script('window.tests = ' . json_encode($this->tests) . ';');
}
}