无法将模拟容器设为 return 除了 null
Unable to get mock container to return anything but null
我有一个测试失败了,因为我无法成功地存根控制器的 get
方法:
1) Tests\my-project\BackendBundle\Service\PdfGenerateServiceTest::test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null
TypeError: Argument 1 passed to Mock_Pdf_d0288d34::setLogger() must implement interface Psr\Log\LoggerInterface, null given, called in /var/www/my-project/src/my-project/BackendBundle/Service/PdfGenerateService.php on line 66
测试
public function test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null()
{
$protocolAndHost = "http://foo.bar.com";
$service = new PdfGenerateService($this->createFileServiceMock(), $this->createTranslatorMock(), $this->createSnappyMock(), $this->createContainerMock(), $protocolAndHost);
$httpPathToPtImage = $service->getHttpPathToPtImage(null);
self::assertEquals($httpPathToPtImage, $protocolAndHost . "abc/def");
}
失败的构造函数
public function __construct(FileService $fileService, Translator $translator, Pdf $snappy, ContainerInterface $container, string $protocolAndHost)
{
$this->fileService = $fileService;
$this->translator = $translator;
$this->currentLocale = $this->translator->getLocale();
/* Could reconfigure the service using `service.yml` to pass these in using DI */
$this->twig = $container->get('twig');
$this->logger = $container->get('logger'); // <--- should not be null
$timeoutInSeconds = 15; // can be high, since the job is done async in a job (user does not wait)
$snappy->setLogger($this->logger); // <--- THIS FAILS due to $this->logger being null
存根
protected function createContainerMock()
{
$containerMock = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
$loggerMock = $this->createLoggerMock();
$containerMock->method('get')->will($this->returnValueMap([
['logger', $loggerMock]
]));
return $containerMock;
}
我真的不明白为什么 get('logger')
只调用 returns null
当我设置了一个要使用上面的 returnValueMap
调用返回的模拟时。
我偶然发现了一个关于这个主题的 SO 问题,其中 someone mentioned 您需要提供所有参数,甚至是可选参数。然后我检查了界面,它确实列出了第二个参数:
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
仍然,将地图更改为 ['logger', null, $loggerMock]
没有任何变化,所以我对接下来要尝试什么感到有点不知所措。
PHPunit 6.5,PHP7.2,Symfony 3.4
您真的很接近解决方案。为 returnValueMap
中的可选参数提供值时,您必须使用该值本身,而不仅仅是 null。
所以不用
['logger', null, $loggerMock]
尝试指定
['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
完整的调用如下所示:
$containerMock->method('get')->will($this->returnValueMap([
['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
]));
我有一个测试失败了,因为我无法成功地存根控制器的 get
方法:
1) Tests\my-project\BackendBundle\Service\PdfGenerateServiceTest::test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null
TypeError: Argument 1 passed to Mock_Pdf_d0288d34::setLogger() must implement interface Psr\Log\LoggerInterface, null given, called in /var/www/my-project/src/my-project/BackendBundle/Service/PdfGenerateService.php on line 66
测试
public function test_getHttpPathToPtImage_should_return_the_default_avatar_when_photo_is_null()
{
$protocolAndHost = "http://foo.bar.com";
$service = new PdfGenerateService($this->createFileServiceMock(), $this->createTranslatorMock(), $this->createSnappyMock(), $this->createContainerMock(), $protocolAndHost);
$httpPathToPtImage = $service->getHttpPathToPtImage(null);
self::assertEquals($httpPathToPtImage, $protocolAndHost . "abc/def");
}
失败的构造函数
public function __construct(FileService $fileService, Translator $translator, Pdf $snappy, ContainerInterface $container, string $protocolAndHost)
{
$this->fileService = $fileService;
$this->translator = $translator;
$this->currentLocale = $this->translator->getLocale();
/* Could reconfigure the service using `service.yml` to pass these in using DI */
$this->twig = $container->get('twig');
$this->logger = $container->get('logger'); // <--- should not be null
$timeoutInSeconds = 15; // can be high, since the job is done async in a job (user does not wait)
$snappy->setLogger($this->logger); // <--- THIS FAILS due to $this->logger being null
存根
protected function createContainerMock()
{
$containerMock = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
$loggerMock = $this->createLoggerMock();
$containerMock->method('get')->will($this->returnValueMap([
['logger', $loggerMock]
]));
return $containerMock;
}
我真的不明白为什么 get('logger')
只调用 returns null
当我设置了一个要使用上面的 returnValueMap
调用返回的模拟时。
我偶然发现了一个关于这个主题的 SO 问题,其中 someone mentioned 您需要提供所有参数,甚至是可选参数。然后我检查了界面,它确实列出了第二个参数:
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE);
仍然,将地图更改为 ['logger', null, $loggerMock]
没有任何变化,所以我对接下来要尝试什么感到有点不知所措。
PHPunit 6.5,PHP7.2,Symfony 3.4
您真的很接近解决方案。为 returnValueMap
中的可选参数提供值时,您必须使用该值本身,而不仅仅是 null。
所以不用
['logger', null, $loggerMock]
尝试指定
['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
完整的调用如下所示:
$containerMock->method('get')->will($this->returnValueMap([
['logger', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $loggerMock]
]));