4

I'm trying to mock properties but can't get it to work. In this case I'm trying to mock request property Symfony\Component\HttpFoundation\Request.

According to this answer I should return a value for __get

Following code keeps showing 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 it's because the answer is too old so checking out the current documentation of mocking but that one doesn't 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'));

That 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 completely lacks any examples of that.

If I try echo ($this->request->request->get('start')); it will fail on expecting search but getting start. There is no way I can give it something that doesn't fail.

Trying to unit test but Symfony2 does not seem to have such a thing. Every documentation almost immediately skips unit testing with examples of ordinary classes needing no dependencies and then moves to functional testing. And some documentation flat out say unit testing for repositories is not recommended.

I would like to see if DQL statements are created correctly though and am left with no documented option to unit test this.

Can do this part by creating a fake Doctrine\ORM\EntityRepository class and include that file at the start of my test. Tried the same with Symfony\Component\HttpFoundation\Request but get a can't re define this class when trying to do that. Somehow when my test is loaded Symfony\Component\HttpFoundation\Request already is loaded.

Community
  • 1
  • 1
HMR
  • 37,593
  • 24
  • 91
  • 160

1 Answers1

1

Not entirely sure how you mean mock properties, I would assume properties are set via getters and setters and then you could just set them with whatever Mock you want.

If however you need to assign a mock object to a private /protected property for which you do not have an accessor method, you can do this:

$reflection = new \ReflectionClass($objectThatContainsThePropertyToMock);
$property   = $reflection->getProperty($propertyName);
$property->setAccessible(true);
return $property->setValue($mockedObject);
malte
  • 1,439
  • 1
  • 11
  • 12