Skip to content

Commit 15b92f8

Browse files
author
Git
committed
Empty commit
1 parent 2f6b91c commit 15b92f8

File tree

13 files changed

+480
-5
lines changed

13 files changed

+480
-5
lines changed

classes/testing.class.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?
2+
3+
class Testing {
4+
private static $ClassDirectories = array("classes");
5+
private static $Classes = array();
6+
7+
/**
8+
* Initialize the testasble classes into a map keyed by class name
9+
*/
10+
public static function init() {
11+
self::load_classes();
12+
}
13+
14+
/**
15+
* Gets the class
16+
*/
17+
public static function get_classes() {
18+
return self::$Classes;
19+
}
20+
21+
/**
22+
* Loads all the classes within given directories
23+
*/
24+
private static function load_classes() {
25+
foreach (self::$ClassDirectories as $Directory) {
26+
$Directory = SERVER_ROOT . "/" . $Directory . "/";
27+
foreach (glob($Directory . "*.php") as $FileName) {
28+
self::get_class_name($FileName);
29+
}
30+
}
31+
}
32+
33+
/**
34+
* Gets the class and adds into the map
35+
*/
36+
private static function get_class_name($FileName) {
37+
$Tokens = token_get_all(file_get_contents($FileName));
38+
$IsTestable = false;
39+
$IsClass = false;
40+
41+
foreach ($Tokens as $Token) {
42+
if (is_array($Token)) {
43+
if (!$IsTestable && $Token[0] == T_DOC_COMMENT && strpos($Token[1], "@TestClass")) {
44+
$IsTestable = true;
45+
}
46+
if ($IsTestable && $Token[0] == T_CLASS) {
47+
$IsClass = true;
48+
} else if ($IsClass && $Token[0] == T_STRING) {
49+
$ReflectionClass = new ReflectionClass($Token[1]);
50+
if (count(self::get_testable_methods($ReflectionClass))) {
51+
self::$Classes[$Token[1]] = new ReflectionClass($Token[1]);
52+
}
53+
$IsTestable = false;
54+
$IsClass = false;
55+
}
56+
}
57+
}
58+
}
59+
60+
/**
61+
* Checks if class exists in the map
62+
*/
63+
public static function has_class($Class) {
64+
return array_key_exists($Class, self::$Classes);
65+
}
66+
67+
/**
68+
* Checks if class has a given testable methood
69+
*/
70+
public static function has_testable_method($Class, $Method) {
71+
$TestableMethods = self::get_testable_methods($Class);
72+
foreach($TestableMethods as $TestMethod) {
73+
if ($TestMethod->getName() === $Method) {
74+
return true;
75+
}
76+
}
77+
return false;
78+
}
79+
80+
/**
81+
* Get testable methods in a class, a testable method has a @Test
82+
*/
83+
public static function get_testable_methods($Class) {
84+
if (is_string($Class)) {
85+
$ReflectionClass = self::$Classes[$Class];
86+
} else {
87+
$ReflectionClass = $Class;
88+
}
89+
$ReflectionMethods = $ReflectionClass->getMethods();
90+
$TestableMethods = array();
91+
foreach($ReflectionMethods as $Method) {
92+
if ($Method->isPublic() && $Method->isStatic() && strpos($Method->getDocComment(), "@Test")) {
93+
$TestableMethods[] = $Method;
94+
}
95+
}
96+
return $TestableMethods;
97+
}
98+
99+
100+
/**
101+
* Get the class comment
102+
*/
103+
public static function get_class_comment($Class) {
104+
$ReflectionClass = self::$Classes[$Class];
105+
return trim(str_replace(array("@TestClass", "*", "/"), "", $ReflectionClass->getDocComment()));
106+
}
107+
108+
/**
109+
* Get the undocumented methods in a class
110+
*/
111+
public static function get_undocumented_methods($Class) {
112+
$ReflectionClass = self::$Classes[$Class];
113+
$Methods = array();
114+
foreach($ReflectionClass->getMethods() as $Method) {
115+
if (!$Method->getDocComment()) {
116+
$Methods[] = $Method;
117+
}
118+
}
119+
return $Methods;
120+
}
121+
122+
/**
123+
* Get the documented methods
124+
*/
125+
public static function get_documented_methods($Class) {
126+
$ReflectionClass = self::$Classes[$Class];
127+
$Methods = array();
128+
foreach($ReflectionClass->getMethods() as $Method) {
129+
if ($Method->getDocComment()) {
130+
$Methods[] = $Method;
131+
}
132+
}
133+
return $Methods;
134+
}
135+
136+
/**
137+
* Get all methods in a class
138+
*/
139+
public static function get_methods($Class) {
140+
return self::$Classes[$Class]->getMethods();
141+
}
142+
143+
/**
144+
* Get a method comment
145+
*/
146+
public static function get_method_comment($Method) {
147+
return trim(str_replace(array("*", "/"), "", $Method->getDocComment()));
148+
}
149+
150+
}

