Julian
Julian

Reputation: 9130

PHPUnit stub only works for the first method

I'm trying to stub Amazon's instance metadata class because it calls an internal URL that only works within an EC2 instance and can't be called from outside. My problem is that the method "send" is not recognized. The method "get" works fine though. This is the error Fatal error: Call to undefined method Stub\Amazon\StubInstanceMetadata::send() in ...etc, etc

    $stub = $this->getMockBuilder('Aws\Common\InstanceMetadata\InstanceMetadataClient')
                 ->disableOriginalConstructor()
                 ->getMock();

    $stub->expects($this->any())
         ->method('get')
         ->will($this->returnValue($this));

    $stub->expects($this->any())
         ->method('send')
         ->will($this->returnValue(json_encode(array('test' => 'value'))));

EDIT: This ended up being a combination of two different issues. The first one being the one pointed out by @fab. The second being that to return a reference to $this, PHPUnit has it's very own method, so I should have done this:

    $stub->expects($this->any())
         ->method('get')
         ->will($this->returnSelf()); // don't use returnValue() here

Upvotes: 1

Views: 302

Answers (1)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

I don't know the original class but it looks like send is not actually a method of it but rather called via magic __call. So if you want to mock it, you will have to do it explicitly:

...->getMock(array('get', 'send'));

Upvotes: 1

Related Questions