Parris Varney
Parris Varney

Reputation: 11488

phpunit - Only mocking some methods in a class

I have a class that has a single method I would like to mock, but I'm having trouble getting phpunit to cooperate.

An example:

  /**
   * @test
   */
  public function mockTest() {
    $mock = $this->getMock('ApiMock', array ('search'));
    $mock->expects($this->once())
      ->method('search');

    $mock->search('test');
    $mock->somethingElse('another test');
  }

With this class:

class ApiStub {
  public function search($var) {
    return $var;
  }

  public function somethingElse($var) {
    return $var;
  }
}

Throws the following error:

PHP Fatal error:  Call to undefined method Mock_ApiMock_14fd352a::somethingElse()

I would like to be able to mock the search() function, but still have somethingElse() do it's usual things.

Upvotes: 0

Views: 1123

Answers (1)

Parris Varney
Parris Varney

Reputation: 11488

For any future people with a similar problem.

The issue was the ApiStub was in the same file as the test cases, which happened to be in a namespace. I fixed the problem like so:

$mock = $this->getMock('namespace\subnamespace\ApiMock', array ('search'));

Upvotes: 1

Related Questions