I have a function in an API call that checks $this->session->isLoggedIn()
.
The session-object is injected via the consturtor.
Now I have to write an api-functional test. But I just can't login the customer and isLoggedIn()
will always return false
in my service. My test-code currently looks like this:
$historyId = 1;
$download = 0;
$serviceInfo = [
'rest' => [
'resourcePath' => '/V1/preorder-history/view/' . $historyId . '/' . $download,
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
]
];
$loginInfo = [
'rest' => [
'resourcePath' => '/V1/customers/me',
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
'token' => $this->token,
],
];
$requestData = [
'historyId' => $historyId,
'download' => $download,
'token' => $this->token
];
$customer = $this->_webApiCall($loginInfo, $requestData);
/** @var $session \Magento\Customer\Model\Session */
$session = Bootstrap::getObjectManager()->get(\Magento\Customer\Model\Session::class);
$session->loginById($customer['id']);
$session->setCustomerId($customer['id']);
$item = $this->_webApiCall($serviceInfo, $requestData);
EDIT:
within my test, the session object obviously returns that the customer is logged in. So I assume this is not the same object that is injected in my service which I want to test.
EDIT2:
spl_object_hash
confirms this theory.