Reputation: 383
I am writing functional tests for my Symfony 3.2 application, and I thought it would be a good idea to test a few links to external websites. However, when my WebTestCase client clicks on an external link, it returns the original page instead of following the link.
I have a page that looks like this:
// views/default/link_test.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<p><a href="{{ path('homepage') }}">Here</a> is a test link.</p>
<p><a href="http://ayso1447.org">There</a> is an external link.</p>
{% endblock %}
My test looks like this and is accessed by the path /link_test.
namespace Tests\AppBundle;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class FollowLinkFunctionalTest extends WebTestCase
{
public function testFollowInternalLink()
{
$client = static::createClient();
$crawler = $client->request('GET', '/link_test');
$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
$link = $crawler->selectLink('Here')->link();
$page = $client->click($link);
$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
$this->assertContains('AYSO United New Mexico', $page->filter('h1')->eq(0)->text());
$this->assertContains('AYSO United', $page->filter('h1')->eq(1)->text());
$this->assertContains('Club News', $page->filter('h1')->eq(2)->text());
$this->assertContains('External Resources', $page->filter('h1')->eq(3)->text());
}
public function testFollowExternalLink()
{
$client = static::createClient();
$client->followRedirects(true);
$crawler = $client->request('GET', '/link_test');
$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
$link = $crawler->selectLink('There')->link();
$page = $client->click($link);
echo $page->text();
$this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
$this->assertContains('Westside', $page->filter('h1')->eq(0)->text());
}
}
testFollowInternalLink passes, but testFollowExternalLink fails. the echo $page->text()
shows the contents of link_test rather than the contents of the linked page.
Am I missing something? Am I not supposed to be able to follow external links in a functional test?
Thanks!
Upvotes: 2
Views: 706
Reputation: 383
I just ran across this answer from @Cyprian.
It's not possible, because $client actually doesn't send any http request (you may notice that when you try run your "functional" test with www server disabled - they still should work). Instead of that it simulates http request and run normal Symfony's dispatching.
So, that answered my question.
Upvotes: 4