Question

For example, how would I test the static createFromGlobals method in the Symfony Request class?

I saw this example, but it only apply's when caller and callee are in the same class.

$class::staticExpects($this->any())
      ->method('helper')
      ->will($this->returnValue('bar'));

What is the preferred method to do this. An example with Mockery would be fine also. They talk about a class alias mock without an example.

Was it helpful?

Solution

If you are using a static call in your code, you are stuck with depending on what that static call is actually doing. It isn't possible to mock it.

Depending on what you are doing, you may not even want to mock it. If you are testing your controller, for example, you don't need to mock it. Post via whatever method the appropriate method (GET, POST, etc) and let the request object be created. Replace only whatever services are external to your system (Database, etc) and make sure that your response is correct. This test isn't strictly a "unit" test but shows that all your classes are working properly together.

If you are creating, this object in a model somewhere change the design so that the Request object is passed in. Then you can create a mockRequest with the proper behavior and without having to try to mock a static method call.

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