FOUnitTestBase Class Reference
[FOUnitTest]

Abstract base class for all unit test classes. More...

List of all members.

Public Member Functions

 FOUnitTestBase ()
 Constructor.
 setSectionLabel ($str_=NULL)
 Section label for next doCompare result.
 addSectionText ($str_)
 Section text for the next doCompare result.
 setFailComment ($str_)
 A comment written when the assert fails.
 needEmpty ($code_)
 Use with eval to verify that $code_ will generate an empty variable.
 needNotEmpty ($code_)
 Use with eval to verify that $code will generate a value or some text.
 needTrue ($code_)
 Use with eval to verify that $code will generate true.
 needFalse ($code_)
 Use with eval to verify that $code will generate false.
 needEqual ($code_, $expectedReturn_)
 Used with eval to verify that the return of the php code in $code_ and the value in $expecedReturn_ is equal.
 needDiff ($code_, $expectedReturn_)
 Used with eval to verify that the return of the php code in $code_ and the value in $expecedReturn_ is not equal.
 needEqualReg ($code_, $reqularExp_)
 Used with eval to verify that the return of the php code in $code_ matches the reqular expression in reqularExp_.
 needDate ($code_)
 Used with eval to verify that the return of the php code in $code_ are a date.
 _addTestResult ($type_, $status_, $code_, $value1_, $value2_)
 Should only be used by the NEED functions.
getTestResult ()
 Will return the result of the doTest test.
 isEqual ($value1_, $value2_)
 Compares $value1 with $value2, return true if equal.

Protected Member Functions

 doTest ()
 This function should be overrided in a child class and include the unit tests.

Private Member Functions

 _isEqualArray ($value1_, $value2_)
 Compares $value1 with $value2, return true if equal, the values has be arrays.
 _getFailComment ()
 Returns the fail comment.
 _resetFailComment ()
 Delete all fail fomments.

Private Attributes

 $_currentSectionLabel
 Section label used by the next call to doCompare.
 $_currentSectionText
 Section text used by the next call to doCompare.
 $_testCount
 Number of test actually done.
 $_testResult
 Array including all the test results, added with _addTestResult.
 $_failComment
 A comment written when the assert fails.


Detailed Description

Abstract base class for all unit test classes.

Has test case functions like needEqual, needTrue etc.

 // File: utFOUnitTestBase.php
  class utFOUnitTestBase extends foUnitTestBase
  {
    public function doTest()
    {
      $this->_testNeedEmpty();
    }

    private function _testNeedEmpty()
    {
      $this->setSectionLabel('needEmpty');
      $this->addSectionText('Will check FOUnitTestBase::needEmpty().');

      // Will succeed
      eval($this->needEmpty('""'));

      $nullVar = NULL;
      eval($this->needEmpty('$nullVar'));

      $emptyArray = array();
      eval($this->needEmpty('$emptyArray'));
    }
  }*

Author:
Daniel Lindh <[email protected]>
Requirements: None

Definition at line 39 of file FOUnitTestBase.class.php.


Member Function Documentation

FOUnitTestBase::_addTestResult ( type_,
status_,
code_,
value1_,
value2_ 
)

Should only be used by the NEED functions.

Will add a test result to the internal test array.

Parameters:
$type_ - string - Type of test performed
$status_ - boolean - Ok if the test passed.
$code_ - mixed - The code that generated $value1 throw an eval.
$value1_ - mixed - Value generated from $code
$value2_ - mixed - Value to be compared with value1.
Todo:
support writeHtmlArray output the "ReportFailDetails" part.

Definition at line 544 of file FOUnitTestBase.class.php.

References _getFailComment(), and _resetFailComment().

