1
votes

I have a class that has two public method. It looks something like following:

class IntRequest
{
    public function updateStatus()
    {
        $isValid = $this->checkValidity();

        // ... next is a complex logic that use $isValid
    }

    /**
     * @return bool
     */
    public function isValid()
    {
        // another complex logic
    }
}

I need to test a first function - IntRequesr::updateStatus; however I need to run to tests. The first one with IntRequests::isValid returns false and the second one with true as a result of IntRequests::isValid

I try to mock that function but tests run with calling actual IntRequests::isValid not mocked one.

My testing code is

$intRequest = new IntRequests;

$mock = m::mock($intRequest);
$mock->shouldReceive('isValid')
    ->once()
    ->andReturn(true);

$res = $mock->updateStatus();

$this->assertTrue($res);

I've try to call $res = $intRequest->updateStatus() instead of $res = $mock->updateStatus() but with no luck.

So, I am wondering is it possible to mock function that is called inside testing method?

1

1 Answers

1
votes

You need a partial mock (a mock object, in which some of the methods are stubbed, while the rest are left as is). Since I've done such only with the phpunit's own mock library, I can only point you to the documentation, but it seems that you should just add ->makePartial() to your mock instantiation