00001 <?php
00039 abstract class FOUnitTestBase
00040 {
00041
00045 public function FOUnitTestBase()
00046 {
00047 $this->setSectionLabel();
00048
00049 $this->_resetFailComment();
00050
00051 $this->_testCount = 0;
00052 $this->_testResult = array();
00053 }
00054
00063 public function setSectionLabel($str_ = NULL)
00064 {
00065 $this->_currentSectionLabel = $str_;
00066 $this->_currentSectionText = array();
00067 }
00068
00077 public function addSectionText($str_)
00078 {
00079 $this->_currentSectionText[] = $str_;
00080 }
00081
00093 function setFailComment($str_)
00094 {
00095 $this->_failComment = $str_;
00096 }
00097
00134 public function needEmpty($code_)
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 }
00148
00188 public function needNotEmpty($code_)
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 }
00202
00225 public function needTrue($code_)
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 }
00239
00262 public function needFalse($code_)
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 }
00276
00313 public function needEqual($code_, $expectedReturn_)
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 }
00327
00364 public function needDiff($code_, $expectedReturn_)
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 }
00378
00415 public function needEqualReg($code_, $reqularExp_)
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 }
00429
00455 public function needDate($code_)
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 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00544 public function _addTestResult($type_, $status_, $code_, $value1_, $value2_)
00545 {
00546 $this->_testCount++;
00547
00548
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 }
00569
00575 public function &getTestResult()
00576 {
00577 return $this->_testResult;
00578 }
00579
00592 public function isEqual($value1_, $value2_)
00593 {
00594 if (is_array($value1__) || is_array($value2__))
00595 {
00596 return $this->_isEqualArray($value1_, $value2_);
00597 }
00598
00599
00600
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 }
00621
00634 private function _isEqualArray($value1_, $value2_)
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 }
00658
00666 private function _getFailComment()
00667 {
00668 return $this->_failComment;
00669 }
00670
00676 private function _resetFailComment()
00677 {
00678 $this->_failComment = '';
00679 }
00680
00696 abstract protected function doTest();
00697
00701 private $_currentSectionLabel;
00702
00706 private $_currentSectionText;
00707
00711 private $_testCount;
00712
00716 private $_testResult;
00717
00724 private $_failComment;
00725 }
00726 ?>