classes/testingview.class.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?
2+
3+
class TestingView {
4+
/**
5+
* Render the linkbox
6+
*/
7+
public static function render_linkbox($Page) { ?>
8+
<div class="linkbox">
9+
<? if ($Page != "classes") { ?>
10+
<a href="testing.php" class="brackets">Classes</a>
11+
<? }
12+
if ($Page != "comments") { ?>
13+
<a href="testing.php?action=comments" class="brackets">Comments</a>
14+
<? } ?>
15+
</div>
16+
<? }
17+
18+
/**
19+
* Render a list of classes
20+
*/
21+
public static function render_classes($Classes) { ?>
22+
<table>
23+
<tr class="colhead">
24+
<td>
25+
Class
26+
</td>
27+
<td>
28+
Testable functions
29+
</td>
30+
</tr>
31+
<? foreach($Classes as $Key => $Value) {
32+
$Doc = Testing::get_class_comment($Key);
33+
$Methods = count(Testing::get_testable_methods($Key));
34+
?>
35+
<tr>
36+
<td>
37+
<a href="testing.php?action=class&amp;name=<?=$Key?>" class="tooltip" title="<?=$Doc?>"><?=$Key?></a>
38+
</td>
39+
<td>
40+
<?=$Methods?>
41+
</td>
42+
</tr>
43+
<? } ?>
44+
</table>
45+
<? }
46+
47+
/**
48+
* Render functions in a class
49+
*/
50+
public static function render_functions($Methods) {
51+
foreach($Methods as $Index => $Method) {
52+
$ClassName = $Method->getDeclaringClass()->getName();
53+
$MethodName = $Method->getName();
54+
?>
55+
<div class="box box2">
56+
<div class="head">
57+
<span><?=self::render_method_definition($Method)?></span>
58+
<span style="float: right;">
59+
<a href="#" class="brackets" onclick="$('#method_params_<?=$Index?>').gtoggle(); return false;">Params</a>
60+
<a href="#" class="brackets run" data-gazelle-id="<?=$Index?>" data-gazelle-class="<?=$ClassName?>" data-gazelle-method="<?=$MethodName?>">Run</a>
61+
</span>
62+
</div>
63+
<div class="pad hidden" id="method_params_<?=$Index?>">
64+
<?self::render_method_params($Method);?>
65+
</div>
66+
<div class="pad hidden" id="method_results_<?=$Index?>">
67+
</div>
68+
</div>
69+
<? }
70+
}
71+
72+
/**
73+
* Render method parameters
74+
*/
75+
private static function render_method_params($Method) { ?>
76+
<table>
77+
<? foreach($Method->getParameters() as $Parameter) {
78+
$DefaultValue = $Parameter->isDefaultValueAvailable() ? $Parameter->getDefaultValue() : "";
79+
?>
80+
<tr>
81+
<td class="label">
82+
<?=$Parameter->getName()?>
83+
</td>
84+
<td>
85+
<input type="text" name="<?=$Parameter->getName()?>" value="<?=$DefaultValue?>"/>
86+
</td>
87+
</tr>
88+
<? } ?>
89+
</table>
90+
<? }
91+
92+
/**
93+
* Render the method definition
94+
*/
95+
private static function render_method_definition($Method) {
96+
$Title = "<span class='tooltip' title='" . Testing::get_method_comment($Method) . "'>" . $Method->getName() . "</span> (";
97+
foreach($Method->getParameters() as $Parameter) {
98+
$Color = "red";
99+
if ($Parameter->isDefaultValueAvailable()) {
100+
$Color = "green";
101+
}
102+
$Title .= "<span style='color: $Color'>";
103+
$Title .= "$" . $Parameter->getName();
104+
if ($Parameter->isDefaultValueAvailable()) {
105+
$Title .= " = " . $Parameter->getDefaultValue();
106+
}
107+
$Title .= "</span>";
108+
$Title .= ", ";
109+
110+
}
111+
$Title = rtrim($Title, ", ");
112+
$Title .= ")";
113+
return $Title;
114+
}
115+
116+
/**
117+
* Renders class documentation stats
118+
*/
119+
public static function render_missing_documentation($Classes) { ?>
120+
<table>
121+
<tr class="colhead">
122+
<td>
123+
Class
124+
</td>
125+
<td>
126+
Class documented
127+
</td>
128+
<td>
129+
Undocumented functions
130+
</td>
131+
<td>
132+
Documented functions
133+
</td>
134+
</tr>
135+
<? foreach($Classes as $Key => $Value) {
136+
$ClassComment = Testing::get_class_comment($Key);
137+
?>
138+
<tr>
139+
<td>
140+
<?=$Key?>
141+
</td>
142+
<td>
143+
<?=!empty($ClassComment) ? "Yes" : "No"?>
144+
<td>
145+
<?=count(Testing::get_undocumented_methods($Key))?>
146+
</td>
147+
<td>
148+
<?=count(Testing::get_documented_methods($Key))?>
149+
</td>
150+
</tr>
151+
<? } ?>
152+
</table>
153+
<? }
154+
155+
/**
156+
* Pretty print any data
157+
*/
158+
public static function render_results($Data) {
159+
$Results = '<pre><ul style="list-style-type: none">';
160+
if (is_array($Data)) {
161+
foreach ($Data as $Key => $Value){
162+
if (is_array($Value)){
163+
$Results .= '<li>' . $Key . ' => ' . self::render_results($Value) . '</li>';
164+
} else{
165+
$Results .= '<li>' . $Key . ' => ' . $Value . '</li>';
166+
}
167+
}
168+
} else {
169+
$Results .= '<li>' . $Data . '</li>';
170+
}
171+
$Results .= '</ul></pre>';
172+
echo $Results;
173+
}
174+
175+
}

