Я пытаюсь протестировать простое действие контроллера в Zend FrameWork, и я не уверен на 100%, почему мои макеты не работают.
Оригинальное действие:
public function overviewAction()
{
$page = $this->params()->fromQuery('page', 1);
$count = 10;
$user = $this->authenticationService->getIdentity();
return new ViewModel([
'paginator' => $this->agentService->getAgentsOwnedByUser($page, $count, $user),
]);
}
Мой тест для этого действия
/**
* Set Rbac Role and route
*/
$url = "cp/agent";
$this->setRbacGuards(['admin']);
//Nb Rbac class code is here
/**
* Objects required in this test
*/
$user = $this->createMock(User::class);
$paginator = $this->createMock(Paginator::class);
/**
* Mock classes and their methods to be called
*/
$authentication = $this->createMock(AuthenticationService::class);
$authentication
->expects($this->once())
->method('getIdentity')
->will($this->returnValue($this->registerMockObject($user)));
$agentService = $this->createMock(AgentService::class);
$agentService
->expects($this->once())
->method('getAgentsOwnedByUser')
->will($this->returnValue($this->registerMockObject($paginator)));
$this->dispatch('/'.$url);
$this->assertResponseStatusCode(200);
Сообщение об ошибке
There was 1 failure:
1) ControlPanelTest\Controller\AgentControllerTest::testAgentOverviewActionCanBeAccessed
Failed asserting response code "200", actual status code is "500"
Exceptions raised:
Exception 'TypeError' with message 'Argument 3 passed to
Admin\Service\AgentService::getAgentsOwnedByUser() must be an instance of Domain\User\User, null given
.
Для полноты, Rbac Класс
public function rbacGuards($roles)
{
/**
* Deal with Rbac Guards
*/
$roleService = $this->getApplicationServiceLocator()->get(RoleService::class);
$identityProvider = $this->prophesize(IdentityProviderInterface::class);
$identity = $this->prophesize(IdentityInterface::class);
// Here you use the setter to inject your mocked identity provider
$roleService->setIdentityProvider($identityProvider->reveal());
$identityProvider->getIdentity()->shouldBeCalled()->willReturn($identity->reveal());
$identity->getRoles()->shouldBeCalled()->willReturn($roles);
}
Прогноз
Кажется, издевательства не называют ...