PHPUnit,接口和命名空间(Symfony2) [英] PHPUnit, Interfaces and Namespaces (Symfony2)

查看:77
本文介绍了PHPUnit,接口和命名空间(Symfony2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为Symfony2开发一个开源软件包,并且真的希望它在单元测试覆盖率和总体可靠性方面成为狗狗的小玩意儿,但是由于缺乏PHPUnit知识,我遇到了麻烦(或者是一个复杂的场景,谁知道).

I'm currently working on an open source bundle for Symfony2, and really want it to be the dogs nadgers in terms of unit test coverage and general reliability, however I've run into a snag due to my lack of PHPUnit knowledge (or a complex scenario, who knows)..

目前,我有一个Mailer类,用于处理单个邮件方案.看起来像这样:

At present, I have a Mailer class, for handling individual mail scenarios. It looks a bit like this:

<?php
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Routing\RouterInterface;

class Mailer
{
    protected $mailer;
    protected $router;
    protected $templating;
    protected $parameters;

    public function __construct($mailer, RouterInterface $router, EngineInterface $templating, array $parameters)
    {
        $this->mailer = $mailer;
        $this->router = $router;
        $this->templating = $templating;
        $this->parameters = $parameters;
    }
}

足够简单,在那里有一些Symfony2接口gubbins来处理不同的路由和模板系统,开心开心,开心开心.

Simple enough, got some Symfony2 interface gubbins in there to handle different routing and templating systems, happy happy joy joy.

这是我尝试为上述设置的初始测试:

Here's the initial test I tried setting up for the above:

<?php
use My\Bundle\Mailer\Mailer

class MailerTest extends \PHPUnit_Framework_TestCase
{
    public function testConstructMailer
    {
        $systemMailer = $this->getSystemMailer();
        $router = $this->getRouter();
        $templatingEngine = $this->getTemplatingEngine();

        $mailer = new Mailer($systemMailer, $router, $templatingEngine, array());
    }

    protected function getSystemMailer()
    {
        $this->getMock('SystemMailer', array('send');
    }   
    protected function getRouter()
    {
        $this->getMock('RouterInterface', array('generate');
    }

    protected function getTemplatingEngine()
    {
        $this->getMock('RouterInterface', array('render');
    }
}

这里的问题是我的模拟对象没有实现Symfony \ Bundle \ FrameworkBundle \ Templating \ EngineInterface和Symfony \ Component \ Routing \ RouterInterface,所以我不能使用自己创建的任何模拟对象.我尝试过的一种方法是创建一个抽象类,该类在测试页上实现正确的接口,但是getMockForAbstractClass失败,表明找不到该类...

The problem here is that my mock objects do not implement Symfony\Bundle\FrameworkBundle\Templating\EngineInterface and Symfony\Component\Routing\RouterInterface, so I can't use any mock objects that I create myself. One method I have tried is creating an abstract class which implements the correct interface on the test page, however the getMockForAbstractClass fails, stating it can't find the class...

推荐答案

进行模拟时,您需要使用完全限定的类路径,因为模拟功能不会占用调用代码或任何其他代码的名称空间考虑使用"语句.

When mocking you need to use the full qualified class path as the mock functionality is not taking the namespace of the calling code or any "use" statements into consideration.

尝试

->getMock('\\Symfony\\Component\\Routing\\RouterInterface'); 

,省略第二个参数.通常,指定方法的弊大于利. 仅当您想要使所有其他方法像以前一样工作时,才需要第二个参数.

and leave out the second parameter. Usually specifying the methods does a lot more worse than good. Only if you want all the other methods to work like before than you should need the second parameter.

<?php

namespace bar;

class MyClass {}

namespace foo;

use \bar\MyClass;

class MockingTest extends \PHPUnit_Framework_TestCase {

    public function testMock() {
        var_dump($this->getMock('MyClass') instanceOf MyClass);
        var_dump($this->getMock('\\bar\\MyClass') instanceOf MyClass);
    }   
}

产生:

/phpunit.sh --debug fiddleTestThree.php 
PHPUnit @package_version@ by Sebastian Bergmann.


Starting test 'foo\MockingTest::testMock'.
.bool(false)
bool(true)

这篇关于PHPUnit,接口和命名空间(Symfony2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