Skip to content

server parameters for testing #9527

Closed
Closed
@stanguy

Description

@stanguy

In the testing documention, it is written that there are 2 ways to set up server parameters, by either passing them to static::createClient() (which works) or by using the 5th parameter for $client->request() (which doesn't).

For the sake of the example (and because I'm not sure how to make a unit test for the unit test), I modify the standard edition application with the following:

In Symfony/src/Acme/DemoBundle/Controller/DemoController.php:

    public function indexAction()
    {
        return array( 'host' => $this->getRequest()->getHost() );
    }

In Symfony/src/Acme/DemoBundle/Resources/views/Demo/index.html.twig:

    <h1 class="title">Available demos</h1>
    <p>{% if host is defined %}host: {{ host }}{% endif %}
    <ul id="demo-list">

And the tests in Symfony/src/Acme/DemoBundle/Tests/Controller/DemoControllerTest.php:

class DemoControllerTest extends WebTestCase
{
    public function testIndexOnClientCreation()
    {
        $client = static::createClient( array(), array(
            'HTTP_HOST'       => 'en.example.com',
            'HTTP_USER_AGENT' => 'MySuperBrowser/1.0',
        ));

        $crawler = $client->request('GET', '/demo/' );

        $this->assertEquals(0, $crawler->filter('html:contains("host: localhost")')->count());
        $this->assertGreaterThan(0, $crawler->filter('html:contains("host: en.example.com")')->count());
    }
    public function testIndexFromRequest()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/demo/', array(), array(), array(
            'HTTP_HOST'       => 'en.example.com',
            'HTTP_USER_AGENT' => 'MySuperBrowser/1.0',
        ));

        $this->assertEquals(0, $crawler->filter('html:contains("host: localhost")')->count());
        $this->assertGreaterThan(0, $crawler->filter('html:contains("host: en.example.com")')->count());
    }
    public function testIndexFromSetParameter()
    {
        $client = static::createClient();

        $client->setServerParameters( array(
            'HTTP_HOST'       => 'en.example.com',
            'HTTP_USER_AGENT' => 'MySuperBrowser/1.0',
        ) );
        $crawler = $client->request('GET', '/demo/' );

        $this->assertEquals(0, $crawler->filter('html:contains("host: localhost")')->count());
        $this->assertGreaterThan(0, $crawler->filter('html:contains("host: en.example.com")')->count());
    }
}

All those tests should have the same result. Except that passing the parameters to request() fails. One alternative is to use setServerParameters() instead.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions