Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking methods used in Static Methods

I'm trying to stop a method that sends an email from actually sending the email and I think that mock objects (or some variant) are the way to go. Here is the situation:

class UserModel {

    public static function resetPassword()
    {
      // Code to generate new password, etc, etc

      self::_sendMail($to, $body);
      return 1;
    }

    private function _sendMail($to, $body)
    {
      // Send email
    }
}

Is there anyway in PHPUnit that I can mock _sendMail() and inject my own code so I can properly test the other logic in resetPassword() ?

My test would simply look something like:

$this->assertTrue(UserModel::resetPassword());

Thanks for any help.

like image 324
Mike B Avatar asked Dec 05 '25 17:12

Mike B


1 Answers

I think this is how you'd do it

class MockUserModel extends UserModel
{
    static function _sendMail( $to, $body )
    {
        // do nothing
    }
}

then

$this->assertTrue( MockUserModel::resetPassword() );

But I'm not a unit testing guru, so I apologize if this leads you on a wild goose chase.

like image 99
Peter Bailey Avatar answered Dec 08 '25 07:12

Peter Bailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!