1

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?

0

1 Answer 1

4

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.