我正在测试一个扩展了抽象类的服务类。
当尝试运行服务类的测试时,我得到错误"调用成员函数get()在…中的非对象上"。
我正在测试的方法:
public function findOneHolidayById($holidayId)
{
$holiday = $this->getEntityManager()
->getRepository('Common'Entity'HolidayEntity')
->findOneBy(array('id' => $holidayId));
return $holiday;
}
我正在运行的测试:
public function testFindOneHolidayById()
{
$holidayId = 40;
$holidayService = new 'Common'Service'Holiday'HolidayService();
$holidayService->findOneHolidayById($holidayId);
}
错误是由测试方法中的代码引起的:
$holidays = $this->getEntityManager()
->getRepository('Common'Entity'HolidayEntity')
->findAll();
我相信我需要模拟从抽象控制器调用的方法,或沿着这条线的东西,但我不确定如何接近这一点。非常感谢你的帮助。
感谢全栈消息:
...............E.
Fatal error: Call to a member function get() on a non-object in C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'module'Common'src'Common'Controller'AbstractController.php on line 40
Call Stack:
0.0004 125872 1. {main}() C:'xampp'php'phpunit:0
0.0089 361456 2. PHPUnit_TextUI_Command::main() C:'xampp'php'phpunit:46
0.0089 364872 3. PHPUnit_TextUI_Command->run() C:'xampp'php'pear'PHPUnit'TextUI'Command.php:129
1.4460 6229720 4. PHPUnit_TextUI_TestRunner->doRun() C:'xampp'php'pear'PHPUnit'TextUI'Command.php:176
1.6645 6549216 5. PHPUnit_Framework_TestSuite->run() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'TextUI'TestRunner.php:406
36.0976 39708632 6. PHPUnit_Framework_TestSuite->run() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'Framework'TestSuite.php:722
39.3706 40551320 7. PHPUnit_Framework_TestCase->run() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'Framework'TestSuite.php:722
39.3706 40551288 8. PHPUnit_Framework_TestResult->run() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'Framework'TestCase.php:699
39.3726 40554360 9. PHPUnit_Framework_TestCase->runBare() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'Framework'TestResult.php:609
39.3758 40588552 10. PHPUnit_Framework_TestCase->runTest() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'Framework'TestCase.php:743
39.3759 40589080 11. ReflectionMethod->invokeArgs() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'Framework'TestCase.php:866
39.3759 40589200 12. CommonTest'Service'HolidayServiceTest->testFindOneHolidayById() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'vendor'phpunit'phpunit'src'Framework'TestCase.php:866
39.6060 41121200 13. Common'Service'Holiday'HolidayService->findOneHolidayById() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'module'Common'test'CommonTest'Service'HolidayServiceTest.php:34
39.6060 41121200 14. Common'Controller'AbstractController->getEntityManager() C:'xampp'htdocs'Effcomm-SA-Portal-V1.0'effcomm-sa-portal'module'Common'src'Common'Service'Holiday'HolidayService.php:57
gettentitymanager:
protected function getEntityManager()
{
if (is_null($this->entityManager)) {
$this->entityManager = $this->getServiceLocator()
->get('Doctrine'ORM'EntityManager');
}
return $this->entityManager;
}
错误信息Call to a member function foobar() on a non-object
意味着您试图在一个非对象的变量上调用foobar()
方法。
在您的例子中,问题是您正在调用$this->getServiceLocator()
并期望它返回某个服务的实例。然而,getServiceLocator
似乎没有找到您请求的服务(在本例中是EM),我猜它会返回null
。
你需要做的是检查getServiceLocator()
方法中是否有你所请求的类的对象实例,如果你不能加载或创建实例,抛出一个异常。