How would you mock an abstract class with template methods that are final -- what's the best practice here?

25 views
Skip to first unread message

Jeff Conrad

unread,
Aug 2, 2012, 11:52:26 AM8/2/12
to phpmo...@googlegroups.com
Hi,

I'm kinda new to unit testing and mock frameworks, so if this is something really easy, pardon my ignorance.

I have an abstract base class that has a template method marked as final.  I want to make sure the abstract protected methods called by my template method are called.

When I call m::mock('My\Package\BaseClassName'), it tells me that because my template method is final, I should pass in an instance of that class instead and create a partial mock.  However, my base class is abstract, so I can't just make an instance of it.

Below is an example class just in case I didn't describe it right.  In the case below, I'd want to make sure the doSendEmail function got called.

abstract class BaseEmailSender {
   final public function sendEmail($from, $to, $subject, $body) {
      // basic setup steps that always happen like address validation...
      

      $this->doSendEmail($from, $to, $subject, $body);
   }

   abstract protected function doSendEmail($from, $to, $subject, $body);
}

I know I can make another class that extends my base class that I use only in testing that has those method calls with nothing inside the bodies, which I can then pass to Mockery, but that doesn't seem right.

How would you deal with this issue?

Thanks,

Jeff
Reply all
Reply to author
Forward
0 new messages