我正在嘗試在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);
}