00545   {
00546     $this->_testCount++;
00547 
00548     // L�gger till i vilken fil och vilken rad som koden k�rdes.
00549     $backTrace = debug_backtrace();
00550     $this->_testResult[$this->_testCount] = array
00551     (
00552       'file' => str_replace(': eval()\'d code', '', $backTrace[0]['file']),
00553 
00554       'class_name'    => strtolower(get_class($this)),
00555       'section_label' => $this->_currentSectionLabel,
00556       'section_text'  => $this->_currentSectionText,
00557       'fail_comment' => $this->_getFailComment(),
00558 
00559       'type'   => $type_,
00560       'status' => $status_,
00561       'code'   => $code_,
00562 
00563       'return'          => $value1_,
00564       'expected_return' => $value2_
00565     );
00566 
00567     $this->_resetFailComment();
00568   }

FOUnitTestBase::_getFailComment (  )  [private]

Returns the fail comment.

Returns:
- string - The comment the will be written in case of test fail.
See also:
addFailComment(), _resetFailComments()

Definition at line 666 of file FOUnitTestBase.class.php.

Referenced by _addTestResult().

00667   {
00668     return $this->_failComment;
00669   }

FOUnitTestBase::_isEqualArray ( value1_,
value2_ 
) [private]

Compares $value1 with $value2, return true if equal, the values has be arrays.

Parameters:
$value1_ - mixed - Value to be compared with value2
$value2_ - mixed - Value to be compared with value1
Returns:
- string - True if values are equal

Definition at line 634 of file FOUnitTestBase.class.php.

References array_diff_assoc_recursive().

Referenced by isEqual().

00635   {
00636     if (is_array($value1_) && is_array($value2_))
00637     {
00638       $diffArr = array_diff_assoc_recursive($value1_, $value2_);
00639       if (empty($diffArr))
00640       {
00641         $diffArr = array_diff_assoc_recursive($value2_, $value1_);
00642       }
00643     }
00644     else
00645     {
00646       $diffArr = true;
00647     }
00648 
00649     if (empty($diffArr))
00650     {
00651       return true;
00652     }
00653     else
00654     {
00655       return false;
00656     }
00657   }

FOUnitTestBase::_resetFailComment (  )  [private]

Delete all fail fomments.

See also:
assert(), addFailComment()

Definition at line 676 of file FOUnitTestBase.class.php.

Referenced by _addTestResult(), and FOUnitTestBase().

00677   {
00678     $this->_failComment = '';
00679   }

FOUnitTestBase::addSectionText ( str_  ) 

Section text for the next doCompare result.

Used in runTest() to comment different unit tests.

Parameters:
$str_ - mixed - The text.

Definition at line 77 of file FOUnitTestBase.class.php.

00078   {
00079     $this->_currentSectionText[] = $str_;
00080   }

FOUnitTestBase::doTest (  )  [abstract, protected]

This function should be overrided in a child class and include the unit tests.

Examples:

  function doTest()
  {
    eval($this->assert(
      '3+3',
      '6'
    ));
  }

FOUnitTestBase::FOUnitTestBase (  ) 

Constructor.

Definition at line 45 of file FOUnitTestBase.class.php.

References _resetFailComment(), and setSectionLabel().

00046   {
00047     $this->setSectionLabel();
00048 
00049     $this->_resetFailComment();
00050 
00051     $this->_testCount = 0;
00052     $this->_testResult = array();
00053   }

& FOUnitTestBase::getTestResult (  ) 

Will return the result of the doTest test.

Returns:
array The result generated by the doTest.

Definition at line 575 of file FOUnitTestBase.class.php.

00576   {
00577     return $this->_testResult;
00578   }

FOUnitTestBase::isEqual ( value1_,
value2_ 
)

Compares $value1 with $value2, return true if equal.

Can also compare arrays, using serialize.

Parameters:
$value1_ - mixed - Value to be compared with value2
$value2_ - mixed - Value to be compared with value1
Returns:
- string - True if values are equal

Definition at line 592 of file FOUnitTestBase.class.php.

References _isEqualArray().

