我正在尝试在PHPUnit中编写一个测试,以获取基于Symfony路由的子域的URL的简单方法。我不确定我的测试是否能够正确访问Symfony路由器组件,这就是为什么它会垮台,但我目前似乎无法让它通过。使用PHPUnit中的Symfony路由器组件生成URL
这是我收到的错误:
There was 1 error:
1) CRMPiccoBundle\Tests\Services\MailerHelperTest::testReturnsBillingURL with data set "crmpicco.co.uk" (CRMPiccoBundle\Entity\Course Object (...), ' https://www.crmpicco.co.uk/en/admin/billing ') TypeError: Return value of CRMPiccoBundle\Services\MailerHelper::getBillingUrlForSubdomain() must be of the type string, null returned
这是我的测试和数据提供者:
/**
* @dataProvider dataProvider
*/
public function testReturnsBillingURL(Course $course, string $expectedUrl)
{
$mailerHelper = new MailerHelper($this->getRouterMock());
$this->assertEquals($expectedUrl, $mailerHelper->getBillingUrlForSubdomain($course, true));
}
public function dataProvider()
{
return [
'crmpicco.co.uk' => [
'site' => (new Course())
->setSubdomain('crmpicco'),
'expectedUrl' => 'https://www.crmpicco.co.uk/en/admin/billing',
],
];
}
这是我getRouterMock方法:
/**
* @return \Symfony\Bundle\FrameworkBundle\Routing\Router
*/
protected function getRouterMock()
{
return $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Routing\Router')
->disableOriginalConstructor()
->setMethods(['generate', 'supports', 'exists'])
->getMockForAbstractClass();
}
这是我正在测试的方法:
/**
* @param Course $course
* @param bool $absoluteUrl
*
* @return string
*/
public function getBillingUrlForSubdomain(Course $course, bool $absoluteUrl = true) : string
{
return $this->router->generate('crmpiccobundle_portal_billing', [
'subdomain' => $site->getSubdomain(),
], $absoluteUrl);
}