Tests
../
Create each of the following files and add a cronjob for runtests.php.
vars.php
<?
// CHANGEABLE VALUES
$apidir = "http://api.geekweb.org/psn/getinfos/unstable/"; // your api directory for the tests
$email = "your-m@il-addr.ess"; // your email for error reporting messages
// GENERAL VARIABLES
$thisurl = "http://".$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"];
$report = "";
$testssuccessful = 0;
$teststotal = 0;
$psnid = "Geekathome";
$apipath = $apidir."out.php?psnid=".$psnid;
$json = json_decode(file_get_contents($apipath."&out=json"),true);
?>
functions.php
<?
function MailTestReports() {
global $apidir,$report,$teststotal,$testssuccessful,$errrepfile,$email;
if ($report!="") {
$reportmsg = "--- Test from ".time() . " (".$testssuccessful."/".$teststotal.") ---";
$reportmsg = $reportmsg . "\n".$report."\n";
$mailtext = $reportmsg."API directory: ".$apidir;
mail($email,"GetInfos Tests",$mailtext);
echo (ereg_replace("\n","<br />",$mailtext));
}
}
function AddError($function) { global $report; $report .= $function."\n"; }
?>
tests.php
<?
function TestIsOutputAvailable() {
global $teststotal,$testssuccessful,$apipath;
$teststotal++;
if (file_get_contents($apipath."&cat=none")=="") { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
function TestIsJsonOutputWellFormed() {
global $teststotal,$testssuccessful,$json;
$teststotal++;
if (!is_array($json['info'])) { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
function TestIsXmlOutputWellFormed() {
global $teststotal,$testssuccessful,$apipath;
$teststotal++;
$xmlstring = file_get_contents($apipath."&out=xml");
if (!simplexml_load_string($xmlstring)) { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
function TestWidgetByUs() {
global $teststotal,$testssuccessful,$json,$psnid;
$teststotal++;
$url = "http://pid.us.playstation.com/user/".$psnid.".jpg";
if ($json['widget']['US']['img'] != $url) {
AddError(__FUNCTION__); return FALSE;
}
if (file_get_contents($url, NULL, NULL, 0, 1) == null) {
AddError(__FUNCTION__); return FALSE;
}
$testssuccessful++;
return TRUE;
}
function TestWidgetByEu() {
global $teststotal,$testssuccessful,$json,$psnid;
$teststotal++;
$url = "http://mypsn.eu.playstation.com/psn/profile/".$psnid.".png";
if ($json['widget']['EU']['img'] != $url) {
AddError(__FUNCTION__); return FALSE;
}
if (file_get_contents($url, NULL, NULL, 0, 1) == null) {
AddError(__FUNCTION__); return FALSE;
}
$testssuccessful++;
return TRUE;
}
function TestTrophyOverviewByUs() {
global $teststotal,$testssuccessful,$json;
$teststotal++;
$trophies = $json['trophy']['US'];
if (
$trophies['platinum'] < 7 ||
$trophies['gold'] < 38 ||
$trophies['silver'] < 88 ||
$trophies['bronze'] < 329 ||
$trophies['total'] < 462 ||
$trophies['level'] < 10 ||
$trophies['percent'] < 12
) { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
function TestTrophyOverviewByEuWidget() {
global $teststotal,$testssuccessful,$json;
$teststotal++;
$trophies = $json['trophy']['EU'];
if (
$trophies['platinum'] < 7 ||
$trophies['gold'] < 38 ||
$trophies['silver'] < 86 ||
$trophies['bronze'] < 313 ||
$trophies['total'] < 444 ||
$trophies['level'] < 9 ||
$trophies['percent'] < 97
) { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
function TestGameTrophyOverview() {
global $teststotal,$testssuccessful,$json;
$teststotal++;
$game = $json['trophy']['US']['game']['582987-MotorStorm-Pacific-Rift'];
if (
$game['name'] != "MotorStorm: Pacific Rift" ||
$game['percent'] != 100 ||
$game['platinum'] != 1 ||
$game['gold'] != 4 ||
$game['silver'] != 11 ||
$game['bronze'] != 50
) { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
function TestCountryByEuWidget() {
global $teststotal,$testssuccessful,$json;
$teststotal++;
$country = $json['country']['EU'];
if (
$country['hash'] != "3f502352c76dd499384cc409a8876bfa" ||
$country['cc'] != "CH" ||
$country['name'] != "Switzerland"
) { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
function TestGameByEuWidget() {
global $teststotal,$testssuccessful,$json;
$teststotal++;
$country = $json['game']['EU'];
if (
$country['hash'] != "1c0171d587178248911c7aaf7be1c972" ||
$country['gid'] != "MSPR" ||
$country['name'] != "MotorStorm Pacific Rift"
) { AddError(__FUNCTION__); return FALSE; }
$testssuccessful++;
return TRUE;
}
?>
runtests.php
<?
/* === INCLUDES ==================================================================================== */
include_once("vars.php");
include_once("functions.php");
include_once("tests.php");
/* === DO THE TESTS ================================================================================ */
TestIsOutputAvailable();
TestIsJsonOutputWellFormed();
TestIsXmlOutputWellFormed();
TestWidgetByUs();
TestWidgetByEu();
TestTrophyOverviewByUs();
TestTrophyOverviewByEuWidget();
TestGameTrophyOverview();
TestCountryByEuWidget();
TestGameByEuWidget();
/* === SEND REPORT BY MAIL ========================================================================= */
MailTestReports();
?>