Enter SfCcTesting: Symfony 1.X Client-Crawler Testing

Earlier today I introduced why and how we initially integrated the Symfony2 testing mechanism into symfony 1.X : in order to make this piece of software as clean and evolvable as possible I just isolated it and made a small repo on Github.

SfCcTesting

SfCcTestingSymfony 1.X Client-Crawler Testing – is a small library (2 classes and a configuration file) which lets you write functional tests, in symfony 1.X, a là Symfony2.

if you take a look at the repository’s README you will find the basic instructions to start writing your tests: keep in mind that – although it’s usable on old-school symfony projects (svn etc) – you should install it with Git and manage dependencies and updates with Composer.

A PHPUnit functional test for symfony 1.X written thanks to SfCcTesting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

use odino\SfCcTesting\WebTestCase;

class HomepageTest extends WebTestCase
{
  public function testHelloWorld()
  {
    $client = $this->createClient();
    $crawler = $client->get('/');

    $this->assertEquals("Hello world", $crawler->filter('h1')->text());
  }

  protected function getApplication()
  {
    return 'frontend';
  }

  protected function bootstrapSymfony($app)
  {
    include(dirname(__FILE__).'/../../test/bootstrap/functional.php');
  }
}

Each test needs to implement 2 protected methods, getApplication() and bootstrapSymfony().

The getApplication method defines the application to bootstrap symfony for, and should be defined in an ApplicationWebTestCase class, like in the following example:

A base class for testing the backend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

use odino\SfCcTesting\WebTestCase;

class BackendWebTestCase extends WebTestCase
{
  protected function getApplication()
  {
    return 'backend';
  }

  protected function bootstrapSymfony($app)
  {
    include(dirname(__FILE__).'/../../test/bootstrap/functional.php');
  }
}

so your test files become leaner:

1
2
3
4
5
6
7
8
9
10
11
12
<?php

class HomepageTest extends BackendWebTestCase
{
  public function testHelloWorld()
  {
    $client = $this->createClient();
    $crawler = $client->get('/');

    $this->assertEquals("Hello world", $crawler->filter('h1')->text());
  }
}

The bootstrapSymfony() method, instead, includes the bootstrap for the symfony application in the test environment; you are allowed to redefine the location of the bootstrap in order not to force you to follow a unique directory structure convention.

The bootstrapSymfony() method should be placed in a BaseWebTestCase of our test suite:

A base class for boostrapping the symfony testing environment
1
2
3
4
5
6
7
8
9
10
11
<?php

use odino\SfCcTesting\WebTestCase;

class BaseWebTestCase extends WebTestCase
{
  protected function bootstrapSymfony($app)
  {
    include(dirname(__FILE__).'/../../test/bootstrap/functional.php');
  }
}

so your different ApplicationWebTestCase can share the same bootstrap file:

1
2
3
4
5
6
7
8
9
10
11
<?php

use odino\SfCcTesting\WebTestCase;

class BackendWebTestCase extends BaseWebTestCase
{
  protected function getApplication()
  {
    return 'backend';
  }
}
1
2
3
4
5
6
7
8
9
10
11
<?php

use odino\SfCcTesting\WebTestCase;

class FrontendWebTestCase extends BaseWebTestCase
{
  protected function getApplication()
  {
    return 'frontend';
  }
}

That’s all folks: with just a bunch of lines of code you are able to functionally test a symfony 1.X application with the Symfony2 DomCrawler.

Feel free to rant and, if you want, you can already rely on this small library via Packagist.


Hi there! I recently wrote an ebook on web application security, currently sold on leanpub, the Amazon Kindle store and gumroad.

It contains 160+ pages of content dedicated to securing web applications and improving your security awareness when building web apps, with chapters ranging from explaining how to secure HTTP cookies with the right flags to understanding why it is important to consider joining a bug bounty program.

Feel free to skim through some of the free chapters published on this blog and, if the content seems interesting enough to you, grab a copy on leanpub, the Amazon Kindle store, gumroad or simply checkout right down below!

Buy the Web Application Security ebook for $9.99

In the mood for some more reading?

...or check the archives.