Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Unit & Functional Tests
Fabien Potencier
Standardization
PHPUnit 3.5
Best practices
AllTests.php
phpunit.xml(.dist)

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="false"
       ...
<testsuites>
  <testsuite name="Project Test Suite">
    <directory>
      ../src/Application/*/Tests
    </directory>
  <...
<filter>
  <whitelist>
    <directory>../src/Application</directory>
    <exclude>
      <directory>
         ../src/Appli...
Application/
  HelloBundle/
    Model/
      Article.php
    Tests/
      Model/
        ArticleTest.php


    Application...
$ phpunit –c hello/
$ cd hello/
$ phpunit
$ phpunit -c hello/ src/Application/HelloBundle/

$ phpunit -c hello/ src/Application/HelloBundle/
Tests/Controller/HelloC...
$ cd hello/
$ phpunit --coverage-html=cov/
Standard artifacts


--coverage-clover=clover.xml

--log-junit=junit.xml
Functional Tests
Do not write Unit Tests for a Controller
namespace ApplicationHelloBundleTestsController;

use SymfonyFrameworkWebBundleTestWebTestCase;

class HelloControllerTest...
$client = $this->createClient();

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

$this->assertTrue($crawler->fil...
Environment   Debug mode
$this->createClient('test', true);
# hello/config/config_test.yml
imports:
    - { resource: config_dev.yml }

web.config:
    toolbar: false

zend.logger:
 ...
The Client makes requests to the Symfony2 application



 The Crawler parses the Response to allow navigation



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

$this->assertTrue(
  $client->getResponse()->isSuc...
The Client / The Crawler
$crawler = $client->request(
   'GET', 'hello/Lucas'
);
$link = $crawler->selectLink("Greet Lucas");

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

$client->submit($form, array(
    'name'         => 'Lucas',
    'country'     ...
$harry = $this->createClient();
$sally = $this->createClient();

$harry->request('POST', '/say/sally/Hello');
$sally->requ...
$harry = $this->createClient();
$sally = $this->createClient();

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

$harry->request(...
Main PHP Process

1                                            4
$harry = $this->createClient();                $this->ass...
Main PHP Process

1                                        3
$harry = $this->createClient();              $sally->request(...
Simulate or use HTTP
$response = $client->getResponse();
$profiler = $this->getProfiler($response);

if ($profiler) {
  $this->assertEquals(2,
...
Questions?
Sensio S.A.
   92-98, boulevard Victor Hugo
       92 115 Clichy Cedex
             FRANCE
      Tél. : +33 1 40 99 80 80
...
Nächste SlideShare
Wird geladen in …5
×

Unit and Functional Testing with Symfony2

38.423 Aufrufe

Veröffentlicht am

Veröffentlicht in: Technologie

Unit and Functional Testing with Symfony2

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

×