MyCakeTestCase.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. App::uses('ShimTestCase', 'Shim.TestSuite');
  3. abstract class MyCakeTestCase extends ShimTestCase {
  4. protected static $_startTime = null;
  5. /**
  6. * @param int $precision
  7. * @return float
  8. */
  9. protected function _microtime($precision = 8) {
  10. return round(microtime(true), $precision);
  11. }
  12. /**
  13. * @param int $precision
  14. * @return void
  15. */
  16. protected function _startClock($precision = 8) {
  17. static::$_startTime = static::_microtime();
  18. }
  19. /**
  20. * @param int $precision
  21. * @param bool $restart
  22. * @return float
  23. */
  24. protected function _elapsedTime($precision = 8, $restart = false) {
  25. $elapsed = static::_microtime() - static::$_startTime;
  26. if ($restart) {
  27. static::_startClock();
  28. }
  29. return round($elapsed, $precision);
  30. }
  31. /**
  32. * @param float $time
  33. * @param int precision
  34. * @param bool $secs: usually in milliseconds (for long times set it to 'true')
  35. */
  36. protected function _printElapsedTime($time = null, $precision = 8, $secs = false) {
  37. if ($time === null) {
  38. $time = static::_elapsedTime($precision);
  39. }
  40. if ($secs) {
  41. $unit = 's';
  42. $prec = 7;
  43. } else {
  44. $time = $time * 1000;
  45. $unit = 'ms';
  46. $prec = 4;
  47. }
  48. $precision = ($precision !== null) ? $precision : $prec;
  49. pr('elapsedTime: ' . number_format($time, $precision, ',', '.') . ' ' . $unit);
  50. }
  51. }