Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-08-2007, 05:58 PM
Junior Member
 
Join Date: Jul 2007
Location: The Netherlands
Posts: 15
Default [SOLVED] How can I imitate Zend_Db_Table results with PHPUnit?

Hello, though being new to both the Zend Framework and PHPUnit, I am now trying to write unit tests.

Now I want to test if the latest entries are being shown, so I though I'd insert a Stub that pretends to be the "Entries" class which should extend Zend_Db_Table. However, I don't know how I should set the return value to the results. I currently have this code:
PHP Code:
$entries $this->getMock('Entries');
$entries->expect($this->any())
          ->
method('fetchAll')
          ->
will($this->returnValue('foo')); 
Here, I'd like to replace 'foo' with fake results from Entries::fetchAll() . How do I do that?

Thanks in advance.

Last edited by Vincentt : 07-18-2007 at 01:00 PM. Reason: Solved :D
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-18-2007, 01:00 PM
Junior Member
 
Join Date: Jul 2007
Location: The Netherlands
Posts: 15
Default

I've found the way to do it, in case anyone's interested:

PHP Code:
// Zend_Db_Table_Rowset contains the fake results
Zend_Loader::loadClass('Zend_Db_Table_Rowset');
// Create a new Zend_Db_Table_Rowset .
// You can pass it data by passing it an array as parameter
// This array contains configuration parameters
// 'data' contains an array with the database rows as values
// Each of the database rows is yet another array,
// obviously, the key is the column and the value the value
$mockResults = new Zend_Db_Table_Rowset(array('data' => $data));
// Then get the script to think that fake entry comes from the database
$entries $this->getMock('Entries');
$entries->expects($this->any())
           ->
method('fetchAll')
           ->
will($this->returnValue($mockResults)); // DB results 
So for example, the $data array could look like this:

PHP Code:
$data = array(
    
// First row:
    
array(
        
'id' => 0,
        
'title' => 'Test result 1',
    ),
    
// Second row:
    
array(
        
'id' => 3,
        
'title' => 'Yet another test result',
    ),
    
// etc.
); 
And you would pass the Zend_Db_Table_Rowset class an array of configuration parameters with the above array as the respective value of the 'data' key.

Hope that helped anyone,

Vincent
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 09:19 PM.