PHPUnit:模拟一个像身份一样的方法

时间:2015-11-26 07:40:06

标签: php unit-testing testing phpunit

使用PHPUnit,有没有办法模拟一个方法并使其表现得像身份(x-> x)?有些东西:

$this->myMethod(Argument::any())->willReturn(<should return the argument untouched>)

1 个答案:

答案 0 :(得分:1)

您可以使用returnArgument()方法。来自doc:

  

有时您想要返回方法调用的一个参数   (未更改)作为存根方法调用的结果。例9.4显示   如何使用returnArgument()而不是returnValue()实现此目的   class StubTest extends PHPUnit_Framework_TestCase { public function testReturnArgumentStub() { // Create a stub for the SomeClass class. $stub = $this->getMockBuilder('SomeClass') ->getMock(); // Configure the stub. $stub->method('doSomething') ->will($this->returnArgument(0)); // $stub->doSomething('foo') returns 'foo' $this->assertEquals('foo', $stub->doSomething('foo')); // $stub->doSomething('bar') returns 'bar' $this->assertEquals('bar', $stub->doSomething('bar')); } }

例如:

$tempQ = "
    <div class='container'  data-ng-controller='formCtrl as fctrl'>
        <div class='card row'>
        <table class='table' width='100%';>
        <thead>
        <tr>
        <th>
        <h3 class='f-400'>Kredi Hesaplama</h3>
        </th>
        </tr>
        </thead>
        <tbody>
            <tr>
            <td>
            <div  class='card-body card-padding'>
                <h4 class='f-500'>Tutar: <span style='font-size:17pt;'>{{ fctrl.nouisliderValue }} </span> TL</h4>
                <div slider class='input-slider' ng-model='fctrl.nouisliderValue' start='1000' end='10000' data-is-color='red'></div> 
                <br/><br/>
                <h4 class='f-500'>Vade Miktarı: <span style='font-size:17pt;'>{{ fctrl.nouisliderValue2 }} </span> Ay</h4>
                <div slider class='input-slider' ng-model='fctrl.nouisliderValue2' start=1 end=36 data-is-color='red'></div> 
            </div>
            </td>
            <td style='background-color:#F44336;'>
            <br/>
                    <div class='col-sm-3 col-xs-6' style='width:100%;height:100%'>
                        <div class='color-block bgm-red'>
                            <span class='color' style='font-size:10pt'>Aylık Ödeme:</span>
                            <span class='code' style='font-size:24pt'>{{ fctrl.nouisliderValue/fctrl.nouisliderValue2 | number:2}}</span> TL
                        </br>
                            <span class='code' style='font-size:10pt'> Faiz tutarı: 1.12%</span>
                        </div>
                    </div>
            </td>
            </tr>
        </tbody>
        </table>
        </div>  
    </div>";

     $temp = rawurlencode($tempQ);

    $result_str .= "<IMAGE_SOURCE>$temp</IMAGE_SOURCE>\n";