Test-driven development with Zend Framework and PHPUnit
after Spending the last few days studying the documentation for the Zend Framework, I was pleasantly surprised by a new functionality that was added in the latest version of this framework in a Web application.
My first thought was the realization of the speed with which the technology Matures in PHP.
The ease of sharing the Zend Framework and PHPUnit is, in my opinion, one of the most significant achievements.
I've worked with PHPUnit to understand how this powerful, customizable and easy to
use tool (it belongs to the the family of xUnit test framework s), but the most noteworthy is that the Zend Framework is ready to work with PHPUnit (I think this commitment will allow you to choose Zend Framework for any PHP project, but that's a topic for another post...).
Here's what we know about PHPUnit: You can not only module to test your PHP classes, but create dependencies between tests, use dataProvider's to increase the sample size of the input test parameters, test exceptions and errors.
In addition, PHPUnit allows you to do the following: organize tests, to provide a testing database, double-testing to source code coverage, and, moreover, can be used to quick documentation.
In conclusion, I note that PHPUnit works very well in conjunction with Selenium, Apache Ant, Apache Maven, Phing, Atlassian Bamboo, CruiseControl and other environments.
It only remains for me to refer to PHPUnit documentation and you can return to the main topic of the article.
The project directory structure Zend Framework clearly shows that the authors of the Zend Framework always paid great attention to the methodology of "development through testing." In the official documentation, reference materials, literature emphasizes the location of the directory for the tests in the overall structure of the project. You can talk about "Zend way PHP development" ("Zend way of PHP development").
In a more detailed study, in the package Zend_Test we find Zend_Test_PHPUnit and Zend_Test_PHPUnit_Db classes that inherit from PHPUnit.
the Zend_Test_PHPUnit provides classes that can be used to test the MVC framework classes.
In the most typical case, the class to test the controller will be the successor of Zend_Test_PHPUnit_ControllerTestcase.
Below is an example class to test the controller.
to simplify writing tests for the database.
The following is a test:
Once Your application is almost finished, it would be appropriate for functional testing.
In conjunction with the Zend Framework with PHPUnit support can be used in Selenium IDE.
There are tests that simulate end user behavior and are designed to test usability, security and performance.
Normal Selenium test inherits PHPUnit_Extensions_SeleniumTestCase and looks like this:
This article is not affected and a small fraction of all the possibilities of testing.
The purpose of this article is to show that the Zend framework is closely integrated into its environment and that
this integration contributes to the user group PHP in practice, use of technology
"test-driven development" (TDD).
Use these opportunities to improve the quality of their products.
Article based on information from habrahabr.ru
My first thought was the realization of the speed with which the technology Matures in PHP.
The ease of sharing the Zend Framework and PHPUnit is, in my opinion, one of the most significant achievements.
I've worked with PHPUnit to understand how this powerful, customizable and easy to
use tool (it belongs to the the family of xUnit test framework s), but the most noteworthy is that the Zend Framework is ready to work with PHPUnit (I think this commitment will allow you to choose Zend Framework for any PHP project, but that's a topic for another post...).
Here's what we know about PHPUnit: You can not only module to test your PHP classes, but create dependencies between tests, use dataProvider's to increase the sample size of the input test parameters, test exceptions and errors.
In addition, PHPUnit allows you to do the following: organize tests, to provide a testing database, double-testing to source code coverage, and, moreover, can be used to quick documentation.
In conclusion, I note that PHPUnit works very well in conjunction with Selenium, Apache Ant, Apache Maven, Phing, Atlassian Bamboo, CruiseControl and other environments.
It only remains for me to refer to PHPUnit documentation and you can return to the main topic of the article.
The project directory structure Zend Framework clearly shows that the authors of the Zend Framework always paid great attention to the methodology of "development through testing." In the official documentation, reference materials, literature emphasizes the location of the directory for the tests in the overall structure of the project. You can talk about "Zend way PHP development" ("Zend way of PHP development").
In a more detailed study, in the package Zend_Test we find Zend_Test_PHPUnit and Zend_Test_PHPUnit_Db classes that inherit from PHPUnit.
the Zend_Test_PHPUnit provides classes that can be used to test the MVC framework classes.
In the most typical case, the class to test the controller will be the successor of Zend_Test_PHPUnit_ControllerTestcase.
Below is an example class to test the controller.
class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestcase<br>{<br> public function setUp()<br> {<br> $this->bootstrap = array($this 'appBootstrap');<br> parent::setUp();<br> }<br><br> public function appBootstrap()<br> {<br> $this->frontController<br> ->registerPlugin( new Bugapp_Plugin_Initialize('development'));<br> }<br><br> public function testCallWithoutActionShouldPullFromindexaction()<br> {<br> $this->dispatch('/user');<br> $this->assertController( 'user');<br> $this->assertAction('index');<br> }<br><br> public function testIndexActionShouldContainLoginform()<br> {<br> $this->dispatch( '/user');<br> $this->assertAction('index');<br> $this->assertQueryCount('form#loginForm', 1);<br> }<br><br> public function testValidLoginShouldGoToProfilePage()<br> {<br> $this->request->setMethod('POST')<br> ->setPost(array(<br> 'username' => 'foobar'<br> 'password' => 'foobar'<br> ));<br> $this->dispatch('/user/login');<br> $this->assertRedirectTo('/user/view');<br><br> $this->resetRequest()<br> ->resetResponse();<br><br> $this->request->setMethod('GET')<br> ->setPost(array());<br> $this->dispatch('/user/view');<br> $this->assertRoute('default');<br> $this->assertModule('default');<br> $this->assertController('user');<br> $this->assertAction('view');<br> $this->assertNotRedirect();<br> $this->assertQuery('dl');<br> $this->assertQueryContentContains('h2' 'User: foobar');<br> }<br>}<br><br>* This source code was highlighted with Source Code Highlighter.
to simplify writing tests for the database.
The following is a test:
class BugsTest extends Zend_Test_PHPUnit_DatabaseTestCase<br>{<br> private $_connectionMock;<br><br> /**<br> * Returns the test database connection.<br> *<br> * @return PHPUnit_Extensions_Database_DB_Idatabaseconnection<br> */<br> protected function getConnection()<br> {<br> if($this->_connectionMock == null) {<br> $connection = Zend_Db::factory(...);<br> $this->_connectionMock = $this->createZendDbConnection(<br> $connection 'zfunittests'<br> );<br> Zend_Db_Table_Abstract::setDefaultAdapter($connection);<br> }<br> return $this->_connectionMock;<br> }<br><br> /**<br> * @return PHPUnit_Extensions_Database_Dataset_idataset<br> */<br> protected function getDataSet()<br> {<br> return $this->createFlatXmlDataSet(<br> dirname(__FILE__) . '/_files/bugsSeed.xml'<br> );<br> }<br>}<br><br>* This source code was highlighted with Source Code Highlighter.
Once Your application is almost finished, it would be appropriate for functional testing.
In conjunction with the Zend Framework with PHPUnit support can be used in Selenium IDE.
There are tests that simulate end user behavior and are designed to test usability, security and performance.
Normal Selenium test inherits PHPUnit_Extensions_SeleniumTestCase and looks like this:
<?php<br>require_once 'PHPUnit/Extensions/SeleniumTestCase.php';<br>class seleniumExampleTest extends PHPUnit_Extensions_SeleniumTestCase<br>{<br> protected function setUp()<br> {<br> $this->setBrowser('*firefox');<br> $this->setBrowserUrl('http://www.google.com.au/');<br> }<br> function testMyTestCase()<br> {<br> $this->open('http://www.google.com.au/');<br> $this->type('q' 'zend framework');<br> $this->click('btnG');<br> $this->waitForPageToLoad('30000');<br> try {<br> $this->assertTrue($this->isTextPresent('framework.zend.com/'));<br> } catch (PHPUnit_Framework_AssertionFailederror $e) {<br> array_push($this->verificationErrors, $e->toString());<br> }<br> }<br>}<br><br>* This source code was highlighted with Source Code Highlighter.
This article is not affected and a small fraction of all the possibilities of testing.
The purpose of this article is to show that the Zend framework is closely integrated into its environment and that
this integration contributes to the user group PHP in practice, use of technology
"test-driven development" (TDD).
Use these opportunities to improve the quality of their products.
Комментарии
Отправить комментарий