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:
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
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…