0

私の質問は:

「getThirdPayUrlSpec」をモックして「getThirdPayUrl」をテストしたいのですが、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;
    }   
}
4

2 に答える 2

0

私の解決策は次のとおりです。最初の php は、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());
    }
}
?>
于 2013-07-26T07:36:47.000 に答える