MyCakeTestCase.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 boolean $force Should the output be flushed (forced)
  59. * @return void
  60. */
  61. public static function debug($data, $force = false) {
  62. if (php_sapi_name() === 'cli') {
  63. return;
  64. }
  65. debug($data, null, false);
  66. if (!$force) {
  67. return;
  68. }
  69. ob_flush();
  70. }
  71. /**
  72. * Outputs debug information during a web tester (browser) test case
  73. * since PHPUnit>=3.6 swallowes all output by default
  74. * this is a convenience output handler
  75. * @param mixed $data
  76. * @param boolean $force Should the output be flushed (forced)
  77. * @return void
  78. */
  79. public static function out($data, $plain = false, $force = false) {
  80. if (php_sapi_name() === 'cli') {
  81. return;
  82. }
  83. if (!$plain|| is_array($data)) {
  84. pr($data);
  85. } else {
  86. echo '<div>' . $data . '</div>';
  87. }
  88. if (!$force) {
  89. return;
  90. }
  91. ob_flush();
  92. }
  93. protected function _basePath($full = false) {
  94. $phpSelf = $_SERVER['PHP_SELF'];
  95. if (strpos($phpSelf, 'webroot/test.php') !== false) {
  96. $pieces = explode('webroot/test.php', $phpSelf, 2);
  97. } else {
  98. $pieces = explode('test.php', $phpSelf, 2);
  99. }
  100. $url = array_shift($pieces);
  101. if ($full) {
  102. $protocol = array_shift(explode('/', $_SERVER['SERVER_PROTOCOL'], 2));
  103. $url = strtolower($protocol).'://'.$_SERVER['SERVER_NAME'].$url;
  104. }
  105. return $url;
  106. }
  107. protected function _header($title) {
  108. if (strpos($title, 'test') === 0) {
  109. $title = substr($title, 4);
  110. $title = Inflector::humanize(Inflector::underscore($title));
  111. }
  112. return '<h3>'.$title.'</h3>';
  113. }
  114. /**
  115. * Without trailing slash!?
  116. * //TODO: test
  117. */
  118. protected function _baseurl() {
  119. return current(split("webroot", $_SERVER['PHP_SELF']));
  120. }
  121. /**
  122. * @param float $time
  123. * @param integer precision
  124. * @param boolean $secs: usually in milliseconds (for long times set it to 'true')
  125. */
  126. protected function _printElapsedTime($time = null, $precision = 8, $secs = false) {
  127. if ($time === null) {
  128. $time = self::_elapsedTime($precision);
  129. }
  130. if ($secs) {
  131. $unit = 's';
  132. $prec = 7;
  133. } else {
  134. $time = $time*1000;
  135. $unit = 'ms';
  136. $prec = 4;
  137. }
  138. $precision = ($precision !== null) ? $precision : $prec;
  139. pr('elapsedTime: '.number_format($time, $precision, ',', '.').' '.$unit);
  140. }
  141. protected function _title($expectation, $title = null) {
  142. $eTitle = '{expects: '.$expectation.'}';
  143. if (!empty($title)) {
  144. $eTitle = $title.' '.$eTitle;
  145. }
  146. return BR.BR.'<b>'.$eTitle.'</b>'.BR;
  147. }
  148. protected function _printTitle($expectation, $title = null) {
  149. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  150. return false;
  151. }
  152. echo self::_title($expectation, $title);
  153. }
  154. protected function _printResults($expected, $is, $pre = null, $status = false) {
  155. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  156. return false;
  157. }
  158. if ($pre !== null) {
  159. echo 'value:';
  160. pr ($pre);
  161. }
  162. echo 'result is:';
  163. pr($is);
  164. if (!$status) {
  165. echo 'result expected:';
  166. pr ($expected);
  167. }
  168. }
  169. protected function _printResult($is, $pre = null, $status = false) {
  170. if (empty($_SERVER['HTTP_HOST']) || !isset($_GET['show_passes']) || !$_GET['show_passes']) {
  171. return false;
  172. }
  173. if ($pre !== null) {
  174. echo 'value:';
  175. pr($pre);
  176. }
  177. echo 'result is:';
  178. pr($is);
  179. }
  180. /**
  181. * OsFix method
  182. *
  183. * @param string $string
  184. * @return string
  185. */
  186. protected function _osFix($string) {
  187. return str_replace(array("\r\n", "\r"), "\n", $string);
  188. }
  189. }