Calamity Jane
Calamity Jane

Reputation: 2686

Symfony2: pass GET parameters for a phpunit functional test

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?

Upvotes: 0

Views: 2771

Answers (1)

Cristian Bujoreanu
Cristian Bujoreanu

Reputation: 1167

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',
        ]
    );
}

Upvotes: 2

Related Questions