Advertisement
Guest User

Untitled

a guest
Mar 6th, 2012
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. <?php
  2. /**
  3. * implementation of a very basic unit testing class
  4. */
  5. abstract class UnitTest {
  6.  
  7. /**
  8. *
  9. * check the value is true
  10. * @param mixed $expr
  11. */
  12. public function assertTrue($expr) {
  13. if (!$expr) {
  14. $this->_printAssertResult("{$expr} - assertTrue failed!");
  15. }
  16. }
  17.  
  18. /**
  19. *
  20. * check the value is false
  21. * @param mixed $expr
  22. */
  23. public function assertFalse($expr) {
  24. if ($expr) {
  25. $this->_printAssertResult("{$expr} - assertFalse failed!");
  26. }
  27. }
  28.  
  29. /**
  30. *
  31. * check the two values are equal
  32. * @param mixed $expr1
  33. * @param mixed $expr2
  34. */
  35. public function assertEquals($expr1, $expr2) {
  36. if ($expr1 != $expr2) {
  37. $this->_printAssertResult("{$expr1} not equals {$expr2}");
  38. }
  39. }
  40.  
  41. /**
  42. *
  43. * check the two values are equal and have the same type
  44. * @param mixed $expr1
  45. * @param mixed $expr2
  46. */
  47. public function assertEqualsStrict($expr1, $expr2) {
  48. if ($expr1 !== $expr2) {
  49. $this->_printAssertResult("{$expr1} not equals {$expr2}");
  50. }
  51. }
  52.  
  53. /**
  54. *
  55. * execute the tests in the extending class.
  56. * All testing method should start with "test" (lowercase)
  57. */
  58. public function run() {
  59. echo '---- Starting the Test (',date('d.m.Y H:i:s'),') -----', "\r\n";
  60.  
  61. // read all methods, and execute tests
  62. $methods_list = get_class_methods($this);
  63. if (!empty($methods_list)) {
  64. foreach ($methods_list as $method) {
  65. if (preg_match('%^test%', $method)) {
  66. echo 'testing ', preg_replace('%^test%', '', $method), "\r\n";
  67. $this->$method();
  68. }
  69. }
  70. }
  71. }
  72.  
  73. /** ************ PROTECTED SECTION *************** **/
  74.  
  75. /**
  76. *
  77. * print assertion result
  78. * @param string $message
  79. */
  80. protected function _printAssertResult($message) {
  81. echo "{$message}\n";
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement