如何使用phpunit测试多个查询结果
我对phpunit有点陌生,经过几个小时的研究,我还没有完全找到一种方法。如何使用phpunit测试函数getResults 函数getResults和getResultsDetails属于结果类。函数searchUser属于用户类。为了获得passlogin,我可以使用用户名和密码在login对象中调用doLogin,它将保持在$\u会话中如何使用phpunit测试多个查询结果,php,mysql,nested,phpunit,Php,Mysql,Nested,Phpunit,我对phpunit有点陌生,经过几个小时的研究,我还没有完全找到一种方法。如何使用phpunit测试函数getResults 函数getResults和getResultsDetails属于结果类。函数searchUser属于用户类。为了获得passlogin,我可以使用用户名和密码在login对象中调用doLogin,它将保持在$\u会话中 class Result { public function __construct() { if(!isset($
class Result {
public function __construct()
{
if(!isset($_SESSION)){
session_start();
}
$this->loginObject = new Login();
}
public function getResults($fName, $lName)
{
$result = searchUser($fName, $lName);
while ($row = mysql_fetch_assoc($results)) {
$userIDs = $result['id'] . ',';
}
return
array(
'userIDs' => $userIDs,
'count' => getResultDetails($userIDs),
'results' => getResultDetails($userIDs, 0, 500),
)
}
如果传入限制和偏移量,则返回结果,否则返回所有项的计数
public function getResultDetails($userIDs, $limit = null, $offset =null)
{
if(!$this->beforeFilter()) return "logout";
//open db connection
$sql = '';
if(!$limit && !$offset)
{
$sql .= 'SELECT count(*) ';
} else {
$sql .= 'SELECT a.info, b.info, d.info ';
}
$sql .= 'FROM a ';
$sql .= 'inner join b on blah blah';
$sql .= 'inner join d blah blah';
$sql .= "where d.userID in ($userIDs)";
if($limit && $offset)
$sql .= "LIMIT $limit, $offset";
return mysql_query($sql);
}
}
这是基于firName和lastName的用户搜索。searchUser属于用户类
班级病人{
公共函数构造()
{
如果(!isset($\会话)){
会话_start();
}
$this->loginObject=new Login();
}
为此,您需要使用存根/模拟。基本上,您不希望在getResult方法单元测试中使用test searchUser或getResultDetails方法。因此,您必须模拟它们,并检查它们是否使用适当的参数调用 为此,您将使用phpunit mock 第一步是设置任何预期值并构建模拟:
$someExpectedUser = 123;
$expectedCount = 234;
$expectedResult = array('blablabla');
$object = $this->getMock('YourTestedClass', array('searchUser', 'getResultDetails'));
您现在已经创建了mock对象。第一个参数是类的名称,第二个参数是数组,其中包含要模拟的方法的名称(请注意,不要在第二个参数中指定“getResult”方法-这意味着在$object上调用此方法时,将调用真正的方法)
下一步是指定searchUser的行为:
$object->expects($this->once())
->method('searchUser')
->with($fName, $lName)
->will($this->returnValue($someExpectedUser));
getResultDetails方法也一样:
$object->expects($this->at(1))
->method('getResultDetails')
->with($someExpectedUser)
->will($this->returnValue($expecedCount));
$object->expects($this->at(2))
->method('getResultDetails')
->with($someExpectedUser, 0, 500)
->will($this->returnValue($expecedResults));
然后,您只需要调用tested方法并检查它是否返回expeced值:
$expectedResult = array(
'userIDs' => $someExpectedUser,
'count' => $expectedCount,
'results' => $expectedResult
);
$result = $object->getResult($fName, $lName);
$this->assertEquals($expectedResult, $result);
如果您有什么不清楚的地方,请告诉我
您好!您的评论真的很有帮助。我确实对我原来的问题做了一些更改。但是当我使用$userObj=$this->getMock('Users',array('doLogin','searchUser'));$userObj->expects($this->once())->method('doLogin')->with('username',password');调用1次时,我得到的方法名称等于的预期失败.Method应该被调用1次,实际上被调用了0次。是的-因为您指定了doLogin应该被精确调用一次($this->once()-告诉您的mock这个方法应该被执行一次,但您从来没有调用过它(至少根据您发布的代码我看不到它)。
$expectedResult = array(
'userIDs' => $someExpectedUser,
'count' => $expectedCount,
'results' => $expectedResult
);
$result = $object->getResult($fName, $lName);
$this->assertEquals($expectedResult, $result);