Tag Archives: phpunit

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.


Little bad habits

Programming will eventually create little bad habits. Some coding habits that make the readability of the code bad, but improve speed when coding, some other habits like not adding comments whenever you are coding something. The worst of the bad habits I have seen so far on any developer is related to quality. The developer will create a excellent logic but very rarely will check the output against what it was supposed to be. This habit is usually fixed with PHPUnit testing that usually points out very well what is expected against what the method has returned.

A little example of this habit can be described as: The method should return the birthday date of the user created and it returned the created date.

A simple issue like this goes on to a production environment without being noticed. The coding has no output errors (it returns a date). The QA team will look at it and most likely will approve the code (not saying that all QA teams will pass this, but a simple error some times are rarely seem). The OPS team will not notice the error, the server doesn’t have any log entries for errors, warnings or notices. In the end one small bad habit could have became a huge production issue.

The example above covers a date, which in most of the cases will never be an issue to anything, but consider that the issue was a floating point error of $0.01 in every transaction made. This could have became a huge mess.

Regardless of the fact that all developers should pay more attention to little details, even when you don’t have much time you can still be safe from bad habits and avoiding coding errors just by having a good unit testing in place.

My bad habit, is not having unit testing in all of my apps (at least this one I can still fix :D)


Free PHPUnit Training

Free is always better!!!!

Found this gold mine in the Zend Developer zone.

“It is a workshop that builds a small demo application the Test Driven Development way. The reason I open this workshop course is because I strongly believe everybody should understand and use PHPUnit in their projects”

More info at: http://devzone.zend.com/article/12765-Free-PHPUnit-Training

Direct link: http://blog.nickbelhomme.com/php/phpunit-training-course-for-free_282


PHPUnit Overview Slideshow

Searching for some information regarding the PHPUnit output buffer I have came across this slideshow that shows perfectly how to start with PHPUnit.

Follow the link: http://www.slideshare.net/sebastian_bergmann/introduction-to-phpunit-best-practices

It’s not a big slideshow, only 40 slides, that you can easily take in about 1/2 hour or so.

Have fun.


PHPUnit

I have just started to play with PHPUnit and I can easily say that this is a must have tool for every PHP developer.

Quoting Matthew Weiner O’Phinney:

“If you are not doing unit-testing, then you are not a good developer.”

This might sound harsh but is in fact a bit of a truth since most of the developers don’t really mind or have the time to go on testing the scripts in a block or whole application overview.

Developing the scripts for PHPUnit are not hard, even though I’m not an expert on the area, it don’t take too much time to develop a good script to test your scripts.

The tests are based on assertions and mock-ups from how your script should work. It will basically create a very least minimal requirement to see if your script is working.

Let’s say, if your object have to set 3 properties, call a method and then another object should load this just created object, the test script should cover all of this.

Afterwards is just run, at command prompt, phpunit yourScript.php and check for the results.

As I have mentioned before, I’m no expert since I have just started to learn PHPUnit, so I’m going to keep this post small until I can create a small tutorial of how to work with PHPUnit but meanwhile:

To install:

Install it using PEAR – It’s the easier way. If the install fails because PEAR is out of date, then run pear upgrade-all to update pear and all packages installed.

Check out Chapter 3 of PHPUnit online documentation to learn how to fully install it: http://www.phpunit.de/manual/3.0/en/installation.html

To write tests:

Check out Chapter 4 to learn the basics of writting tests to run PHPUnit. The online documentation is not big and it’s worth to read it fully. Anyway keep the Chapter 20, about the API, in your perspective to really see how PHPUnit can help you to make your scripts even stronger.