Created
June 15, 2011 21:33
-
-
Save s0enke/1028181 to your computer and use it in GitHub Desktop.
Stubbing protected methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'PHPUnit/Framework/TestCase.php'; | |
class CouchDB | |
{ | |
public function doGeloet() | |
{ | |
return $this->makeRequest(); | |
} | |
protected function makeRequest() | |
{ | |
return 'doing external shit'; | |
} | |
} | |
class CouchDBTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testYourMum() | |
{ | |
$couch = $this->getMock('CouchDB', array('makeRequest')); | |
$couch->expects($this->any())->method('makeRequest')->will($this->returnValue('Stubbed!')); | |
$this->assertEquals('Stubbed!', $couch->doGeloet()); | |
} | |
} |
For some reason, even if I add array('makeRequest'), everything else is stubbed too.
So I need to configure an expectation for everything -- kinda defeats the purpose.
Yeah that's strange and not intended. Btw i use phpunit 3.5.14.
If it still does not work, please post your updated code. I think we're getting closer ;)
Yeah, working now. I probably didn't have getMock('foo', array('protectedMethod')). That's the only thing I can think of. Though I'm sure I tried that before. ;-(
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hrhr ;)
Here's an example that is closer to your example code:
Now you can set the stubbed server response with will($this->returnValue(json_encode(array('blub')))
Hth!