MyCakeTestCase.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /** enhanced **/
  84. public static function assertNull($is, $title = null, $value = null, $message = '', $options = array()) {
  85. $expectation = 'NULL';
  86. self::_printTitle($expectation, $title, $options);
  87. self::_printResult($is, $value, $options);
  88. return parent::assertNull($is, $message);
  89. }
  90. public static function assertNotNull($is, $title = null, $value = null, $message = '', $options = array()) {
  91. $expectation = 'NOT NULL';
  92. self::_printTitle($expectation, $title, $options);
  93. self::_printResult($is, $value, $options);
  94. return parent::assertNotNull($is, $message);
  95. }
  96. /**
  97. * own function: notEmpty
  98. * FAIL on: array(), NULL, '', false, 0
  99. * 2009-07-09 ms
  100. */
  101. //deprecated
  102. public static function assertNotEmpty($is, $title = null, $value = null, $message = '') {
  103. $expectation = 'NOT EMPTY';
  104. self::_printTitle($expectation, $title);
  105. self::_printResult($is, $value);
  106. return parent::assertTrue(!empty($is), $message);
  107. }
  108. //deprecated
  109. public static function assertIsTrue($is, $title = null, $value = null, $message = '') {
  110. $expectation = 'TRUE';
  111. echo self::_title($expectation, $title);
  112. self::_printResult($is, $value);
  113. return parent::assertTrue($is, $message);
  114. }
  115. //deprecated
  116. public static function assertIsFalse($is, $title = null, $value = null, $message = '') {
  117. $expectation = 'FALSE';
  118. echo self::_title($expectation, $title);
  119. self::_printResult($is, $value);
  120. return parent::assertFalse($is, $message);
  121. }
  122. /*** Helper Functions **/
  123. public function _title($expectation, $title = null) {
  124. $eTitle = '{expects: '.$expectation.'}';
  125. if (!empty($title)) {
  126. $eTitle = $title.' '.$eTitle;
  127. }
  128. return BR.BR.'<b>'.$eTitle.'</b>'.BR;
  129. }
  130. public function _printTitle($expectation, $title = null) {
  131. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  132. return false;
  133. }
  134. echo self::_title($expectation, $title);
  135. }
  136. public function _printResults($expected, $is, $pre = null, $status = false) {
  137. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  138. return false;
  139. }
  140. if ($pre !== null) {
  141. echo 'value:';
  142. pr ($pre);
  143. }
  144. echo 'result is:';
  145. pr($is);
  146. if (!$status) {
  147. echo 'result expected:';
  148. pr ($expected);
  149. }
  150. }
  151. public function _printResult($is, $pre = null, $status = false) {
  152. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  153. return false;
  154. }
  155. if ($pre !== null) {
  156. echo 'value:';
  157. pr($pre);
  158. }
  159. echo 'result is:';
  160. pr($is);
  161. }
  162. /**
  163. * outputs debug information during a web tester (browser) test case
  164. * since PHPUnit>=3.6 swallowes all output by default
  165. * this is a convenience output handler since debug() or pr() have no effect
  166. * @param mixed $data
  167. * @param bool $pre should a pre tag be enclosed around the output
  168. * @return void
  169. * 2011-12-04 ms
  170. */
  171. public function out($data, $pre = true) {
  172. if ($pre) {
  173. $data = pre($data);
  174. }
  175. echo $data;
  176. if (empty($_SERVER['HTTP_HOST'])) {
  177. # cli mode / shell access: use the --debug modifier if you are using the CLI interface
  178. return;
  179. }
  180. ob_flush();
  181. }
  182. }