classes/top10view.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static function render_linkbox($Selected) {
66
?>
77
<div class="linkbox">
88
<a href="top10.php?type=torrents" class="brackets"><?=self::get_selected_link("Torrents", $Selected == "torrents")?></a>
9-
<a href="top10.php?type=lastfm" class="brackets"><?=self::get_selected_link("Last.FM", $Selected == "lastfm")?></a>
9+
<a href="top10.php?type=lastfm" class="brackets"><?=self::get_selected_link("Last.fm", $Selected == "lastfm")?></a>
1010
<a href="top10.php?type=users" class="brackets"><?=self::get_selected_link("Users", $Selected == "users")?></a>
1111
<a href="top10.php?type=tags" class="brackets"><?=self::get_selected_link("Tags", $Selected == "tags")?></a>
1212
<a href="top10.php?type=votes" class="brackets"><?=self::get_selected_link("Favorites", $Selected == "votes")?></a>
@@ -18,8 +18,8 @@ public static function render_linkbox($Selected) {
1818
public static function render_artist_links($Selected, $View) {
1919
?>
2020
<div class="center">
21-
<a href="top10.php?type=lastfm&amp;category=weekly&amp;view=<?=$View?>" class="brackets tooltip" title="These are the artists with the most Last.FM listeners this week"><?=self::get_selected_link("Weekly Artists", $Selected == "weekly")?></a>
22-
<a href="top10.php?type=lastfm&amp;category=hyped&amp;view=<?=$View?>" class="brackets tooltip" title="These are the the fastest rising artists on Last.FM this week"><?=self::get_selected_link("Hyped Artists", $Selected == "hyped")?></a>
21+
<a href="top10.php?type=lastfm&amp;category=weekly&amp;view=<?=$View?>" class="brackets tooltip" title="These are the artists with the most Last.fm listeners this week"><?=self::get_selected_link("Weekly Artists", $Selected == "weekly")?></a>
22+
<a href="top10.php?type=lastfm&amp;category=hyped&amp;view=<?=$View?>" class="brackets tooltip" title="These are the the fastest rising artists on Last.fm this week"><?=self::get_selected_link("Hyped Artists", $Selected == "hyped")?></a>
2323

2424
</div>
2525
<?

docs/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
CHANGE LOG
22

3+
2014-01-23 by Ajax
4+
Introduced classes to test models. Classes with a @TestClass comment which include public static functions with a @Test comment will be made available to test via testing.php
5+
36
2014-01-18 by alderaan
47
Fix grammar in pop-up notifications about new Staff PMs.
58

sections/testing/ajax_run_method.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?
2+
authorize();
3+
if (!check_perms('users_mod')) {
4+
error(404);
5+
}
6+
7+
$Class = $_POST['class'];
8+
$Method = $_POST['method'];
9+
$Params = json_decode($_POST['params'], true);
10+
11+
if (!empty($Class) && !empty($Method) && Testing::has_testable_method($Class, $Method)) {
12+
if (count($Params)) {
13+
$Results = call_user_func_array(array($Class, $Method), array_values($Params));
14+
} else {
15+
$Results = call_user_func(array($Class, $Method));
16+
}
17+
TestingView::render_results($Results);
18+
}

0 commit comments

Comments
 (0)