MyCakeTestCase.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. abstract class MyCakeTestCase extends CakeTestCase {
  3. /*** assert mods ***/
  4. /** enhanced **/
  5. protected static function assertNotWithinMargin($result, $expected, $margin, $message = '') {
  6. $upper = $result + $margin;
  7. $lower = $result - $margin;
  8. return self::assertFalse((($expected <= $upper) && ($expected >= $lower)), $message);
  9. }
  10. //deprecated?
  11. public function assertIsNull($is, $title = null, $value = null, $message = '', $options = array()) {
  12. $expectation = 'NULL';
  13. self::_printTitle($expectation, $title, $options);
  14. self::_printResult($is, $value, $options);
  15. return $this->assertNull($is, $message);
  16. }
  17. //deprecated?
  18. public function assertIsNotNull($is, $title = null, $value = null, $message = '', $options = array()) {
  19. $expectation = 'NOT NULL';
  20. self::_printTitle($expectation, $title, $options);
  21. self::_printResult($is, $value, $options);
  22. return $this->assertNotNull($is, $message);
  23. }
  24. /*** time needed ***/
  25. protected static $startTime = null;
  26. protected function _microtime($precision = 8) {
  27. return round(microtime(true), $precision);
  28. }
  29. protected function _startClock($precision = 8) {
  30. self::$startTime = self::_microtime();
  31. }
  32. protected function _elapsedTime($precision = 8, $restart = false) {
  33. $elapsed = self::_microtime() - self::$startTime;
  34. if ($restart) {
  35. self::_startClock();
  36. }
  37. return round($elapsed, $precision);
  38. }
  39. /*
  40. # cakephp2 phpunit wrapper
  41. public function assertEquals($expected, $actual, $title = null, $value = null, $message = '', $options = array()) {
  42. return $this->assertEqual($expected, $actual, $title, $value, $message, $options);
  43. }
  44. public function assertInternalType($expected, $actual) {
  45. return $this->assertType($expected, $actual);
  46. }
  47. public function markTestIncomplete() {
  48. $this->skipIf(true, '%s - Test Incomplete');
  49. return;
  50. }
  51. */
  52. /*** Helper Functions **/
  53. /**
  54. * outputs debug information during a web tester (browser) test case
  55. * since PHPUnit>=3.6 swallowes all output by default
  56. * this is a convenience output handler since debug() or pr() have no effect
  57. * @param mixed $data
  58. * @param bool $pre should a pre tag be enclosed around the output
  59. * @return void
  60. * 2011-12-04 ms
  61. */
  62. public function out($data, $pre = true) {
  63. if ($pre) {
  64. $data = pre($data);
  65. }
  66. echo $data;
  67. if (empty($_SERVER['HTTP_HOST'])) {
  68. # cli mode / shell access: use the --debug modifier if you are using the CLI interface
  69. return;
  70. }
  71. ob_flush();
  72. }
  73. protected function _basePath($full = false) {
  74. $phpSelf = $_SERVER['PHP_SELF'];
  75. if (strpos($phpSelf, 'webroot/test.php') !== false) {
  76. $pieces = explode('webroot/test.php', $phpSelf, 2);
  77. } else {
  78. $pieces = explode('test.php', $phpSelf, 2);
  79. }
  80. $url = array_shift($pieces);
  81. if ($full) {
  82. $protocol = array_shift(explode('/', $_SERVER['SERVER_PROTOCOL'], 2));
  83. $url = strtolower($protocol).'://'.$_SERVER['SERVER_NAME'].$url;
  84. }
  85. return $url;
  86. }
  87. protected function _header($title) {
  88. if (strpos($title, 'test') === 0) {
  89. $title = substr($title, 4);
  90. $title = Inflector::humanize(Inflector::underscore($title));
  91. }
  92. return '<h3>'.$title.'</h3>';
  93. }
  94. /**
  95. * without trailing slash!?
  96. * //TODO: test
  97. * 2011-04-03 ms
  98. */
  99. protected function _baseurl() {
  100. return current(split("webroot", $_SERVER['PHP_SELF']));
  101. }
  102. /**
  103. * @param float $time
  104. * @param int precision
  105. * @param bool $secs: usually in milliseconds (for long times set it to 'true')
  106. * 2009-07-20 ms
  107. */
  108. protected function _printElapsedTime($time = null, $precision = 8, $secs = false) {
  109. if ($time === null) {
  110. $time = self::_elapsedTime($precision);
  111. }
  112. if ($secs) {
  113. $unit = 's';
  114. $prec = 7;
  115. } else {
  116. $time = $time*1000;
  117. $unit = 'ms';
  118. $prec = 4;
  119. }
  120. $precision = ($precision !== null) ? $precision : $prec;
  121. pr('elapsedTime: '.number_format($time, $precision, ',', '.').' '.$unit);
  122. }
  123. protected 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. protected 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. protected 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. protected 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. * osFix method
  164. *
  165. * @param string $string
  166. * @return string
  167. */
  168. protected function _osFix($string) {
  169. return str_replace(array("\r\n", "\r"), "\n", $string);
  170. }
  171. }