Symfony book 2 1

Page 124

Functional Tests Functional tests check the integration of the different layers of an application (from the routing to the views). They are no different from unit tests as far as PHPUnit is concerned, but they have a very specific workflow: • • • • •

Make a request; Test the response; Click on a link or submit a form; Test the response; Rinse and repeat.

Your First Functional Test Functional tests are simple PHP files that typically live in the Tests/Controller directory of your bundle. If you want to test the pages handled by your DemoController class, start by creating a new DemoControllerTest.php file that extends a special WebTestCase class. For example, the Symfony2 Standard Edition provides a simple functional test for its DemoController (DemoControllerTest2) that reads as follows: Listing 10-5

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

// src/Acme/DemoBundle/Tests/Controller/DemoControllerTest.php namespace Acme\DemoBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class DemoControllerTest extends WebTestCase { public function testIndex() { $client = static::createClient(); $crawler = $client->request('GET', '/demo/hello/Fabien'); $this->assertGreaterThan( 0, $crawler->filter('html:contains("Hello Fabien")')->count() ); } }

To run your functional tests, the WebTestCase class bootstraps the kernel of your application. In most cases, this happens automatically. However, if your kernel is in a non-standard directory, you'll need to modify your phpunit.xml.dist file to set the KERNEL_DIR environment variable to the directory of your kernel: Listing 10-6

1 <phpunit> 2 <!-- ... --> 3 <php> 4 <server name="KERNEL_DIR" value="/path/to/your/app/" /> 5 </php> 6 <!-- ... --> 7 </phpunit>

2. https://github.com/symfony/symfony-standard/blob/master/src/Acme/DemoBundle/Tests/Controller/DemoControllerTest.php

PDF brought to you by generated on February 4, 2013

Chapter 10: Testing | 124


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.