这是我要测试的服务:
class AffairesJSService {
private $inlineScript;
private $url;
private $sBasepath;
public function __construct($inlineScript, $url, $sBasepath)
{
$this->inlineScript = $inlineScript;
$this->url = $url;
$this->sBasepath = $sBasepath;
}
public function inject($aParams)
{
$url = $this->url;
$containerRoutes = new Container('IntraRouteListAffaires');
$sEtatAffaire = $containerRoutes->sEtatAffaire !== null ? $containerRoutes->sEtatAffaire : 'EN_COURS';
$iIdPrestation = $containerRoutes->iIdPrestation !== null ? $containerRoutes->iIdPrestation : 0;
$iIdCentreProfit = $containerRoutes->iIdCentreProfit !== null ? $containerRoutes->iIdCentreProfit : 0;
$this->inlineScript->appendFile($this->sBasepath . $aParams["myfile"]);
$sUrlListContrats = $url('module/action', ['idaffaire' => $aParams['idaffaire']]);
$this->inlineScript->captureStart();
echo <<<JS
loadPanelContrats('$sUrlListContrats'); JS;
$this->inlineScript->captureEnd();
} }
嗯,这是我的测试,但我不知道如何模拟InlineScripts并声明appendFile:
class AffairesJSServiceTest extends TestCase
{
/**
* @var MyService
*/
private $myService;
public function setUp()
{
$mockUrl = $this->getMock('Zend\View\Helper\Url\Url', array(), array(), '', false);
$inlineScript = $this->getMock('Zend\View\Helper\InlineScript\InlineScript', array(), array(), '', false);
$sBasepath = "/my/path";
$this->myService = new AffairesJSService(
$inlineScript,
$mockUrl,
$sBasepath,
);
}
public function testInject()
{
$aParams = array();
//$aParams["myfile"] = 'myfile';
$this->myService->inject($aParams);
}
你可以帮忙吗?最有问题的问题是如何模拟特定的zf2方法。感谢。
答案 0 :(得分:1)
使用流利语法,您可以执行以下操作。 mock的所有方法都不会执行任何操作并返回null:
$inlineScript = $this->getMockBuilder('Zend\View\Helper\InlineScript\InlineScript')
->disableOriginalConstructor()
->getMock();
如果您只想模拟特定方法,可以使用setMethods()
方法指定它:
$inlineScript = $this->getMockBuilder('Zend\View\Helper\InlineScript\InlineScript')
->setMethods(['appendFile'])
->disableOriginalConstructor()
->getMock();