How to set a variable within a mock object How to set a variable within a mock object php php

How to set a variable within a mock object


Here is what works for me:

$stub = $this->getMock('SomeClass');$stub->myvar = "Value";


Don't know why this works but it seems to for me. If you put the __get magic method as one of the overridden methods e.g.

$mock = $this->getMock('Mail', array('__get'));

You can then you can successfully do

$mock->transport = 'smtp';


The idea of a stub is to replace a dependency with a test double offering the same method interface that (optionally) returns configured return values. This way, the SUT can work with the double like it was the dependency. If you need a specific return value from the stub, you just tell it what it should return, e.g.:

// Create a stub for the SomeClass class.$stub = $this->getMock('SomeClass');// Configure the stub.$stub->expects($this->any())     ->method('doSomething')     ->will($this->returnArgument(0));$stub->doSomething('foo'); // returns foo

See http://www.phpunit.de/manual/current/en/test-doubles.html