views:

69

answers:

3

How do I unit-test a Zend_Form form class I wrote for my application?
I aspire to have a 100% code coverage, so just testing each controller that uses it, doesn't seem enough, but I could be missing something in the UnitTest approach..

A: 

Unit testing stands for testing of units, so you should

  • test your Zend_Form based class - you can pass any test data to populate method
  • than test your controller separately - create mock for your Zend_Form based class, because you are going to test it again, this requires some sort of factory or dependency injection in controller.

The LoginForm class in the example from comment doesn't provide more functionality than setting elements, validators, decorators etc., so this may be tested easily. If you want to test the controller, you should pass it (e.g. as constructor parameter, see Dependency injection) some mock of LoginForm. If you want to test controller and form intergration (which is not unit test, but one can do it also with PhpUnit), you can use controller's method setRequest to set fake request data.

Steve
Thanks steve, however you didn't quite answer my question - I asked *how* to test it, and not whether I should (I just mentioned why I was asking what I asked about testing Zend_Form)
Doron
And what you exactly want to test? You can create the instance, check whether constructor created all the elements correclty with all their properties and validators or do you have some other specific functionality in your Zend_Form based class?
Steve
Let's consider the form in the following guide:http://weierophinney.net/matthew/archives/165-Login-and-Authentication-with-Zend-Framework.htmlHow would one write the most comprehensive test for it?I'm asking to know all the possibilities to test a form.I know I can assert that the properties are what the constructor should have set, but none of the methods don't return any values (though they could have return, like in a translate method or etc..)
Doron
Answer updated.
Steve
A: 

You can try it with Selenium. This way you can prepare inputs and expected outputs. However, it requires that you actually display the form in a browser, so your controller has to work as well.

Larry_Croft
Hey Larry.Selenium provides a full functionality test opportunity (and it does it really good!) but as I wrote in the question, I am talking about unit-test.
Doron
A: 

What about writing test cases that POST form data to your action controller? This way you can generate as many different user inputs as you want and you can check whether your form validation works or whether you get correct error messages.

Marcin