I have a doctrine class User which is of this hierarchy Doctrine_Record -> BaseUser -> User. In PHP symfony you are aware that you can access doctrine record using array accessor as well methods. Each class has @property and a method.
$user['mode'] == $user->getMode()
When I am writing PHPUnit test cases, I am unable to mock occurrences where array accessor method is used.
Here is sample code from unit test as well the actual code -
User.php
class User extends BaseUser {
public function clearInactiveUsers()
{
foreach ($this->users as $user) {
if (!$user['mode']) {
unset($this->users[array_search($user, $this->users)]);
$user->delete();
}
}
unset($user);
}
}
This is the test for it
UserTest.php
public function testOnlyInactiveUsersAreRemoved()
{
$userGroup = new UserGroup();
$user_1 = $this->getMock('User');
$user_2 = $this->getMock('User');
$user_1->expects($this->at(0))->method('__get')->with($this->equalTo('mode'))->will($this->returnValue(1));
$user_2->expects($this->at(0))->method('__get')->with($this->equalTo('mode'))->will($this->returnValue(0));
$userGroup->adduser($user_1);
$userGroup->adduser($user_2);
$userGroup->clearInactiveUsers();
$this->assertCount(1, $userGroup->getUsers());
}
I am trying to mock the occurrence $user['mode']
in the code. What I am doing wrong?
I have referenced following link and wrote above code.
PHPUnit - creating Mock objects to act as stubs for properties