如何模拟ZF2 Url方法?

时间:2015-10-12 12:54:44

标签: php unit-testing zend-framework2 phpunit

这是我的服务我想测试:

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 = new InlineScript();

        $sBasepath = "/my/path";

        $this->myService = new AffairesJSService(
            $inlineScript,
            $mockUrl,
            $sBasepath,
        );
    }

    public function testInject()
    {
        $aParams = array();
        $aParams["myfile"] = 'myfile';
        $this->myService->inject($aParams);
    }

事实上,我的服务构造函数中的$ url是由工厂创建的:

public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $inlineScript = $serviceLocator->get('viewhelpermanager')->get('InlineScript');
        $url = $serviceLocator->get('viewhelpermanager')->get('url');
        $sBasepath = $serviceLocator->get('Zend\View\Renderer\RendererInterface')->basePath();

        $affairesJSService = new AffairesJSService(
            $inlineScript,
            $url,
            $sBasepath
        );

        return $affairesJSService;
    }

如果我执行测试,我会收到错误:

  

PHP致命错误:函数名称必须是字符串

请你帮我做我的网址()模拟?感谢。

0 个答案:

没有答案