How to simulate the properties of an object?

I am trying to make fun of properties, but cannot make it work. In this case, I am trying to make fun of a query property Symfony\Component\HttpFoundation\Request.

According to this answer, I have to return a value for__get

The following code continues to show NULL:

$testRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
  ->disableOriginalConstructor()
  ->getMock();
$testRequest->expects($this->any())
  ->method('__get')
  ->with($this->equalTo('request'))
  ->will($this->returnValue("working"));
var_dump($testRequest->request);

Maybe because the answer is too old, so check the current documentation of mockery, but this does not mention how to mock properties at all.

According to another answer, I can try the following:

private $post=array('search'=>'abc','start'=>null);
...
$paramBag = $this->getMockBuilder('Symfony\Component\HttpFoundation\ParameterBag')
  ->disableOriginalConstructor()
  ->getMock();
foreach($this->post as $key=>$value){
  echo "setting:".$key.' with:'.$value.PHP_EOL;              
  $paramBag->expects($this->any())
    ->method('get')
    ->with($this->equalTo($key))
    ->will($this->returnValue($value));
}
$this->request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
  ->disableOriginalConstructor()
  ->getMock();
$this->request->request=$paramBag;
echo ($this->request->request->get('start'));

This gives me:

Expectation failed for method name is equal to <string:get> when invoked zero or more times
Parameter 0 for invocation Symfony\Component\HttpFoundation\ParameterBag::get('search', null, false) does not match expected value.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'search'
+'start'

Is it not possible to override multiple methods with different values? Again, the documentation is completely missing any examples of this.

echo ($this->request->request->get('start'));, , . -, .

unit test, Symfony2, , . , , . - .

, DQL, unit test.

, Doctrine\ORM\EntityRepository . Symfony\Component\HttpFoundation\Request, can't re define this class. -, , Symfony\Component\HttpFoundation\Request .

+4
1

, , , , , .

private/protected, , :

$reflection = new \ReflectionClass($objectThatContainsThePropertyToMock);
$property   = $reflection->getProperty($propertyName);
$property->setAccessible(true);
return $property->setValue($mockedObject);
+1

All Articles