PHPUnit how to test when the method is never invoked

I was creating a PHPUnit test and I needed to confirm that it never was sending an email given some conditions. The test where you check that the email is being called at least once is pretty simple, but how to do the opposite. A friend tipped me off and PHPUnit has $this->never() to be used instead of $this->once();

So basically is like this. If you need to test if its sending the email (or the method is being called):

  $observer = $this->getMock('Observer', array('update'));
$observer->expects($this->once())
->method('update')
->with($this->equalTo('something'));

but if you need to test if the method is never being called just do:

  $observer = $this->getMock('Observer', array('update'));
$observer->expects($this->never())
->method('update')
->with($this->equalTo('something'));

Pretty simple and logical. I haven’t still found that on the PHPUnit documentation, but I will stop by later again to see if I can find it.

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: