Reputation: 432
I'm new to PHP Unit and now I have a dilemma.
Function structure is something like this:
function myfunc($arg) {
$data = $anotherobject->myfunc2($arg);
$diffdata = $anotherobject->myfunc3($arg);
return $data. " ".arg. " ". $diffdata;
}
How can I verifiy that the output is what it should be?
Upvotes: 0
Views: 70
Reputation: 1391
Edit: Jasir is of course also right. The point of unittesting is to test a unit as small as possible. So you would also make tests to cover myfunc2() and myfunc3().
End of edit
Using a stub, you can set myfunc2() and myfunc3() to return a known value. You can then assert the return of myfunc as you would normally do.
Something along the lines of:
<?php
require_once 'SomeClass.php';
class StubTest extends PHPUnit_Framework_TestCase
{
public function testStub()
{
// Create a stub for the SomeClass class.
$stub = $this->getMock('SomeClass');
// Configure the stub.
$stub->expects($this->any())
->method('myfunc2')
->will($this->returnValue('foo'));
$stub->expects($this->any())
->method('myfunc3')
->will($this->returnValue('bar'));
// Calling $stub->doSomething() will now return
// 'foo'.
$this->assertEquals('foo somearg bar', $stub->myfunc('somearg'));
}
}
?>
Upvotes: 2
Reputation: 1471
You should test myfunc() output only. If you need to test myfunc2(), myfunct3(), make separate tests for them.
function test_myfunc() {
...
}
function test_myfunc2() {
...
}
function test_myfunc3() {
...
}
Upvotes: 1