MyCakeTestCase.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 static function out($data, $pre = true) {
  63. if (PHP_SAPI === 'cli') {
  64. return;
  65. }
  66. if ($pre) {
  67. pr($data);
  68. } else {
  69. echo $data;
  70. }
  71. if (empty($_SERVER['HTTP_HOST'])) {
  72. # cli mode / shell access: use the --debug modifier if you are using the CLI interface
  73. return;
  74. }
  75. ob_flush();
  76. }
  77. protected function _basePath($full = false) {
  78. $phpSelf = $_SERVER['PHP_SELF'];
  79. if (strpos($phpSelf, 'webroot/test.php') !== false) {
  80. $pieces = explode('webroot/test.php', $phpSelf, 2);
  81. } else {
  82. $pieces = explode('test.php', $phpSelf, 2);
  83. }
  84. $url = array_shift($pieces);
  85. if ($full) {
  86. $protocol = array_shift(explode('/', $_SERVER['SERVER_PROTOCOL'], 2));
  87. $url = strtolower($protocol).'://'.$_SERVER['SERVER_NAME'].$url;
  88. }
  89. return $url;
  90. }
  91. protected function _header($title) {
  92. if (strpos($title, 'test') === 0) {
  93. $title = substr($title, 4);
  94. $title = Inflector::humanize(Inflector::underscore($title));
  95. }
  96. return '<h3>'.$title.'</h3>';
  97. }
  98. /**
  99. * without trailing slash!?
  100. * //TODO: test
  101. * 2011-04-03 ms
  102. */
  103. protected function _baseurl() {
  104. return current(split("webroot", $_SERVER['PHP_SELF']));
  105. }
  106. /**
  107. * @param float $time
  108. * @param int precision
  109. * @param bool $secs: usually in milliseconds (for long times set it to 'true')
  110. * 2009-07-20 ms
  111. */
  112. protected function _printElapsedTime($time = null, $precision = 8, $secs = false) {
  113. if ($time === null) {
  114. $time = self::_elapsedTime($precision);
  115. }
  116. if ($secs) {
  117. $unit = 's';
  118. $prec = 7;
  119. } else {
  120. $time = $time*1000;
  121. $unit = 'ms';
  122. $prec = 4;
  123. }
  124. $precision = ($precision !== null) ? $precision : $prec;
  125. pr('elapsedTime: '.number_format($time, $precision, ',', '.').' '.$unit);
  126. }
  127. protected function _title($expectation, $title = null) {
  128. $eTitle = '{expects: '.$expectation.'}';
  129. if (!empty($title)) {
  130. $eTitle = $title.' '.$eTitle;
  131. }
  132. return BR.BR.'<b>'.$eTitle.'</b>'.BR;
  133. }
  134. protected function _printTitle($expectation, $title = null) {
  135. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  136. return false;
  137. }
  138. echo self::_title($expectation, $title);
  139. }
  140. protected function _printResults($expected, $is, $pre = null, $status = false) {
  141. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  142. return false;
  143. }
  144. if ($pre !== null) {
  145. echo 'value:';
  146. pr ($pre);
  147. }
  148. echo 'result is:';
  149. pr($is);
  150. if (!$status) {
  151. echo 'result expected:';
  152. pr ($expected);
  153. }
  154. }
  155. protected function _printResult($is, $pre = null, $status = false) {
  156. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  157. return false;
  158. }
  159. if ($pre !== null) {
  160. echo 'value:';
  161. pr($pre);
  162. }
  163. echo 'result is:';
  164. pr($is);
  165. }
  166. /**
  167. * osFix method
  168. *
  169. * @param string $string
  170. * @return string
  171. */
  172. protected function _osFix($string) {
  173. return str_replace(array("\r\n", "\r"), "\n", $string);
  174. }
  175. }