dennis-fedco Posted March 21, 2014 Share Posted March 21, 2014 say I have a function called load() in a huge legacy codebase. I supply it variables A, B, and C, and it produces variables X, Y, and Z. Aside from those variables, there class itself where load() is located has a whole set of existing variables, call them V1, V2, ... Vn. Some of these may be used in computation of X, Y, Z, but aside from they stay unchanged. When it comes to testing, there are two ways I can go about it: Test with assert that X, Y, Z do not exist yet, then call load() and verify that X, Y, Z now have values. This requires more work -- tracking down what is actually being changed and plucking out the actual changes and putting them into tests. Dump entire variable set into test, i.e. print_r($this) before the test, and print_r($this) after the test. Compare those huge print-outs before and after. Benefit: less work - just lots of copy and paste and still gets the work done. It also tests for any side-effects that may be introduced somehow with the legacy code base, so this may have extra benefits over the first approach in that this will catch any out-of-band errors as well. i.e. if variables V do change due to a side-effect, this will be more easily caught. Is there a school of thought on this? Which way/method do I use? Quote Link to comment https://forums.phpfreaks.com/topic/287160-phpunit-testing-legacy-code/ Share on other sites More sharing options...
trq Posted March 21, 2014 Share Posted March 21, 2014 Test with assert that X, Y, Z do not exist yet, then call load() and verify that X, Y, Z now have values. functions can not dump variables into the global namespace so that makes statement doesn't make any sense to me. Really, without seeing some example of what your talking about, I'm finding your post pretty hard to follow. Unless the code base was written with tests in mind it is highly unlikely that it is going to be very "testable". Your already talking about dependencies on variables from outside. This alone breaks the "unit" in "unit test". Quote Link to comment https://forums.phpfreaks.com/topic/287160-phpunit-testing-legacy-code/#findComment-1473493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.