Functional tests with authenticated users

When I first started off using Symfony I was excited because of the great built in support for automated testing. I was particularly drawn to the project because it included some support for functional tests. After writing a few of my own tests and reading the docs on the Symfony site, I was disappointed with the support for running functional tests as actual users; the docs all suggest using http authentication, which doesn't test my security and other features correctly. So for quite a while my authenticated pages remained with zero automated test coverage. That was until I created this ClientHelper class.
Using this class, I can create a new user as part of the test and navigate to authenticated pages and perform tests. I can also log in as an 'admin' user (presuming it is pre configured) and perform tests on admin pages.

Here's how it is used for testing admin pages:

use xxx\xxx\Tests\TestHelpers\ClientHelper;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class EventControllerTest extends WebTestCase
{
public function testEventShow()
{
    $clientHelper = new ClientHelper();
    $client       = $clientHelper->getAdminClient();
    $entity       = $this->getOneEvent($client);
    $crawler      = $client->request('GET', '/admin/event/' . $entity->getId()); //this page is secured for only authenticated users with the 'admin' role
    $this->assertTrue(200 === $client->getResponse()->getStatusCode(), "expected status code of 200, got " . $client->getResponse()->getStatusCode() . $client->getResponse()->getContent());
    $this->assertTrue($crawler->filter('html:contains("' . $entity->getTitle() . '")')->count() > 0, "The text '" . $entity->getTitle() . "' was not found" . $client->getResponse()->getContent());

}
}

Here's how it is used for testing authenticated pages with generated user accounts:

 public function  testAuctionNotificationToonExclusionCreate()
{
    $clientHelper = new ClientHelper();
    $clientHelper->createUser();
    $client     = $clientHelper->getClient();
    $toIgnore = array('Carteron', 'Sphinkx', 'Wrenchdk');
    $crawler = $client->request('GET', "/auctionnotificationignoretoon/"); //this page is secured for only authenticated users
    foreach ($toIgnore as $toonName)
        $this->assertTrue($crawler->filter('html:contains("' . $toonName . '")')->count() > 0, '"' . $toonName . '" was not found on the page');
}

And the big reveal: Some configuration may be required for your project (assertions of text, user entity namespace, etc)

namespace ChrisJohnson00\OldStoneBundle\Helpers\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ClientHelper extends WebTestCase
{
private $client;
private $user;

function __construct()
{
    $client = static::createClient();
    $client->followRedirects();

    $crawler = $client->request('GET', '/');
    $this->assertTrue(200 === $client->getResponse()->getStatusCode(), "expected status code of 200, got " . $client->getResponse()->getStatusCode() . $client->getResponse()->getContent());

    $this->client = $client;
}

public function getClient()
{
    return $this->client;
}

public function getUser()
{
    return $this->user;
}

public function getAdminClient()
{
    $client  = $this->getClient();
    $crawler = $client->request('GET', '/login');
    $this->assertTrue(200 === $client->getResponse()->getStatusCode(), "expected status code of 200, got " . $client->getResponse()->getStatusCode() . $client->getResponse()->getContent());


    // Fill in the form and submit it
    $form = $crawler->selectButton('Login')->form(array(
        '_username' => 'admin',
        '_password' => 'admin'
    ));

    $client->submit($form);
    $this->assertTrue(200 === $client->getResponse()->getStatusCode(), "expected status code of 200, got " . $client->getResponse()->getStatusCode() . $client->getResponse()->getContent());
    $this->assertTrue($crawler->filter('html:contains("Old Stone Preschool")')->count() > 0, "The text 'Old Stone Preschool' was not found");
    $this->client = $client;
    return $this->getClient();
}

public function createUser()
{
    $client = static::createClient();

    $crawler = $client->request('GET', '/register');
    $crawler = $client->followRedirect();
    $this->assertTrue(200 === $client->getResponse()->getStatusCode(), "expected status code of 200, got " . $client->getResponse()->getStatusCode());

    $username = 'automation_' . time();

    // Fill in the form and submit it
    $form = $crawler->selectButton('Register')->form(array(
        'fos_user_registration_form[username]'              => $username,
        'fos_user_registration_form[email]'                 => $username . '@wowguild.co',
        'fos_user_registration_form[plainPassword][first]'  => 'password',
        'fos_user_registration_form[plainPassword][second]' => 'password'
    ));

    $client->submit($form);
    $crawler = $client->followRedirect();
    $this->assertTrue(200 === $client->getResponse()->getStatusCode(), "expected status code of 200, got " . $client->getResponse()->getStatusCode());
    $this->assertTrue($crawler->filter('html:contains("your account is now activated")')->count() > 0, "The text 'your account is now activated' was not found");

    $container = $client->getContainer();
    $doctrine  = $container->get('doctrine');
    $em        = $doctrine->getManager();
    $user      = $em->getRepository('FTWGuildBundle:User')->findOneByUsername($username);
    $this->assertInstanceOf('FTW\GuildBundle\Entity\User', $user);
    $this->user   = $user;
    $this->client = $client;
}
}