this is first time to create unit test by PHPunit to test my business model of my web application. I was confused to mock my class which is used to persist entity. Here is the function of newUser().
class AccountBM extends AbstractBusinessModel {
public function newUser(Account $account) {
$salt = StringHelper::generateRandomString ( "10" );
$account->setUsername ( $account->getEmail () );
$invitation_code = hash ( 'md5', $account->getEmail () );
$account->setInviationCode ( $invitation_code );
$account->setPassword ( $this->encoder->encodePassword ( $account->getPassword (), $salt ) );
$account->setSalt ( $salt );
$this->persistEntity ( $account );
return $account;
}
}
Then, I try to create a test unit for this fucntion testNewUser()
public function testNewUser() {
$account = $this->newAccountEntity ();
$account_bm =$this->getMockBuilder('AccountBM')
->setMethods(array('newUser'))
->disableOriginalConstructor()
->getMock();
$account=$account_bm->newUser($account);
// compare setter value with saved entity
$this->assertEquals ( '[email protected]', $account->getEmail () );
$this->assertEquals ( '123456789', $account->getPhone () );
$this->assertEquals ( '987654321', $account->getMobilePhone () );
$this->assertEquals ( 'Mr', $account->getTitle () );
$this->assertEquals ( 'test', $account->getFirstName () );
$this->assertEquals ( 'test', $account->getLastName () );
$this->assertEquals ( 'male', $account->getGender () );
$this->assertEquals ( '1', $account->getCompanyLogo () );
$this->assertEquals ( AccountType::IDS, $account->getUserType () );
$this->assertEquals ( RegionType::NCN, $account->getRegion () );
$this->assertEquals ( hash ( 'md5', $account->getEmail () ), $account->getInviationCode () );
$this->assertEquals ( hash ( 'sha256', $account->getSalt () . "test" ), $account->getPassword () );
}
public function newAccountEntity() {
$account = new Account ();
// set account
$account->setEmail ( "[email protected]" );
$account->setPassword ( "test" );
$account->setPhone ( "123456789" );
$account->setMobilePhone ( "987654321" );
$account->setTitle ( "Mr" );
$account->setFirstName ( "test" );
$account->setLastName ( "test" );
$account->setGender ( "male" );
$account->setCompanyLogo ( "1" );
$account->setUserType ( AccountType::IDS );
$account->setRegion ( RegionType::NCN );
return $account;
}
But its seems not going to mock the method "newUser" and its not working on my entity. is there anything wrong with test codes. Thanks.