📜 ⬆️ ⬇️

Development by testing with Zend Framework and PHPUnit

After spending the past few days studying the Zend Framework documentation, I was pleasantly surprised by the new functionality that was added to the latest version of this Web application framework.
My first thought was the realization of the speed with which the PHP technology matures.
The ease of sharing the Zend Framework and PHPUnit is, in my opinion, one of the most significant achievements.


I've already done enough with PHPUnit to understand how powerful, customizable and easy it is to
using the tool (it belongs to the xUnit family of testing frameworks ), but the most remarkable thing is that Zend Framework is fully ready to work with PHPUnit (in my opinion such readiness will allow to choose Zend Framework for any PHP project, but this is a topic for another post. ..).

Here's what we know about PHPUnit: You can not only unit-test your PHP classes, but also create dependencies between tests, use dataProviders to increase the sample size of test input parameters, test exceptions and errors .
')
In addition, PHPUnit allows you to do the following: organize tests , provide database testing , perform double testing to cover source code, and, moreover, can be used for quick documentation .
In conclusion, PHPUnit works very well in conjunction with Selenium, Apache Ant, Apache Maven, Phing, Atlassian Bamboo, CruiseControl, and other environments.

I can only refer to the documentation for PHPUnit and you can return to the main topic of the article.

According to the structure of the Zend Framework project directories, it is clearly seen that the authors of the Zend Framework have always paid great attention to the “development through testing” methodology. The official documentation, reference materials, literature emphasize the place of the test catalog in the overall project structure. You can talk about the "Zend way of PHP development" ( "Zend way of PHP development" ).
For a more detailed study, in the Zend_Test package, we find the Zend_Test_PHPUnit and Zend_Test_PHPUnit_Db classes that are inherited from PHPUnit.

Zend_Test_PHPUnit provides classes that can be used to test the framework’s MVC classes.
In the most typical case, the class for testing the controller will be the successor of Zend_Test_PHPUnit_ControllerTestCase.
Below is an example class for testing 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 .

Zend_Test_PHPUnit_Db inherits the PHPUnit database extension with the Zend Framework specific code, which allows you to significantly
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> * . <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 .

When your application is almost finished, functional testing will be appropriate.
Together with the Zend Framework with PHPUnit support, Selenium IDE can be used.
There are tests that mimic the behavior of the end user and are designed to test usability, security and performance.

The usual test Selenium 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 does not touch upon a small fraction of all testing capabilities.
The purpose of this article is to show that the Zend framework is very closely integrated into its environment and that
This integration contributes to a group of PHP users to practice using technology.
"Development through testing" (TDD).
Use these features to improve the quality of your products.

Source: https://habr.com/ru/post/87202/


All Articles