我的问题是:
我想测试' getThirdPayUrl'通过模拟' getThirdPayUrlSpec',如何使用phpunit创建模拟类?
class BasePayController{
public static function getThirdPayUrl($type,$order,$arr,&$url){
//$objCtrl = new AliPayController();
$objCtrl = self::getPayController($type);
$ret = $objCtrl->getThirdPayUrlSpec($order,$arr,$url);
return $ret;
}
}
答案 0 :(得分:0)
PHPUnit Manual有一个包含Mock's示例的部分。从这里开始,如果您遇到问题,请发布更详细的问题。
基本上,你的测试会模拟BasePayController,并返回一个硬盘URL进行测试。
<?php
require_once 'BasePayController.php';
class BasePayControllerTest extends PHPUnit_Framework_TestCase
{
public function testThirdPartyURL()
{
// Create a stub
$stub = $this->getMockBuilder('BasePayController')
->disableOriginalConstructor()
->getMock();
// Configure the stub.
$stub->expects($this->any())
->method('getThirdPayUrl')
->will($this->returnValue('http://Dummy.com/URLToTest'));
// Test the Stub
$this->assertEquals('http://Dummy.com/URLToTest', $stub->getThirdPartyUrl());
}
}
?>
答案 1 :(得分:0)
我的解决方法如下,首先需要安装一个名为php-test-helper的插件
class BasePayControllerTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->getMock(
'AliPayContoller', /* name of class to mock */
array('getThirdPayUrlSpec'), /* list of methods to mock */
array(), /* constructor arguments */
'AliPayMock' /* name for mocked class */
);
set_new_overload(array($this, 'newCallback')); //php-test-helper plug-in
}
protected function tearDown()
{
unset_new_overload(); //php-test-helper plug-in
}
protected function newCallback($className)
{
switch ($className) {
case 'AliPayContoller': return 'AliPayMock';
default: return $className;
}
}
public function testgetThirdPayUrl()
{
$foo = new BasePayController;
$this->assertTrue($foo->getThirdPayUrl());
}
}
?>