Question

I normally use getter and setter methods on my objects and I am fine with testing them as mock objects in SimpleTest by manipulating them with code like:

Mock::generate('MyObj');
$MockMyObj->setReturnValue('getPropName', 'value')

However, I have recently started to use magic interceptors (__set() __get()) and access properties like so:

$MyObj->propName = 'blah';

But I am having difficulty making a mock object have a particular property accessed by using that technique.

So is there some special way of setting properties on MockObjects.

I have tried doing:

 $MockMyObj->propName = 'test Value';

but this does not seem to work. Not sure if it is my test Subject, Mock, magic Interceptors, or SimpleTest that is causing the property to be unaccessable.

So, in summary:

I can mock-up methods on my mock objects but i am having trouble mocking-up class properties of mock objects. Is it possible to set properties on a Mock Object in Simpletest?

Any advice welcome.

Was it helpful?

Solution

In answer to my own question...

Yes it is possible to set the properties of mocks of objects that use magic interceptors - just set the return value of the interceptor method like you would with any other method.

SimpleTest Example Mocking Intercepted Properties on Mocked Objects:

for this object

class MyObj 
   {

   public function __set($name, $value)
    {
    $props[$name] = $value;
    }

   public function __get($name)
    {
    return $props[$name] = $value;
    }

   }

a client (tested aggregator class) can access the properties like so

$MyObj->propName = 'blah';
echo $MyObj->propName; //prints blah

and it can be mocked like so

Mock::generate('MyObj');
$MockMyObj = new MockMyObj();
$MockMyObj->setReturnValue('__get', 'test property value', array('propName'));

//...later on...
echo $MockMyObj->propName; //prints "test property value"

P.S. Here are some more docs about mocks: http://www.simpletest.org/en/mock_objects_documentation.html

P.P.S

I did actually try it before, but my experimentation was obscured by a typo in my code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top