00593   {
00594     if (is_array($value1__) || is_array($value2__))
00595     {
00596       return $this->_isEqualArray($value1_, $value2_);
00597     }
00598 
00599     //
00600     // Both variables must be of the same type.
00601     //
00602     if (gettype($value1_) == 'double' || gettype($value1_) == 'float')
00603     {
00604       $value1_ = (double)((string)$value1_);
00605     }
00606 
00607     if (gettype($value2_) == 'double' || gettype($value2_) == 'float')
00608     {
00609       $value2_ = (double)((string)$value2_);
00610     }
00611 
00612     if ($value1_ == $value2_)
00613     {
00614       return true;
00615     }
00616     else
00617     {
00618       return false;
00619     }
00620   }

FOUnitTestBase::needDate ( code_  ) 

Used with eval to verify that the return of the php code in $code_ are a date.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

  $this->setSectionLabel('needDate');

  eval($this->needDate('"2001-01-01"'));
  eval($this->needDate('"2001-01-30"'));
  eval($this->needDate('"2001-01-20 20:00"'));
  eval($this->needDate('"2001-01-20 20:00:00"'));
  eval($this->needDate('"2001-01-20 20:00:00"'));
  eval($this->needDate('"01jan2009"'));

Parameters:
$code_ - mixed - Php code that should generate a value that is different from $expectedReturn_
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 455 of file FOUnitTestBase.class.php.

00456   {
00457     return '
00458       $value1 = ' . $code_ . ';
00459       $value2 = strtotime($value1);
00460       $this->_addTestResult
00461       (
00462         "needDate",
00463         $value2 !== false,
00464         "' . htmlEntities(str_replace('$', '\$', $code_)) . '" ,
00465         $value1,
00466         $value2
00467       );';
00468   }

FOUnitTestBase::needDiff ( code_,
expectedReturn_ 
)

Used with eval to verify that the return of the php code in $code_ and the value in $expecedReturn_ is not equal.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

  $this->setSectionLabel('needDiff');

  eval($this->needDiff('(3+2)', '4'));

  $numVar = 5;
  eval($this->needDiff('$numVar', '4'));

  $stringVar = 'Fareoffice';
  eval($this->needDiff('$stringVar', '"fareoffice"'));

  $rootDir = dir("/tmp");
  eval($this->needDiff('$rootDir->path', '"/tMp"'));

 $arr = array(1,2,3,4);
 eval($this->needDiff('$arr', 'unserialize("a:4:{i:0;si:1;i:1;i:2;i:2;i:3;i:3;i:4;}")'));

Parameters:
$code_ - mixed - Php code that should generate a value that is different from $expectedReturn_
$expectedReturn_ - mixed - Php code that should generate a value that is different from value in $code_.
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 364 of file FOUnitTestBase.class.php.

00365   {
00366     return '
00367       $value1 = ' . $code_ . ';
00368       $value2 = ' . $expectedReturn_ . ';
00369       $this->_addTestResult
00370       (
00371         "needDiff",
00372         !$this->isEqual($value1, $value2),
00373         "'.htmlEntities(str_replace('$', '\$', $code_)).'" ,
00374         $value1,
00375         $value2
00376       );';
00377   }

FOUnitTestBase::needEmpty ( code_  ) 

Use with eval to verify that $code_ will generate an empty variable.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

 // Will succeed
 eval($this->needEmpty('""'));

 $nullVar = NULL;
  eval($this->needEmpty('$nullVar'));

  $falseVar = false;
  eval($this->needEmpty('$falseVar'));

  $emptyArray = array();
  eval($this->needEmpty('$emptyArray'));

 // Will fail
 eval($this->needEmpty('"Fareoffice"'));
 eval($this->needEmpty('0'));

 $nonEmptyVar = 'Fareoffice';
 eval($this->needEmpty('$nonEmptyVar'));

  $falseVar = false;
  eval($this->needEmpty('$falseVar'));

Parameters:
$code_ - mixed - Php code that should generate an empty result
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 134 of file FOUnitTestBase.class.php.

00135   {
00136     return '
00137       $value1 = ' . $code_ . ';
00138       $value2= "";
00139       $this->_addTestResult
00140       (
00141         "needEmpty",
00142         emptyString($value1),
00143         "' . htmlEntities(str_replace('$', '\$', $code_)) . '",
00144         $value1,
00145         $value2
00146       );';
00147   }

FOUnitTestBase::needEqual ( code_,
expectedReturn_ 
)

Used with eval to verify that the return of the php code in $code_ and the value in $expecedReturn_ is equal.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

  $this->setSectionLabel('needEqual');

  eval($this->needEqual('(3+2)', '5'));

  $numVar = 5;
  eval($this->needEqual('$numVar', '5'));

  $stringVar = 'Fareoffice';
  eval($this->needEqual('$stringVar', '"Fareoffice"'));

  $rootDir = dir("/tmp");
  eval($this->needEqual('$rootDir->path', '"/tmp"'));

 $arr = array(1,2,3,4);
 eval($this->needEqual('$arr', 'unserialize("a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}")'));

Parameters:
$code_ - mixed - Php code that should generate a value equal to $expectedReturn_
$expectedReturn_ - mixed - Php code that should generate a value equal to the value in $code_.
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 313 of file FOUnitTestBase.class.php.

00314   {
00315     return '
00316       $value1 = ' . $code_ . ';
00317       $value2 = ' . $expectedReturn_ . ';
00318       $this->_addTestResult
00319       (
00320         "needEqual",
00321         $this->isEqual($value1, $value2),
00322         "'.htmlEntities(str_replace('$', '\$', $code_)).'" ,
00323         $value1,
00324         $value2
00325       );';
00326   }

FOUnitTestBase::needEqualReg ( code_,
reqularExp_ 
)

Used with eval to verify that the return of the php code in $code_ matches the reqular expression in reqularExp_.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

  $this->setSectionLabel('needEqualReg');

  eval($this->needEqualReg('(3+102)', '"/^[\d]{2,3}$/"'));

  $numVar = 5;
  eval($this->needEqualReg('$numVar', '"/^[5]$/"'));

  $stringVar = 'Fareoffice';
  eval($this->needEqualReg('$stringVar', '"/^[\w]*$/"'));

  $rootDir = dir("/tmp");
  eval($this->needEqualReg('$rootDir->path', '"/^[\/][\w]*$/"'));

  $today = date('dMY H:i:s', strtotime('now'));
  eval($this->needEqualReg('$today', '"/^[\d]{2}[\w]{3}[\d]{4} [\d]{2}[:][\d]{2}[:][\d]{2}$/"'));

Parameters:
$code_ - mixed - Php code that should generate a value that is different from $expectedReturn_
$reqularExp_ - mixed - Php code that should generate a value that is different from value in $code_.
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 415 of file FOUnitTestBase.class.php.

00416   {
00417     return '
00418       $value1 = ' . $code_ . ';
00419       $value2 = ' . $reqularExp_ . ';
00420       $this->_addTestResult
00421       (
00422         "needEqualReg",
00423         preg_match($value2, $value1),
00424         "' . htmlEntities(str_replace('$', '\$', $code_)) . '" ,
00425         $value1,
00426         $value2
00427       );';
00428   }

FOUnitTestBase::needFalse ( code_  ) 

Use with eval to verify that $code will generate false.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

 // Will succeed
  eval($this->needFalse('(1 != 1)'));
 eval($this->needFalse('false'));
 eval($this->needFalse('!true'));

 $falseVar = false;
 eval($this->needFalse('$falseVar'));

Parameters:
$code_ - mixed - Php code that should generate false.
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 262 of file FOUnitTestBase.class.php.

00263   {
00264     return '
00265       $value1 = ' . $code_ . ';
00266       $value2 = false;
00267       $this->_addTestResult
00268       (
00269         "needFalse",
00270         $this->isEqual($value1, $value2),
00271         "'.htmlEntities(str_replace('$', '\$', $code_)).'",
00272         $value1,
00273         $value2
00274       );';
00275   }

FOUnitTestBase::needNotEmpty ( code_  ) 

