MyCakeTestCase.php 5.5 KB

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