Search code examples
urlsymfony1symfony-1.4functional-testing

Functional testing absolute links


When I run functional tests in Symfony, my absolute links don't appear as they would even in the "real" test environment. For example, a link I output like this:

echo url_for("@homepage", true);

it will show up as http://localhost/index.php/, whereas if I actually visit the test site in my browser, it'll show up as http://example.com.localhost/frontend_test.php/

Now, I'm assuming this is happening because the test environment doesn't know anything about my local virtual server setup, etc., so it has no way of knowing what its "real" absolute URL should be.

But this makes my tests more difficult, and less "real". Making my tests pass by matching some odd link that wouldn't actually work if I were to click on it feels a bit wrong.

Maybe the best result would be if my test browser's absolute links looked the same as my real environment's absolute links, so I can easily test the results against the values that the real system would output, e.g. an absolute link to the homepage would look like it did in the live system:

http://example.com/

instead of

http://localhost/index.php

How do people normally deal with this? Can I "tell" the test browser where its absolute URLs should be based, in terms of hostname and controller, including dropping "index.php", and so get more realistic URLs out of the test framework?

(These URLs are being generated for an Atom feed and in emails, which is why I need them to be absolute. Relative links are working fine.)


Solution

  • In my opinion your website should work with ANY domain. Testing for host name ties tests to it.

    I think what you need is to check if the link is absolute or not (easily done with regexp).

    I can hardly find a need for testing specific domain (unless you're using multiple domains; in such case domains should be configurable anyways).

    EDIT:

    You can actually change the host name with first parameter passed to the browser's constructor:

    $test = new sfTestFunctional(new sfBrowser('mydomain.com'));
    

    I never tried it.