Note that there are some explanatory texts on larger screens.

plurals
  1. POZend Framework: How to unit test a model using Zend_Service_Twitter
    primarykey
    data
    text
    <p>I have been getting into Unit Testing with Zend Framework. I am getting used to the other things it provide but I am having a hard time understanding Mock Objects.</p> <p>For this example, I am trying to use a Mock Object to test out my model.</p> <pre><code>&lt;?php class Twitter_Model_Twitter { private $_twitter; /** * Make the options injectable. * __contruct($auth, $key) */ public function __construct() { $config = new Zend_Config_Ini(APPLICATION_INI, APPLICATION_ENV); $key = $config-&gt;encryption-&gt;salt; $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $password = mcrypt_decrypt(MCRYPT_XTEA, $key, $password, MCRYPT_MODE_ECB, $iv); $this-&gt;_twitter = new Zend_Service_Twitter($username, $password); } public function verifyCredentials() { return $this-&gt;_twitter-&gt;account-&gt;verifyCredentials(); } public function friendsTimeline($params) { return $this-&gt;_twitter-&gt;status-&gt;friendsTimeline($params); } } </code></pre> <p>For my unit test:</p> <pre><code>require_once ('../application/models/Twitter.php'); class Model_TwitterTest extends ControllerTestCase { /** * @var Model_Twitter */ protected $_twitter; public function testfriendsTimeline() { $mockPosts = array('foo', 'bar'); //my understanding below is: //get a mock of Zend_Service_Twitter with the friendsTimeline method $twitterMock = $this-&gt;getMock('Zend_Service_Twitter', array('friendsTimeline')); /* line above will spit out an error: 1) testfriendsTimeline(Model_TwitterTest) Missing argument 1 for Mock_Zend_Service_Twitter_9fe2aeaa::__construct(), called in /Applications/MAMP/bin/php5/lib/php/PHPUnit/Framework/TestCase.php on line 672 and defined /htdocs/twitter/tests/application/models/TwitterTest.php:38 */ $twitterMock-&gt;expects($this-&gt;once()) -&gt;method('friendsTimeline') -&gt;will($this-&gt;returnValue($mockPosts)); $model = new Twitter_Model_Twitter(); $model-&gt;setOption('twitter', $twitterMock); $posts = $model-&gt;friendsTimeline(array('count'=&gt;20)); $this-&gt;assertEquals($posts, $mockPosts); } } </code></pre> <p>How would you test the following? 1) verifyCredentials() 2) friendsTimeline()</p> <p>Thanks, Wenbert</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload