I'm trying to finalize a unit test but it's failing when I get something from the session and then cast it into an array.
What I'm doing:
public function formatAddress($address) {
if(empty($address)) {
$full_address = craft()->httpSession->get('address');
$full_address = (array) $full_address;
return $formatted_address = array(
"address1" => $full_address['street'],
"city" => $full_address['city'],
"state" => $full_address['state'],
);
} else {
return $address;
}
}
$formatted_address = $this->formatAddress($user['address']);
How it's coming back from the session:
stdClass Object
(
[city] => Warsaw
[county] => Hancock
[id] => 110458115f8a45849a31df1d9144aa62
[latitude] => 40.27917
[longitude] => -91.30012
[state] => IL
[street] => 1091 E County Road 550
[zip] => 62379-3212
)
So I take this session variable, typecast it to an array then take those array properties and assign them to keys in another array.
However, when running my unit test it's telling me that there's an undefined index "street" on the 'address1" => $full_address['street'], line?
The function works perfect in my dev, but the unit test is not liking it for some reason.
Unit test:
$mocked_address = array(
"address1" => "109 some street",
"city" => "some city",
"state" => "AZ",
"zip" => "99955"
);
$this->httpSession
->shouldReceive('get')
->with('address')
->andReturn($mocked_address);