I'm trying to assert that a method on an object (object under test) is called at least once during the construction of another object. I want to use phpunit's expects() functionality to test that a getDetails() method is called. This function is called during the construction of another customer account object. I must be missing something fundamental about the way phpunit works but I may be approaching the problem with a python mindset. I know I could get this working in python but perhaps it's not possible here? Here is my test code:
public function testPhoneBookCode()
{
$account = 'testaccount';
$phone_book_mock = $this->getMockBuilder('\corp\data\PhoneBook')
->setConstructorArgs(array(null))
->setMethods(null)
->getMock();
$expected = array(
'id' => 1234,
'title' => 'Mr',
'first_name' => 'George',
'last_name' => 'Smith',
'phone' => '111-111-1111'
);
$phone_book_mock->expects($this->atLeastOnce())
->method('getDetails')
->with($this->equalTo(1234))
->will($this->returnValue($expected));
try {
$c = new Customer_Account(
$account,
true,
'acme_corp',
1234,
null,
null,
$phone_book_mock
);
} catch ( \Exception $e ) {
$this->fail("An unexpected exception has been raised [$account].". $e);
}
}
At this point I would expect the instantiation of the Customer_Account class to call the getDetails method on my mock and the atLeastOnce check to return true.
This code is modified from a real life example. So any typos may have been introduced when re-typing. In the real life code the 'phone_book' is passed into the Customer_Account object's constructor and the getDetails() method is called in that constructor. I have confirmed that the method IS actually being called and I'm getting the return value I expect. However my mock expects() test is failing.
Expectation failed for method name is equal to when invoked at least once. Expected invocation at least once but it never occured.
The mock is not registering the call to getDetails() even though I'm confident the right code is being called. Is what I'm trying to do just not possible? Do I lose the reference to the mock object once I pass it to another constructor? Is there another way to accomplish this? Any help would be greatly appreciated.