Unit and Functional Testing with Symfony2
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Unit and Functional Testing with Symfony2

on

  • 30,387 views

 

Statistics

Views

Total Views
30,387
Views on SlideShare
24,235
Embed Views
6,152

Actions

Likes
35
Downloads
352
Comments
4

34 Embeds 6,152

http://query7.com 2303
http://jerome.tamarelle.net 1172
http://romain.cambien.net 498
http://www.symfony.es 489
http://www.symfony-project.org 416
http://www.symfonylab.com 402
http://symfony.com 335
http://swik.net 116
http://www.symfony.gr.jp 92
http://www.sfexception.com 84
http://coderwall.com 69
http://www.phpquote.net 45
http://charles-guillemot.info 29
http://rcambien.tumblr.com 17
http://translate.googleusercontent.com 15
http://www.pearltrees.com 14
http://webcache.googleusercontent.com 8
http://rimzy.net 6
http://feeds.feedburner.com 5
http://www.slideshare.net 5
http://www.phpframeworks.com 5
http://static.slidesharecdn.com 5
http://jerem.blogduwebdesign.com 4
http://www.linkedin.com 3
http://webdesign.feedfury.com 2
http://symfony.lab216.com 2
http://paper.li 2
http://98.139.168.220 2
http://www.prestataire-symfony.com 2
http://a0.twimg.com 1
resource://brief-content 1
https://si0.twimg.com 1
http://phpquote.net 1
http://www.alertize.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Unit and Functional Testing with Symfony2 Presentation Transcript

  • 1. Unit & Functional Tests Fabien Potencier
  • 2. Standardization
  • 3. PHPUnit 3.5
  • 4. Best practices
  • 5. AllTests.php
  • 6. 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" >
  • 7. <testsuites> <testsuite name="Project Test Suite"> <directory> ../src/Application/*/Tests </directory> </testsuite> </testsuites>
  • 8. <filter> <whitelist> <directory>../src/Application</directory> <exclude> <directory> ../src/Application/*/Resources </directory> <directory> ../src/Application/*/Tests </directory> </exclude> </whitelist> </filter>
  • 9. Application/ HelloBundle/ Model/ Article.php Tests/ Model/ ArticleTest.php Application/Tests/Model/ArticleTest.php
  • 10. $ phpunit –c hello/
  • 11. $ cd hello/ $ phpunit
  • 12. $ phpunit -c hello/ src/Application/HelloBundle/ $ phpunit -c hello/ src/Application/HelloBundle/ Tests/Controller/HelloControllerTest.php
  • 13. $ cd hello/ $ phpunit --coverage-html=cov/
  • 14. Standard artifacts --coverage-clover=clover.xml --log-junit=junit.xml
  • 15. Functional Tests
  • 16. Do not write Unit Tests for a Controller
  • 17. namespace ApplicationHelloBundleTestsController; use SymfonyFrameworkWebBundleTestWebTestCase; 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()); } }
  • 18. $client = $this->createClient(); $crawler = $client->request( 'GET', '/hello/Fabien'); $this->assertTrue($crawler->filter( 'html:contains("Hello Fabien")')->count());
  • 19. Environment Debug mode $this->createClient('test', true);
  • 20. # hello/config/config_test.yml imports: - { resource: config_dev.yml } web.config: toolbar: false zend.logger: priority: debug kernel.test: ~
  • 21. The Client makes requests to the Symfony2 application The Crawler parses the Response to allow navigation The PHPUnit Assertions tests the Response
  • 22. Assertions
  • 23. $this->assertEquals( 10, $crawler->filter('div.hentry')->count()); $this->assertTrue( $client->getResponse()->isSuccessful());
  • 24. The Client / The Crawler
  • 25. $crawler = $client->request( 'GET', 'hello/Lucas' );
  • 26. $link = $crawler->selectLink("Greet Lucas"); $client->click($link);
  • 27. $form = $crawler->selectButton('submit'); $client->submit($form, array( 'name' => 'Lucas', 'country' => 'France', 'like_symfony' => true, 'photo' => '/path/to/lucas.jpg', ));
  • 28. $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());
  • 29. $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());
  • 30. Main PHP Process 1 4 $harry = $this->createClient(); $this->assertEquals(201, $sally = $this->createClient(); $harry->getResponse()->getStatusCode()); $this-­‐>assertRegExp('/Hello/',   $harry->insulate();    $sally-­‐>getResponse()-­‐>getContent());   $sally->insulate(); 2 Forked PHP Process $harry->request('POST', '/say/sally/Hello'); 3 Forked PHP Process $sally-­‐>request('GET',  '/messages');
  • 31. Main PHP Process 1 3 $harry = $this->createClient(); $sally->request('GET', '/messages'); $sally = $this->createClient(); $this->assertEquals(201, $harry->insulate(); $harry->getResponse()->getStatusCode()); $this-­‐>assertRegExp('/Hello/',      $sally-­‐>getResponse()-­‐>getContent());   2 Forked PHP Process $harry->request('POST', '/say/sally/Hello');
  • 32. Simulate or use HTTP
  • 33. $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); }
  • 34. Questions?
  • 35. 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/