Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Debugging PHP
Creating the test class
The first step is the creation of AccountTester class as a subclass of TestCase class provided by PhpUnit.
The TestCase class has two methods, setUp and tearDown, that are interesting to us. Both methods have an empty implementation in the TetsCase class and must be overridden in order to be used.
The setUp method is used for initialization stuff. In our case we used to initialize the sample accounts used in our tests.The tearDown method is used for clean up stuff, we don't need it for our tests. Our AccountTester class looks like this:

<?php

class AccountTester extends TestCase{
  
  var
$_ac1;
  var
$_ac2;
  var
$_ac3;
  var
$_ac4;
  
  function
AccountTester($name){
    
$this->TestCase($name); // call parent constructor
  
}

  
  function
setUp(){
    
    
// data  for testWithdraw
    
$this->_ac1 = new Account(100);  
    
    
// data for  testDeposit
    
$this->_ac2 = new Account(20);  
    
    
// data for  testTransferFrom
    
$this->_ac3 = new Account(30);  
    
$this->_ac4 = new Account(50);  
  }
}

?>
Adding Individual Tests
Now we must add our first test methods to AccountTester class.

<?php

// Make a withdrawal of 25 units from _ac1.
// _ac1's initial balance is  100

function testWithdraw(){
    
$this->_ac1->withdraw(25);
    
$this->assert($this->_ac1->getBalance() == 75); // 100 - 25 = 75
}

// Make a deposit of 10 units into _ac2.
// _ac1's initial balance is  20

function testDeposit(){
    
$this->_ac2->deposit(10);
    
$this->assertEquals(30,$this->_ac2->getBalance()); //20 +10 = 30
}
  
//  Tranfers 10 units from _ac3 to _ac4
//  _ac3's initial balance is  30
//  _ac4's initial balance is  50

function testTransferFrom(){
    
$this->_ac4->transferFrom(&$this->_ac3,10);
    
$this->assertEquals(20,$this->_ac3->getBalance(),"Source account balance incorrect"); // 30 - 10 = 20
    
$this->assertEquals(60,$this->_ac4->getBalance(),"Target account balance incorrect"); // 50 + 10 = 60
}

?>
The key of the test is the assert method. If the expression inside the assert is true the test is passed, otherwise an error as occurred.
A lot of asserts check for the equality of two values, for this reason the the TestCase class has a assertEquals method. The assertEqual method has three parameters: the expected value, the actual value, and a optional message to be printed if the test fails.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
Problem with PhpUnitShaji Mathew02/15/02 06:59
"enhanced" phpunitAdrian Kubala07/18/01 11:07
RE: What if I don't know the result?Oier Blasco06/13/01 01:50
What about the Zend de buggerMooza05/25/01 13:04
What if I don't know the result?Ken Egervari04/12/01 06:03
This WILL save you time and heartacheAdam Jeffery04/11/01 12:28
PhpUnit.sourceforge.net now hosts PhpUnit codFred Yankowski04/10/01 10:37
see PhpUnit.sourceforge.netFred Yankowski04/09/01 19:31
download of class TestCaseDaniel Hopp04/09/01 09:42
followed the link but no joyDarren Christie04/09/01 04:02
This type of development is worth the time.Robert Nickens04/07/01 11:04
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.