3

so this is the class that i want to test. and specifically i just pick one of the function that i want to test. while var is a value returned from doing some function from classB bar is instance from classC and then do some function which pass some variables. for most of the hints/example, the function to be tested is return a value. so my question is, how to test that this particular function worked?

thanks.

class mA extends A {
    ...
    function doSomething($foo) {
        $var = doStuffFromClassB("hallo");
        $bar = ClassC::instance();
        $bar->doStuffFromClassC($var, $foo, "world");
    }
}

1 Answer 1

6

If it's called doSomething and it doesn't indicate what it does by returning a value, then you can use mock objects to trace the interaction with the other objects.

See PhpUnit's documentation on mock objects. I guess in this case you want to verify that the doStuffFromClassC method is involved with the var from doStuffFromClassB.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks 4 the suggestion. but i have following questions to ask: [1] so to test those methods (doStuffFromClassB & C), should I write it in one test or write separate test for each doStuffFromClass*? [2] or should I just need to test those methods from the TestCase of class B and C instead of test the void function in class mA?
For Unit testing, you want to isolate the class you are testing away from all of the implementation of other classes. If mA's code just calls the methods on B and C, then verify that. B and C can be unit tested separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.