I'm trying to test my CakePHP 3 Internal error Exception.
My Controller:
public function getConfirmation()
{
if (!$this->request->getData())
throw new InternalErrorException(__('data not found'));
$confirmStatus = $this->XYZ->getConfirmation($this->request->getData('ID'), $this->request->getData('MANAGER_ID'));
$this->set([
'confirmStatus' => ($confirmStatus) ? 1 : 0,
]);
}
In the exception test, I've added expectException
as suggested on Sebastian Bergmann's blog and I think it is a good idea:
public function testInternalErrorExceptionIsRaised()
{
$this->enableCsrfToken();
$this->enableSecurityToken();
$formPostData = [];
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->expectException(\Cake\Network\Exception\InternalErrorException::class);
$this->post(
[
'controller' => 'XYZ',
'action' => 'getConfirmation'
],
$formPostData
);
$this->assertResponseFailure();
$this->assertResponseCode(500);
}
Error:
1) App\Test\TestCase\Controller\XYZControllerTest::testInternalErrorExceptionIsRaised
Failed asserting that exception of type "Cake\Network\Exception\InternalErrorException" is thrown.
I've tried various ways, but was not able to test CakePHP 3 exception. I have tried expectExceptionCode()
and expectExceptionMessage
as well, but no luck. Is it possible to test the exception?