David Pauli
David Pauli

Reputation: 89

PHPUnit: Mocking objects in 'Wrapper' objects

I don't know whether this works, but I don't find any information how to do this in the documentation or similar blogs:

I want to test my class A with some function calls of class B in their:

class A {

    function foo() {

       B::doSomeThings();
    }
}

For not using the real class B I want to mock this class. If I Unit test my object A I only find the solution to mock this object like:

$mockA = $this->getMockBuilder('\A')->setMethod('foo')->getMock();

$mockA->expects($this->once())->method('foo')->will(...)

Is it possible to only mock the function doSomeThings() in class B

Upvotes: 0

Views: 174

Answers (1)

fazi
fazi

Reputation: 11

There are some workarounds

http://miljar.github.io/blog/2014/01/29/phpunit-testing-static-calls/

or you can move to other method in A function and like

class A {

    function foo() {

     $this->doSomeThings();
    }

    function doSomeThings() {
       B::doSomeThings();
    }
}

and mock doSomeThings() function in A class

Upvotes: 1

Related Questions