Testing Zend Framework 2 code which uses a HTTP Request object.

Yesterday I was trying to test that the ServiceManager was correctly creating an instance of a class which took a Zend\Http\Request object as a parameter to the constructor. The problem I ran into was that since the Zend MVC Application had been initialised from PHPUnit, it decided it was running in a console context and was using a Zend\Console\Request instead.

After a little bit of hunting around I came up with the following simple solution:
Continue reading

Should you test private methods?

While trying to learn TDD I found myself asking this question fairly often. I’ve searched for the answer online many times and always got mixed responses. I have also devised many clever ways to try and test them and even ways to mock $this. However now I’ve got to a point where I’m confident I have a very valid answer to this question and I’m going to try to explain it here… Continue reading

Using custom form elements with the AnnotationBuilder in Zend Framework 2

Just a quick little tip – Since ZF 2.2 we now have the FormElementManager service available which make is nice as easy to register new custom form elements and fieldsets in your ZF2 application (see Creating Custom Elements). However I quickly discovered that it wasn’t possible to use custom elements easily in my forms if the form was created using the annotation builder. Continue reading

Neat exception classes with traits

So we’re all told that our exception messages should be nice and informative and often sprintf is used to format the exception messages. This is all well and good and is very helpful when an exception gets thrown. However it leads to code that looks like this:

Continue reading