Reputation: 2557
I am new in testing. I want to test my service and function, but this gets $_GET parameter. How I can simulate get parameter in test?
Upvotes: 1
Views: 982
Reputation: 6012
When using Symfony2, you should abstract your code away from direct usage of PHP superglobals. Instead pass a Request object to your service:
use Symfony\Component\HttpFoundation\Request;
class MyService
{
public function doSomething(Request $request)
{
$foo = $request->query->get('foo');
// ...
}
}
Then, in your unit tests, do something like:
use Symfony\Component\HttpFoundation\Request;
class MyServiceTest
{
public function testSomething()
{
$service = new MyService();
$request = new Request(array('foo' => 'bar'));
$service->doSomething($request);
// ...
}
}
You could also consider making your service even more generic, and just pass the values you want when calling it's methods:
class MyService
{
public function doSomething($foo)
{
// ...
}
}
$service = new MyService();
$service->doSomething($request->query->get('foo');
Upvotes: 4