Page 1 of 1

How do I test this class?

Posted: Tue Jan 13, 2009 9:42 pm
by LonelyProgrammer
I just start practicing proper OOP with PHP5 (using private/public access properties) and found myself wondering how to test this class using TDD.

Code: Select all

 
public function securityCheck()
    {
    
    }
    
    private function checkDuplicateTXN()
    {
        
    }
    
    private function checkTamperedEmail()
    {
        
    }
    
    private function checkIncompletePayment()
    {
        
    }
    
    private function checkWrongCurrency()
    {
        
    }
    
    private function checkWrongPayTo()
    {
        
    }
    
    private function processErrors()
    {
        
    }
 
The point of entry into this class is securityCheck(), where two sets of data will be compared with each other. The return results would be an array of flags, indicating which checks have failed, be it an incorrect invoice number, tampered email and etc.

I would like do test each of the function individually. Any advice on how should I do that, or do I need to define a different set of data for each case?

Re: How do I test this class?

Posted: Wed Jan 14, 2009 12:23 am
by Chris Corbyn
If those methods are private then you should not be testing them. However, it looks as though each method would have some value if it was made public? Would it hurt anything if you were able to perform each check by itself?

Make them public and test each one :)

Re: How do I test this class?

Posted: Sun Jan 25, 2009 3:26 pm
by josh
Corrrection, you SHOULD test them but you should not test them directly. Your tests should test the public methods which in turn call the private methods. If the private methods fail the tests should be crafted in such a way that the main tests fail too. Basically you just need to think of your ideal API, write it up, figure out what you expect a given function to return, and write tests for each different scenario

Try the tutorial on the simpletest page

Re: How do I test this class?

Posted: Mon Jan 26, 2009 7:14 am
by Chris Corbyn
Sorry, I was midleading with my previous answer there. jshpro2 is right. Basically although you're sending all data through one public method, you still wants tests for the various scenarios like testUsingWrongCurrencyFailsCheck() and so on.

Re: How do I test this class?

Posted: Mon Jan 26, 2009 9:50 pm
by josh
Yeah but at the same time you don't want one extreme or the other. You don't want only 1 unit test for your application and a ton of private helper methods, at the same time you don't want a "god method"