Use with eval to verify that $code will generate a value or some text.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

 // Will succeed
  eval($this->needNotEmpty('"Fareoffice"'));
  eval($this->needNotEmpty('0'));

  $nonEmptyVar = 'Fareoffice';
  eval($this->needNotEmpty('$nonEmptyVar'));

  $trueVar = true;
  eval($this->needNotEmpty('$trueVar'));

  $nonEmptyArray = array(0,1,2);
  eval($this->needNotEmpty('$nonEmptyArray'));

 // Will fail
  eval($this->needNotEmpty('""'));

  $nullVar = NULL;
  eval($this->needNotEmpty('$nullVar'));

  $falsVar = false;
  eval($this->needNotEmpty('$falseVar'));

  $emptyArray = array();
  eval($this->needNotEmpty('$emptyArray'));

Parameters:
$code_ - mixed - Php code that should generate a result
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 188 of file FOUnitTestBase.class.php.

00189   {
00190     return '
00191       $value1 = ' . $code_ . ';
00192       $value2= "";
00193       $this->_addTestResult
00194       (
00195         "needNotEmpty",
00196         !emptyString($value1),
00197         "' . htmlEntities(str_replace('$', '\$', $code_)) . '",
00198         $value1,
00199         $value2
00200       );';
00201   }

FOUnitTestBase::needTrue ( code_  ) 

Use with eval to verify that $code will generate true.

Note: The php code send in to the need funktion should look like this '5' or '"Fareoffice"' or '$var' or 'getValue()'

Example:

 // Will succeed
  eval($this->needTrue('(1 == 1)'));
 eval($this->needTrue('true'));
 eval($this->needTrue('!false'));

 $trueVar = true;
 eval($this->needTrue('$trueVar'));

Parameters:
$code_ - mixed - Php code that should generate true.
Returns:
- string - Php code that will be used in an eval() expression.

Definition at line 225 of file FOUnitTestBase.class.php.

00226   {
00227     return '
00228       $value1 = ' . $code_ . ';
00229       $value2 = true;
00230       $this->_addTestResult
00231       (
00232         "needTrue",
00233         $this->isEqual($value1, $value2),
00234         "'.htmlEntities(str_replace('$', '\$', $code_)).'",
00235         $value1,
00236         $value2
00237       );';
00238   }

FOUnitTestBase::setFailComment ( str_  ) 

A comment written when the assert fails.

Used to give hints about what went wrong.

Will be reseted after each test is performed.

Parameters:
$str_ - mixed - The comment.
See also:
_getFailComment(), _resetFailComment()

Definition at line 93 of file FOUnitTestBase.class.php.

00094   {
00095     $this->_failComment = $str_;
00096   }

FOUnitTestBase::setSectionLabel ( str_ = NULL  ) 

Section label for next doCompare result.

Used in runTest() to separate different unit tests.

Parameters:
$str_ - mixed - The label.

Definition at line 63 of file FOUnitTestBase.class.php.

Referenced by FOUnitTestBase().

00064   {
00065     $this->_currentSectionLabel = $str_;
00066     $this->_currentSectionText = array();
00067   }


Member Data Documentation

FOUnitTestBase::$_currentSectionLabel [private]

Section label used by the next call to doCompare.

Definition at line 701 of file FOUnitTestBase.class.php.

FOUnitTestBase::$_currentSectionText [private]

Section text used by the next call to doCompare.

Definition at line 706 of file FOUnitTestBase.class.php.

FOUnitTestBase::$_failComment [private]

A comment written when the assert fails.

Used to give hints about what went wrong.

Will be reseted after each test is performed.

Definition at line 724 of file FOUnitTestBase.class.php.

FOUnitTestBase::$_testCount [private]

Number of test actually done.

Definition at line 711 of file FOUnitTestBase.class.php.

FOUnitTestBase::$_testResult [private]

Array including all the test results, added with _addTestResult.

Definition at line 716 of file FOUnitTestBase.class.php.


The documentation for this class was generated from the following file:

Generated on Wed May 6 23:28:24 2009 for fareofficelib by  doxygen 1.5.8