我目前正在为 Symfony2 开发一个开源包,并且真的希望它在单元测试覆盖率和一般可靠性方面成为最优秀的,但是由于我缺乏 PHPUnit 知识(或复杂的场景,谁知道)..
目前,我有一个 Mailer 类,用于处理个人邮件场景。它看起来有点像这样:
<?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 来处理不同的路由和模板系统,快乐快乐快乐。
这是我尝试为上述设置的初始测试:
<?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\Templated\EngineInterface 和 Symfony\Component\Routing\RouterInterface,所以我无法使用我自己创建的任何模拟对象。我尝试过的一种方法是创建一个抽象类,它在测试页上实现正确的接口,但是 getMockForAbstractClass 失败,指出它找不到该类...