Download - Unit and Functional Testing with Symfony2
![Page 1: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/1.jpg)
Unit & Functional Tests Fabien Potencier
![Page 2: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/2.jpg)
Standardization
![Page 3: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/3.jpg)
PHPUnit 3.5
![Page 4: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/4.jpg)
Best practices
![Page 5: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/5.jpg)
AllTests.php
![Page 6: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/6.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/7.jpg)
<testsuites> <testsuite name="Project Test Suite"> <directory> ../src/Application/*/Tests </directory> </testsuite> </testsuites>
![Page 8: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/8.jpg)
<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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/9.jpg)
Application/ HelloBundle/ Model/ Article.php Tests/ Model/ ArticleTest.php
Application/Tests/Model/ArticleTest.php
![Page 10: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/10.jpg)
$ phpunit –c hello/
![Page 11: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/11.jpg)
$ cd hello/ $ phpunit
![Page 12: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/12.jpg)
$ phpunit -c hello/ src/Application/HelloBundle/
$ phpunit -c hello/ src/Application/HelloBundle/Tests/Controller/HelloControllerTest.php
![Page 13: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/13.jpg)
$ cd hello/ $ phpunit --coverage-html=cov/
![Page 14: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/14.jpg)
Standard artifacts
--coverage-clover=clover.xml
--log-junit=junit.xml
![Page 15: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/15.jpg)
Functional Tests
![Page 16: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/16.jpg)
Do not write Unit Tests for a Controller
![Page 17: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/17.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/18.jpg)
$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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/19.jpg)
$this->createClient('test', true);
Environment Debug mode
![Page 20: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/20.jpg)
# 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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/21.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/22.jpg)
Assertions
![Page 23: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/23.jpg)
$this->assertEquals( 10, $crawler->filter('div.hentry')->count());
$this->assertTrue( $client->getResponse()->isSuccessful());
![Page 24: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/24.jpg)
The Client / The Crawler
![Page 25: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/25.jpg)
$crawler = $client->request( 'GET', 'hello/Lucas' );
![Page 26: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/26.jpg)
$link = $crawler->selectLink("Greet Lucas");
$client->click($link);
![Page 27: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/27.jpg)
$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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/28.jpg)
$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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/29.jpg)
$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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/30.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/31.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/32.jpg)
Simulate or use HTTP
![Page 33: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/33.jpg)
$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](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/34.jpg)
Questions?
![Page 35: Unit and Functional Testing with Symfony2](https://reader033.vdocument.in/reader033/viewer/2022052321/55580febd8b42a5e468b47d1/html5/thumbnails/35.jpg)
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/