How do I test this class?

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

How do I test this class?

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How do I test this class?

Post 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 :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: How do I test this class?

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How do I test this class?

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: How do I test this class?

Post 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"
Post Reply