debugTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * Debug测试
  13. * @author 大漠 <zhylninc@gmail.com>
  14. */
  15. namespace tests\thinkphp\library\think;
  16. use tests\thinkphp\library\think\config\ConfigInitTrait;
  17. use think\Config;
  18. use think\Debug;
  19. use think\Response;
  20. use think\response\Redirect;
  21. class debugTest extends \PHPUnit_Framework_TestCase
  22. {
  23. use ConfigInitTrait;
  24. /**
  25. *
  26. * @var Debug
  27. */
  28. protected $object;
  29. /**
  30. * Sets up the fixture, for example, opens a network connection.
  31. * This method is called before a test is executed.
  32. */
  33. protected function setUp()
  34. {
  35. $this->object = new Debug();
  36. }
  37. /**
  38. * Tears down the fixture, for example, closes a network connection.
  39. * This method is called after a test is executed.
  40. */
  41. protected function tearDown()
  42. {}
  43. /**
  44. * @covers \think\Debug::remark
  45. * @todo Implement testRemark().
  46. */
  47. public function testRemark()
  48. {
  49. $name = "testremarkkey";
  50. Debug::remark($name);
  51. }
  52. /**
  53. * @covers \think\Debug::getRangeTime
  54. * @todo Implement testGetRangeTime().
  55. */
  56. public function testGetRangeTime()
  57. {
  58. $start = "testGetRangeTimeStart";
  59. $end = "testGetRangeTimeEnd";
  60. Debug::remark($start);
  61. usleep(20000);
  62. // \think\Debug::remark($end);
  63. $time = Debug::getRangeTime($start, $end);
  64. $this->assertLessThan(0.03, $time);
  65. //$this->assertEquals(0.03, ceil($time));
  66. }
  67. /**
  68. * @covers \think\Debug::getUseTime
  69. * @todo Implement testGetUseTime().
  70. */
  71. public function testGetUseTime()
  72. {
  73. $time = Debug::getUseTime();
  74. $this->assertLessThan(30, $time);
  75. }
  76. /**
  77. * @covers \think\Debug::getThroughputRate
  78. * @todo Implement testGetThroughputRate().
  79. */
  80. public function testGetThroughputRate()
  81. {
  82. usleep(100000);
  83. $throughputRate = Debug::getThroughputRate();
  84. $this->assertLessThan(10, $throughputRate);
  85. }
  86. /**
  87. * @covers \think\Debug::getRangeMem
  88. * @todo Implement testGetRangeMem().
  89. */
  90. public function testGetRangeMem()
  91. {
  92. $start = "testGetRangeMemStart";
  93. $end = "testGetRangeMemEnd";
  94. Debug::remark($start);
  95. $str = "";
  96. for ($i = 0; $i < 10000; $i++) {
  97. $str .= "mem";
  98. }
  99. $rangeMem = Debug::getRangeMem($start, $end);
  100. $this->assertLessThan(33, explode(" ", $rangeMem)[0]);
  101. }
  102. /**
  103. * @covers \think\Debug::getUseMem
  104. * @todo Implement testGetUseMem().
  105. */
  106. public function testGetUseMem()
  107. {
  108. $useMem = Debug::getUseMem();
  109. $this->assertLessThan(35, explode(" ", $useMem)[0]);
  110. }
  111. /**
  112. * @covers \think\Debug::getMemPeak
  113. * @todo Implement testGetMemPeak().
  114. */
  115. public function testGetMemPeak()
  116. {
  117. $start = "testGetMemPeakStart";
  118. $end = "testGetMemPeakEnd";
  119. Debug::remark($start);
  120. $str = "";
  121. for ($i = 0; $i < 100000; $i++) {
  122. $str .= "mem";
  123. }
  124. $memPeak = Debug::getMemPeak($start, $end);
  125. $this->assertLessThan(500, explode(" ", $memPeak)[0]);
  126. }
  127. /**
  128. * @covers \think\Debug::getFile
  129. * @todo Implement testGetFile().
  130. */
  131. public function testGetFile()
  132. {
  133. $count = Debug::getFile();
  134. $this->assertEquals(count(get_included_files()), $count);
  135. $info = Debug::getFile(true);
  136. $this->assertEquals(count(get_included_files()), count($info));
  137. $this->assertContains("KB", $info[0]);
  138. }
  139. /**
  140. * @covers \think\Debug::dump
  141. * @todo Implement testDump().
  142. */
  143. public function testDump()
  144. {
  145. if (strstr(PHP_VERSION, 'hhvm')) {
  146. return;
  147. }
  148. $var = [];
  149. $var["key"] = "val";
  150. $output = Debug::dump($var, false, $label = "label");
  151. $array = explode("array", json_encode($output));
  152. if (IS_WIN) {
  153. $this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\r\\n\"", end($array));
  154. } elseif (strstr(PHP_OS, 'Darwin')) {
  155. $this->assertEquals("(1) {\\n [\\\"key\\\"] => string(3) \\\"val\\\"\\n}\\n\\n\"", end($array));
  156. } else {
  157. $this->assertEquals("(1) {\\n 'key' =>\\n string(3) \\\"val\\\"\\n}\\n\\n\"", end($array));
  158. }
  159. }
  160. /**
  161. * @expectedException \think\exception\ClassNotFoundException
  162. */
  163. public function testInjectWithErrorType()
  164. {
  165. Config::set('trace', ['type' => 'NullDebug']);
  166. $response = new Response();
  167. $context = 'TestWithErrorType';
  168. Debug::inject($response, $context);
  169. }
  170. public function testInject()
  171. {
  172. Config::set('trace', ['type' => 'Console']);
  173. $response = new Response();
  174. $context = 'TestWithoutBodyTag';
  175. Debug::inject($response, $context);
  176. $this->assertNotEquals('TestWithoutBodyTag', $context);
  177. $this->assertStringStartsWith('TestWithoutBodyTag', $context);
  178. $response = new Response();
  179. $context = '<body></body>';
  180. Debug::inject($response, $context);
  181. $this->assertNotEquals('<body></body>', $context);
  182. $this->assertStringStartsWith('<body>', $context);
  183. $this->assertStringEndsWith('</body>', $context);
  184. $response = new Redirect();
  185. $context = '<body></body>';
  186. Debug::inject($response, $context);
  187. $this->assertEquals('<body></body>', $context);
  188. }
  189. }