Unit and Functional Testing with Symfony2

35
Unit & Functional Tests Fabien Potencier

Transcript of Unit and Functional Testing with Symfony2

Page 1: Unit and Functional Testing with Symfony2

Unit & Functional Tests Fabien Potencier

Page 2: Unit and Functional Testing with Symfony2

Standardization

Page 3: Unit and Functional Testing with Symfony2

PHPUnit 3.5

Page 4: Unit and Functional Testing with Symfony2

Best practices

Page 5: Unit and Functional Testing with Symfony2

AllTests.php

Page 6: Unit and Functional Testing with Symfony2

phpunit.xml(.dist)

<phpunit backupGlobals="false" backupStaticAttributes="false" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="true" stopOnFailure="false" syntaxCheck="false" bootstrap="../src/autoload.php" >

Page 7: Unit and Functional Testing with Symfony2

<testsuites> <testsuite name="Project Test Suite"> <directory> ../src/Application/*/Tests </directory> </testsuite> </testsuites>

Page 8: Unit and Functional Testing with Symfony2

<filter> <whitelist> <directory>../src/Application</directory> <exclude> <directory> ../src/Application/*/Resources </directory> <directory> ../src/Application/*/Tests </directory> </exclude> </whitelist> </filter>

Page 9: Unit and Functional Testing with Symfony2

Application/ HelloBundle/ Model/ Article.php Tests/ Model/ ArticleTest.php

Application/Tests/Model/ArticleTest.php

Page 10: Unit and Functional Testing with Symfony2

$ phpunit –c hello/

Page 11: Unit and Functional Testing with Symfony2

$ cd hello/ $ phpunit

Page 12: Unit and Functional Testing with Symfony2

$ phpunit -c hello/ src/Application/HelloBundle/

$ phpunit -c hello/ src/Application/HelloBundle/Tests/Controller/HelloControllerTest.php

Page 13: Unit and Functional Testing with Symfony2

$ cd hello/ $ phpunit --coverage-html=cov/

Page 14: Unit and Functional Testing with Symfony2

Standard artifacts

--coverage-clover=clover.xml

--log-junit=junit.xml

Page 15: Unit and Functional Testing with Symfony2

Functional Tests

Page 16: Unit and Functional Testing with Symfony2

Do not write Unit Tests for a Controller

Page 17: Unit and Functional Testing with Symfony2

namespace Application\HelloBundle\Tests\Controller;

use Symfony\Framework\WebBundle\Test\WebTestCase;

class HelloControllerTest extends WebTestCase { public function testIndex() { $client = $this->createClient(); $crawler = $client->request( 'GET', '/hello/Fabien');

$this->assertTrue($crawler->filter( 'html:contains("Hello Fabien")')->count()); } }

Page 18: Unit and Functional Testing with Symfony2

$client = $this->createClient();

$crawler = $client->request( 'GET', '/hello/Fabien');

$this->assertTrue($crawler->filter( 'html:contains("Hello Fabien")')->count());

Page 19: Unit and Functional Testing with Symfony2

$this->createClient('test', true);

Environment Debug mode

Page 20: Unit and Functional Testing with Symfony2

# hello/config/config_test.yml imports: - { resource: config_dev.yml }

web.config: toolbar: false

zend.logger: priority: debug

kernel.test: ~

Page 21: Unit and Functional Testing with Symfony2

The Client makes requests to the Symfony2 application

The Crawler parses the Response to allow navigation

The PHPUnit Assertions tests the Response

Page 22: Unit and Functional Testing with Symfony2

Assertions

Page 23: Unit and Functional Testing with Symfony2

$this->assertEquals( 10, $crawler->filter('div.hentry')->count());

$this->assertTrue( $client->getResponse()->isSuccessful());

Page 24: Unit and Functional Testing with Symfony2

The Client / The Crawler

Page 25: Unit and Functional Testing with Symfony2

$crawler = $client->request( 'GET', 'hello/Lucas' );

Page 26: Unit and Functional Testing with Symfony2

$link = $crawler->selectLink("Greet Lucas");

$client->click($link);

Page 27: Unit and Functional Testing with Symfony2

$form = $crawler->selectButton('submit');

$client->submit($form, array( 'name' => 'Lucas', 'country' => 'France', 'like_symfony' => true, 'photo' => '/path/to/lucas.jpg', ));

Page 28: Unit and Functional Testing with Symfony2

$harry = $this->createClient(); $sally = $this->createClient();

$harry->request('POST', '/say/sally/Hello'); $sally->request('GET', '/messages');

$this->assertEquals(201, $harry->getResponse()->getStatusCode());

$this->assertRegExp('/Hello/', $sally->getResponse()->getContent());

Page 29: Unit and Functional Testing with Symfony2

$harry = $this->createClient(); $sally = $this->createClient();

$harry->insulate(); $sally->insulate();

$harry->request('POST', '/say/sally/Hello'); $sally->request('GET', '/messages');

$this->assertEquals(201, $harry->getResponse()->getStatusCode()); $this->assertRegExp('/Hello/', $sally->getResponse()->getContent());

Page 30: Unit and Functional Testing with Symfony2

Main PHP Process

Forked PHP Process $harry->request('POST', '/say/sally/Hello');

Forked PHP Process $sally-­‐>request('GET',  '/messages');

$harry = $this->createClient(); $sally = $this->createClient();

$harry->insulate(); $sally->insulate();

$this->assertEquals(201, $harry->getResponse()->getStatusCode()); $this-­‐>assertRegExp('/Hello/',      $sally-­‐>getResponse()-­‐>getContent());  

1

2

3

4

Page 31: Unit and Functional Testing with Symfony2

Forked PHP Process $harry->request('POST', '/say/sally/Hello');

Main PHP Process

$harry = $this->createClient(); $sally = $this->createClient();

$harry->insulate();

$sally->request('GET', '/messages');

$this->assertEquals(201, $harry->getResponse()->getStatusCode()); $this-­‐>assertRegExp('/Hello/',      $sally-­‐>getResponse()-­‐>getContent());  

1

2

3

Page 32: Unit and Functional Testing with Symfony2

Simulate or use HTTP

Page 33: Unit and Functional Testing with Symfony2

$response = $client->getResponse(); $profiler = $this->getProfiler($response);

if ($profiler) { $this->assertEquals(2, $profiler['db']->getQueryCount());

$this->assertEquals('blog_post', $profiler['app']->getRoute());

$this->assertTrue( $profiler['timer']->getTime() < 0.5); }

Page 34: Unit and Functional Testing with Symfony2

Questions?

Page 35: Unit and Functional Testing with Symfony2

Sensio S.A. 92-98, boulevard Victor Hugo

92 115 Clichy Cedex FRANCE

Tél. : +33 1 40 99 80 80

Contact Fabien Potencier

fabien.potencier at sensio.com

http://www.sensiolabs.com/

http://www.symfony-project.org/

http://fabien.potencier.org/