How to mock a function to test persist into database by PHPunit in Symfony
<p>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().</p>

<pre><code>class AccountBM extends AbstractBusinessModel {

    public function newUser(Account $account) {
        $salt = StringHelper::generateRandomString ( "10" );
        $account-&gt;setUsername ( $account-&gt;getEmail () );
        $invitation_code = hash ( 'md5', $account-&gt;getEmail () );
        $account-&gt;setInviationCode ( $invitation_code );
        $account-&gt;setPassword ( $this-&gt;encoder-&gt;encodePassword ( $account-&gt;getPassword (), $salt ) );
        $account-&gt;setSalt ( $salt );
        $this-&gt;persistEntity ( $account );
        return $account;
    }
}
</code></pre>

<p>Then, I try to create a test unit for this fucntion testNewUser()</p>

<pre><code> public function testNewUser() {
        $account = $this-&gt;newAccountEntity ();

        $account_bm =$this-&gt;getMockBuilder('AccountBM')
                    -&gt;setMethods(array('newUser'))
                    -&gt;disableOriginalConstructor()
                    -&gt;getMock();
        $account=$account_bm-&gt;newUser($account);

        // compare setter value with saved entity
        $this-&gt;assertEquals ( 'test@test.com', $account-&gt;getEmail () );
        $this-&gt;assertEquals ( '123456789', $account-&gt;getPhone () );
        $this-&gt;assertEquals ( '987654321', $account-&gt;getMobilePhone () );
        $this-&gt;assertEquals ( 'Mr', $account-&gt;getTitle () );
        $this-&gt;assertEquals ( 'test', $account-&gt;getFirstName () );
        $this-&gt;assertEquals ( 'test', $account-&gt;getLastName () );
        $this-&gt;assertEquals ( 'male', $account-&gt;getGender () );
        $this-&gt;assertEquals ( '1', $account-&gt;getCompanyLogo () );
        $this-&gt;assertEquals ( AccountType::IDS, $account-&gt;getUserType () );
        $this-&gt;assertEquals ( RegionType::NCN, $account-&gt;getRegion () );
        $this-&gt;assertEquals ( hash ( 'md5', $account-&gt;getEmail () ), $account-&gt;getInviationCode () );
        $this-&gt;assertEquals ( hash ( 'sha256', $account-&gt;getSalt () . "test" ), $account-&gt;getPassword () );
    }

public function newAccountEntity() {
        $account = new Account ();

        // set account
        $account-&gt;setEmail ( "test@test.com" );
        $account-&gt;setPassword ( "test" );
        $account-&gt;setPhone ( "123456789" );
        $account-&gt;setMobilePhone ( "987654321" );
        $account-&gt;setTitle ( "Mr" );
        $account-&gt;setFirstName ( "test" );
        $account-&gt;setLastName ( "test" );
        $account-&gt;setGender ( "male" );
        $account-&gt;setCompanyLogo ( "1" );
        $account-&gt;setUserType ( AccountType::IDS );
        $account-&gt;setRegion ( RegionType::NCN );
        return $account;
    }
</code></pre>

<p>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.</p>