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

Using the @ComposedObject Zend Framework 2 Form Annotation

Recently I started playing with Zend Framework 2 Form Annotations which certainly makes building forms much simpler (if you have not heard about this yet see Matthew Weier O’Phinney’s post and the ZF2 Documentation). After getting it up and running I found out about about the @ComposedObject annotation which seemed very useful, however it took a bit of fiddling to get it up and running, here’s how I did it. Continue reading

Getting the ServiceManager into the test environment and Dependency Injection

So far I’ve got the basic idea of how to test a ZF2 Controller with PHPUnit. However I had problems testing all my actions that used the AlbumTable class to load data from the database because I had no access to the ServiceManager which is used to provide AlbumTable to the controller. Obviously solving this was my next step…
Continue reading

Getting PHPUnit working with a Zend Framework 2 MVC application

Now that I’ve got a little MVC application up and running in Zend Framework 2 (see my previous post) I decided it was time to try and get PHPUnit to play with it. While I don’t think I’m alone in wanting to use ZF2 & PHPUnit together it’s certainly something not many people are talking about.

Here I will show how I bootstrapped PHPUnit with the Zend Framework. Continue reading

Getting started with Zend Framework 2

After a little Google search for a good intro into the Zend Framework 2 I came up with Rob Allen aka Akrabat’s tutorial. I’d had a little play with ZF2 before when it was in beta 3 or beta 4 but after opening up beta 5 I was quite surprised to see how much things had changed.

Rob has not yet updated his tutorial but I understand an update is coming very very soon, while meaning that some things in the tutorial didn’t work it gave me the opportunity to do some digging into the Zend Framework code & documentation to get it up and running.


UPDATE!
Rob has now updated his tutorial which you can download from here.


Continue reading