Problem constructing mock object of a Zend component.

56 views
Skip to first unread message

Andree Surya

unread,
Jun 29, 2010, 8:37:12 AM6/29/10
to phpmo...@googlegroups.com
Hi there,

I have been using Mockery for a while. Great job, I would say. Very intuitive interface, and the readme file pretty much explains everything.
However, I'm currently having a problem constructing a mock object of a Zend component.

When I do this ..
\Mockery::mock('Zend_Db_Table_Rowset_Abstract');

.. I always got the following error:

BadMethodCallException: Method ::__wakeup() does not exist on this mock object

D:\programs\xampplite\php\PEAR\Mockery\Container.php:246
D:\programs\xampplite\php\PEAR\Mockery\Container.php:96
D:\programs\xampplite\php\PEAR\Mockery.php:49
D:\documents\eclipse_workspace\project_name\tests\application\module_name\models\SampleTest.php:24

Looking at the source code, I found the following method at \Mockery\Container.php line 246:

    protected function _getInstance($mockName)
    {
        if (!method_exists($mockName, '__construct')) {
            $return = new $mockName;
            return $return;
        }
        $return = unserialize(sprintf('O:%d:"%s":0:{}', strlen($mockName), $mockName));
        return $return;
    }

Well, I do actually have several questions in my mind, like why the constructor is not found, why do mockery have to perform serialization/unserialization. But basically what I want to do is to make a mock object of Zend_Db_Table_Rowset_Abstract. Can anyone help me?

Pádraic Brady

unread,
Jun 29, 2010, 9:53:56 AM6/29/10
to phpmo...@googlegroups.com
Hi Andree,
 
I'll take a look later this evening if I get a chance. The handling of a few areas like __wakeup and __sleep probably need special handling but I can confirm that later and work on adding it for a mini-release.
 
The reason for the unserialisation is that it's a quirky method that forces PHP to instantiate an object from a class without calling the constructor. Constructor handling for mock objects is really hard, usually not desireable (mocks don't need a startup state relying on dependencies), and tends to cause parameter hell (see PHPUnit).
 
PHPUnit actually uses the same short-cut to instantiate its own mocks I think.
 
As a result, __wakeup and __sleep need specific workarounds. I'll see what's needed to minimally allow them to pass through without errors.
 
Best regards,
Paddy
 
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
OpenID Europe Foundation Irish Representative



From: Andree Surya <andree...@gmail.com>
To: phpmo...@googlegroups.com
Sent: Tue, June 29, 2010 1:37:12 PM
Subject: Problem constructing mock object of a Zend component.
Reply all
Reply to author
Forward
0 new messages