I have been learning MVC recently and have started creating my own framework (for learning purposes only, of course) from http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/
I want to be able to extend these base classes but first I would like to be able to unit test them. Does anyone have an idea how I would go about testing the base Controller, Model and Template classes?
Thanks in advance!
Just write test classes, preferably mirroring your framework structure (eg ControllerTest
, ModelTest
, etc), that put the classes through their paces, meaning putting in some data and checking the output. If your code is well-structured, should be quite easy to implement.
Edit
Basically, what you test in a unit test is if ( Class::methodToTest( $input ) === $expected_output )
. The output must always be identical for the same $input
. If this is not the case, or you can't test write a test case like this, it's often an indicator that your code is not well structured (object oriented and loosely coupled).
Your Template::render()
method for example, is not testable because it prints the data instead of returning it. Now, you could work around this by using ob_start()
, but better anyway would be to chop the function into smaller parts, that return values instead of directly printing them.
It's a bit abstract, but I hope you get the point.