MyCakeTestCase.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. abstract class MyCakeTestCase extends CakeTestCase {
  3. /*** time needed ***/
  4. protected static $startTime = null;
  5. public function _microtime($precision = 8) {
  6. return round(microtime(true), $precision);
  7. }
  8. public function _startClock($precision = 8) {
  9. self::$startTime = self::_microtime();
  10. }
  11. public function _elapsedTime($precision = 8, $restart = false) {
  12. $elapsed = self::_microtime() - self::$startTime;
  13. if ($restart) {
  14. self::_startClock();
  15. }
  16. return round($elapsed, $precision);
  17. }
  18. public function _header($title) {
  19. if (strpos($title, 'test') === 0) {
  20. $title = substr($title, 4);
  21. $title = Inflector::humanize(Inflector::underscore($title));
  22. }
  23. return '<h3>'.$title.'</h3>';
  24. }
  25. /**
  26. * without trailing slash!?
  27. * //TODO: test
  28. * 2011-04-03 ms
  29. */
  30. public function _baseurl() {
  31. return current(split("webroot", $_SERVER['PHP_SELF']));
  32. }
  33. /*
  34. # cakephp2 phpunit wrapper
  35. public function assertEquals($expected, $actual, $title = null, $value = null, $message = '', $options = array()) {
  36. return $this->assertEqual($expected, $actual, $title, $value, $message, $options);
  37. }
  38. public function assertInternalType($expected, $actual) {
  39. return $this->assertType($expected, $actual);
  40. }
  41. public function markTestIncomplete() {
  42. $this->skipIf(true, '%s - Test Incomplete');
  43. return;
  44. }
  45. */
  46. # helper methods
  47. public function _basePath($full = false) {
  48. $phpSelf = $_SERVER['PHP_SELF'];
  49. if (strpos($phpSelf, 'webroot/test.php') !== false) {
  50. $pieces = explode('webroot/test.php', $phpSelf, 2);
  51. } else {
  52. $pieces = explode('test.php', $phpSelf, 2);
  53. }
  54. $url = array_shift($pieces);
  55. if ($full) {
  56. $protocol = array_shift(explode('/', $_SERVER['SERVER_PROTOCOL'], 2));
  57. $url = strtolower($protocol).'://'.$_SERVER['SERVER_NAME'].$url;
  58. }
  59. return $url;
  60. }
  61. /**
  62. * @param float $time
  63. * @param int precision
  64. * @param bool $secs: usually in milliseconds (for long times set it to 'true')
  65. * 2009-07-20 ms
  66. */
  67. public function _printElapsedTime($time = null, $precision = 8, $secs = false) {
  68. if ($time === null) {
  69. $time = self::_elapsedTime($precision);
  70. }
  71. if ($secs) {
  72. $unit = 's';
  73. $prec = 7;
  74. } else {
  75. $time = $time*1000;
  76. $unit = 'ms';
  77. $prec = 4;
  78. }
  79. $precision = ($precision !== null) ? $precision : $prec;
  80. pr('elapsedTime: '.number_format($time, $precision, ',', '.').' '.$unit);
  81. }
  82. /*** assert mods ***/
  83. /** compatibility */
  84. protected static function assertEqual($expected, $is, $title = null, $value = null, $message = '', $options = array()) {
  85. $expectation = 'EQUAL';
  86. self::_printTitle($expectation, $title, $options);
  87. self::_printResults($expected, $is, $value, $options);
  88. return parent::assertEqual($expected, $is, $message);
  89. }
  90. protected static function assertNotEqual($expected, $is, $title = null, $value = null, $message = '', $options = array()) {
  91. $expectation = 'NOT EQUAL';
  92. self::_printTitle($expectation, $title, $options);
  93. self::_printResults($expected, $is, $value, $options);
  94. return parent::assertNotEqual($expected, $is, $message);
  95. }
  96. protected static function assertPattern($expected, $is, $title = null, $value = null, $message = '', $options = array()) {
  97. $expectation = 'PATTERN';
  98. self::_printTitle($expectation, $title, $options);
  99. self::_printResults($expected, $is, $value, $options);
  100. return parent::assertNotEqual($expected, $is, $message);
  101. }
  102. protected static function assertWithinMargin($result, $expected, $margin, $message = '') {
  103. return parent::assertWithinMargin($result, $expected, $margin, $message);
  104. }
  105. protected static function assertIsA($object, $type, $message ='') {
  106. return parent::assertIsA($object, $type, $message);
  107. }
  108. /** enhanced **/
  109. public static function assertNull($is, $title = null, $value = null, $message = '', $options = array()) {
  110. $expectation = 'NULL';
  111. self::_printTitle($expectation, $title, $options);
  112. self::_printResult($is, $value, $options);
  113. return parent::assertNull($is, $message);
  114. }
  115. public static function assertNotNull($is, $title = null, $value = null, $message = '', $options = array()) {
  116. $expectation = 'NOT NULL';
  117. self::_printTitle($expectation, $title, $options);
  118. self::_printResult($is, $value, $options);
  119. return parent::assertNotNull($is, $message);
  120. }
  121. /**
  122. * own function: notEmpty
  123. * FAIL on: array(), NULL, '', false, 0
  124. * 2009-07-09 ms
  125. */
  126. public static function assertNotEmpty($is, $title = null, $value = null, $message = '') {
  127. $expectation = 'NOT EMPTY';
  128. self::_printTitle($expectation, $title);
  129. self::_printResult($is, $value);
  130. return parent::assertTrue(!empty($is), $message);
  131. }
  132. public static function assertIsTrue($is, $title = null, $value = null, $message = '') {
  133. $expectation = 'TRUE';
  134. echo self::_title($expectation, $title);
  135. self::_printResult($is, $value);
  136. return parent::assertTrue($is, $message);
  137. }
  138. public static function assertIsFalse($is, $title = null, $value = null, $message = '') {
  139. $expectation = 'FALSE';
  140. echo self::_title($expectation, $title);
  141. self::_printResult($is, $value);
  142. return parent::assertFalse($is, $message);
  143. }
  144. /*** Helper Functions **/
  145. public function _title($expectation, $title = null) {
  146. $eTitle = '{expects: '.$expectation.'}';
  147. if (!empty($title)) {
  148. $eTitle = $title.' '.$eTitle;
  149. }
  150. return BR.BR.'<b>'.$eTitle.'</b>'.BR;
  151. }
  152. public function _printTitle($expectation, $title = null) {
  153. /*
  154. if (!self::_reporter->params['show_passes']) {
  155. return false;
  156. }
  157. */
  158. echo self::_title($expectation, $title);
  159. }
  160. public function _printResults($expected, $is, $pre = null, $status = false) {
  161. /*
  162. if (!self::_reporter->params['show_passes']) {
  163. return false;
  164. }
  165. */
  166. if ($pre !== null) {
  167. echo 'value:';
  168. pr ($pre);
  169. }
  170. echo 'result is:';
  171. pr($is);
  172. if (!$status) {
  173. echo 'result expected:';
  174. pr ($expected);
  175. }
  176. }
  177. public function _printResult($is, $pre = null, $status = false) {
  178. /*
  179. if (!self::_reporter->params['show_passes']) {
  180. return false;
  181. }
  182. */
  183. if ($pre !== null) {
  184. echo 'value:';
  185. pr($pre);
  186. }
  187. echo 'result is:';
  188. pr($is);
  189. }
  190. public function out($array, $pre = true) {
  191. if (empty($_SERVER['HTTP_HOST'])) {
  192. # cake shell!!!
  193. return;
  194. }
  195. if ($pre) {
  196. $array = pre($array);
  197. }
  198. echo $array;
  199. }
  200. }