是否可以以这种方式配置PHPUnit mock?
$context = $this->getMockBuilder('Context')
->getMock();
$context->expects($this->any())
->method('offsetGet')
->with('Matcher')
->will($this->returnValue(new Matcher()));
$context->expects($this->any())
->method('offsetGet')
->with('Logger')
->will($this->returnValue(new Logger()));
Run Code Online (Sandbox Code Playgroud)
我使用PHPUnit 3.5.10,当我要求Matcher时它会失败,因为它需要"Logger"参数.这就像第二个期望是重写第一个,但是当我转储模拟时,一切看起来都不错.
有没有机会让控制器依赖于他们的服务,而不是通过使用其中的服务容器,而是通过纯构造函数依赖注入?
我想以这种方式编写控制器:
<?php
class ArticleController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
{
private $articleFacade;
private $articleRepository;
public function __construct(ArticleFacade $articleFacade, ArticleRepository $articleRepository)
{
$this->articleFacade = $articleFacade;
$this->articleRepository = $articleRepository;
}
public function indexAction()
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我可以看到Symfony ControllerResolver不是通过ServiceContainer而是通过简单的return new $controller调用来完成控制器的新实例.
model-view-controller dependency-injection symfony fosuserbundle