testsuite.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 800 行 · 第 1/2 页
PHP
800 行
<?php
/* $Id: testsuite.php,v 1.65 2007/02/25 18:45:19 ggiunta Exp $ */
include(getcwd().'/parse_args.php');
require_once('xmlrpc.inc');
require_once('xmlrpcs.inc');
require_once('xmlrpc_wrappers.inc');
require_once 'phpunit.php';
require_once 'PHPUnit/TestDecorator.php';
// let testuite run for the needed time
if ((int)ini_get('max_execution_time') < 180)
ini_set('max_execution_time', 180);
$suite = new PHPUnit_TestSuite();
// array with list of failed tests
$failed_tests = array();
class LocalhostTests extends PHPUnit_TestCase
{
var $client = null;
var $method = 'http';
var $timeout = 10;
var $request_compression = null;
var $accepted_compression = '';
function fail($message = '')
{
PHPUnit_TestCase::fail($message);
// save in global var that this particular test has failed
// (but only if not called from subclass objects / multitests)
if (function_exists('debug_backtrace') && strtolower(get_class($this)) == 'localhosttests')
{
global $failed_tests;
$trace = debug_backtrace();
for ($i = 0; $i < count($trace); $i++)
{
if (strpos($trace[$i]['function'], 'test') === 0)
{
$failed_tests[$trace[$i]['function']] = true;
break;
}
}
}
}
function setUp()
{
global $DEBUG, $LOCALSERVER, $URI;
$server = split(':', $LOCALSERVER);
if(count($server) > 1)
{
$this->client=&new xmlrpc_client($URI, $server[0], $server[1]);
}
else
{
$this->client=&new xmlrpc_client($URI, $LOCALSERVER);
}
if($DEBUG)
{
$this->client->setDebug($DEBUG);
}
$this->client->request_compression = $this->request_compression;
$this->client->accepted_compression = $this->accepted_compression;
}
function send($msg, $errrorcode=0, $return_response=false)
{
$r = $this->client->send($msg, $this->timeout, $this->method);
// for multicall, return directly array of responses
if(is_array($r))
{
return $r;
}
$this->assertEquals($r->faultCode(), $errrorcode, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
if(!$r->faultCode())
{
if($return_response)
return $r;
else
return $r->value();
}
else
{
return null;
}
}
function testString()
{
$sendstring="here are 3 \"entities\": < > &" .
"and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
" - isn't that great? \\\"hackery\\\" at it's best " .
" also don't want to miss out on \$item[0]. ".
"The real weird stuff follows: CRLF here".chr(13).chr(10).
"a simple CR here".chr(13).
"a simple LF here".chr(10).
"and then LFCR".chr(10).chr(13).
"last but not least weird names: G黱ter, El鑞e, and an xml comment closing tag: -->";
$f=new xmlrpcmsg('examples.stringecho', array(
new xmlrpcval($sendstring, 'string')
));
$v=$this->send($f);
if($v)
{
// when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
// so we relax our tests...
$l1 = strlen($sendstring);
$l2 = strlen($v->scalarval());
if ($l1 == $l2)
$this->assertEquals($sendstring, $v->scalarval());
else
$this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
}
}
function testAddingDoubles()
{
// note that rounding errors mean i
// keep precision to sensible levels here ;-)
$a=12.13; $b=-23.98;
$f=new xmlrpcmsg('examples.addtwodouble',array(
new xmlrpcval($a, 'double'),
new xmlrpcval($b, 'double')
));
$v=$this->send($f);
if($v)
{
$this->assertEquals($a+$b,$v->scalarval());
}
}
function testAdding()
{
$f=new xmlrpcmsg('examples.addtwo',array(
new xmlrpcval(12, 'int'),
new xmlrpcval(-23, 'int')
));
$v=$this->send($f);
if($v)
{
$this->assertEquals(12-23, $v->scalarval());
}
}
function testInvalidNumber()
{
$f=new xmlrpcmsg('examples.addtwo',array(
new xmlrpcval('fred', 'int'),
new xmlrpcval("\"; exec('ls')", 'int')
));
$v=$this->send($f);
/// @todo a fault condition should be generated here
/// by the server, which we pick up on
if($v)
{
$this->assertEquals(0, $v->scalarval());
}
}
function testBoolean()
{
$f=new xmlrpcmsg('examples.invertBooleans', array(
new xmlrpcval(array(
new xmlrpcval(true, 'boolean'),
new xmlrpcval(false, 'boolean'),
new xmlrpcval(1, 'boolean'),
new xmlrpcval(0, 'boolean'),
//new xmlrpcval('true', 'boolean'),
//new xmlrpcval('false', 'boolean')
),
'array'
)));
$answer='0101';
$v=$this->send($f);
if($v)
{
$sz=$v->arraysize();
$got='';
for($i=0; $i<$sz; $i++)
{
$b=$v->arraymem($i);
if($b->scalarval())
{
$got.='1';
}
else
{
$got.='0';
}
}
$this->assertEquals($answer, $got);
}
}
function testBase64()
{
$sendstring='Mary had a little lamb,
Whose fleece was white as snow,
And everywhere that Mary went
the lamb was sure to go.
Mary had a little lamb
She tied it to a pylon
Ten thousand volts went down its back
And turned it into nylon';
$f=new xmlrpcmsg('examples.decode64',array(
new xmlrpcval($sendstring, 'base64')
));
$v=$this->send($f);
if($v)
{
if (strlen($sendstring) == strlen($v->scalarval()))
$this->assertEquals($sendstring, $v->scalarval());
else
$this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendstring), $v->scalarval());
}
}
function testCountEntities()
{
$sendstring = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
$f = new xmlrpcmsg('validator1.countTheEntities',array(
new xmlrpcval($sendstring, 'string')
));
$v = $this->send($f);
if($v)
{
$got = '';
$expected = '37210';
$expect_array = array('ctLeftAngleBrackets','ctRightAngleBrackets','ctAmpersands','ctApostrophes','ctQuotes');
while(list(,$val) = each($expect_array))
{
$b = $v->structmem($val);
$got .= $b->me['int'];
}
$this->assertEquals($expected, $got);
}
}
function _multicall_msg($method, $params)
{
$struct['methodName'] = new xmlrpcval($method, 'string');
$struct['params'] = new xmlrpcval($params, 'array');
return new xmlrpcval($struct, 'struct');
}
function testServerMulticall()
{
// We manually construct a system.multicall() call to ensure
// that the server supports it.
// NB: This test will NOT pass if server does not support system.multicall.
// Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
$good1 = $this->_multicall_msg(
'system.methodHelp',
array(php_xmlrpc_encode('system.listMethods')));
$bad = $this->_multicall_msg(
'test.nosuch',
array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
$recursive = $this->_multicall_msg(
'system.multicall',
array(new xmlrpcval(array(), 'array')));
$good2 = $this->_multicall_msg(
'system.methodSignature',
array(php_xmlrpc_encode('system.listMethods')));
$arg = new xmlrpcval(
array($good1, $bad, $recursive, $good2),
'array'
);
$f = new xmlrpcmsg('system.multicall', array($arg));
$v = $this->send($f);
if($v)
{
//$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
$this->assertTrue($v->arraysize() == 4, "bad number of return values");
$r1 = $v->arraymem(0);
$this->assertTrue(
$r1->kindOf() == 'array' && $r1->arraysize() == 1,
"did not get array of size 1 from good1"
);
$r2 = $v->arraymem(1);
$this->assertTrue(
$r2->kindOf() == 'struct',
"no fault from bad"
);
$r3 = $v->arraymem(2);
$this->assertTrue(
$r3->kindOf() == 'struct',
"recursive system.multicall did not fail"
);
$r4 = $v->arraymem(3);
$this->assertTrue(
$r4->kindOf() == 'array' && $r4->arraysize() == 1,
"did not get array of size 1 from good2"
);
}
}
function testClientMulticall1()
{
// NB: This test will NOT pass if server does not support system.multicall.
$this->client->no_multicall = false;
$good1 = new xmlrpcmsg('system.methodHelp',
array(php_xmlrpc_encode('system.listMethods')));
$bad = new xmlrpcmsg('test.nosuch',
array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
$recursive = new xmlrpcmsg('system.multicall',
array(new xmlrpcval(array(), 'array')));
$good2 = new xmlrpcmsg('system.methodSignature',
array(php_xmlrpc_encode('system.listMethods'))
);
$r = $this->send(array($good1, $bad, $recursive, $good2));
if($r)
{
$this->assertTrue(count($r) == 4, "wrong number of return values");
}
$this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
if(!$r[0]->faultCode())
{
$val = $r[0]->value();
$this->assertTrue(
$val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
"good1 did not return string"
);
}
$this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
$this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
$this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
if(!$r[3]->faultCode())
{
$val = $r[3]->value();
$this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
}
// This is the only assert in this test which should fail
// if the test server does not support system.multicall.
$this->assertTrue($this->client->no_multicall == false,
"server does not support system.multicall"
);
}
function testClientMulticall2()
{
// NB: This test will NOT pass if server does not support system.multicall.
$this->client->no_multicall = true;
$good1 = new xmlrpcmsg('system.methodHelp',
array(php_xmlrpc_encode('system.listMethods')));
$bad = new xmlrpcmsg('test.nosuch',
array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
$recursive = new xmlrpcmsg('system.multicall',
array(new xmlrpcval(array(), 'array')));
$good2 = new xmlrpcmsg('system.methodSignature',
array(php_xmlrpc_encode('system.listMethods'))
);
$r = $this->send(array($good1, $bad, $recursive, $good2));
if($r)
{
$this->assertTrue(count($r) == 4, "wrong number of return values");
}
$this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
if(!$r[0]->faultCode())
{
$val = $r[0]->value();
$this->assertTrue(
$val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
"good1 did not return string");
}
$this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
$this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
$this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
if(!$r[3]->faultCode())
{
$val = $r[3]->value();
$this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
}
}
function testClientMulticall3()
{
// NB: This test will NOT pass if server does not support system.multicall.
$this->client->return_type = 'phpvals';
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?