0

In a test which extends a WebTestCase I want to test something, which depends on passing on GET parameters.

A solution for POST parameters can be found here: Symfony2 Functional test: Passing form data directly

But if I use this analog for GET it does not work.

test code:

    // given
    $client = static::createClient();
    $paramValue = 'frwkuh437iwaw';

    // when
    $client->request(
        'GET',
        '/',
        ['paramName' => $paramValue],
        [],
        [
            'HTTP_Host'            => 'boys2go.com',
            'REMOTE_ADDR'          => '127.0.0.1',
            'HTTP_X-FORWARDED-FOR' => '127.0.0.1',
        ]
    );

Part in the class, where they are been evaluated:

        $crossSaleParametersValue = $requestStack
            ->getCurrentRequest()
            ->query->get('paramName');

 if (!empty($crossSaleParametersValue)) {
            echo 'crossSale';
}

Has anybody an idea how I can pass on my GET parameters?

The dirty attempt in just adding them after the '/' doesn't work neither.

How can I get my GET parameters into my test?

1 Answer 1

2

A solution could be:

// Preferable, write this method in a parent class (that extends abstract
// class WebTestCase) to be used in other tests too
protected static function getUrl($route, $params = array())
{
    return self::getKernel()->getContainer()->get('router')->generate($route, $params, UrlGeneratorInterface::ABSOLUTE_URL);
}

public function testDemo() {
    $client = static::createClient();
    $paramValue = 'frwkuh437iwaw';
    $url = $this->getUrl('route_name');

    // when
    $client->request(
        'GET',
        $url . '?paramName=' . $paramValue,
        [],
        [],
        [
            'HTTP_Host'            => 'boys2go.com',
            'REMOTE_ADDR'          => '127.0.0.1',
            'HTTP_X-FORWARDED-FOR' => '127.0.0.1',
        ]
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

What is the difference between just attaching the string without calling getUrl? the $params parameter of your getUrl - what should go there? My array with GET parameters?
If you have an route in Symfony defined like this: @Route("/edit/{id}", name="edit"), $params from getUrl() should contain an array ['id'] => 99. In your tests, you send a query parameter, it's different.

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.