You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
13 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-28
  8. *
  9. * Unit tests for CommandProfiler class
  10. */
  11. require_once dirname(__FILE__) . '/../../../Registry.php';
  12. require_once dirname(__FILE__) . '/../../../Config.php';
  13. require_once dirname(__FILE__) . '/../../../exception/GeneralException.php';
  14. require_once dirname(__FILE__) . '/../../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php';
  15. require_once dirname(__FILE__) . '/../../../util/profiler/Profiler.php';
  16. /**
  17. * @TODO: Profiler->getJson() hardly depends on FB library
  18. */
  19. class ProfilerTest extends PHPUnit_Framework_TestCase
  20. {
  21. public function run(PHPUnit_Framework_TestResult $result = NULL)
  22. {
  23. $this->setPreserveGlobalState(false);
  24. return parent::run($result);
  25. }
  26. public function setUp()
  27. {
  28. set_new_overload(array($this, 'newCallback'));
  29. }
  30. /**
  31. * @runInSeparateProcess
  32. */
  33. public function testGetInstaceNoDebug()
  34. {
  35. Config::set('PROFILER', false);
  36. $this->setExpectedException('GeneralException', 'Need turn PROFILER before use.');
  37. Profiler::getInstance();
  38. }
  39. /**
  40. * @runInSeparateProcess
  41. */
  42. public function testGetInstance()
  43. {
  44. Config::set('PROFILER', true);
  45. $profiler = Profiler::getInstance();
  46. $this->assertInstanceOf('Profiler', $profiler);
  47. $this->assertSame($profiler, Profiler::getInstance());
  48. }
  49. /**
  50. * @runInSeparateProcess
  51. */
  52. public function testProfilerCommand()
  53. {
  54. Config::set('PROFILER', true);
  55. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  56. $profiler = Profiler::getInstance();
  57. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  58. $this->assertInstanceOf('CommandProfiler', $cmdProfiler);
  59. }
  60. /**
  61. * @runInSeparateProcess
  62. */
  63. public function testStartEndNoCommandProfiler()
  64. {
  65. Config::set('PROFILER', true);
  66. Config::set('PROFILER_DETAILS', true);
  67. $profiler = Profiler::getInstance();
  68. $profiler->start();
  69. $result = $profiler->end('<body></body>');
  70. $this->assertNotEquals('<body></body>', $result);
  71. $this->assertStringStartsWith('<body>', $result);
  72. $this->assertStringEndsWith(']<br/></div></body>', $result);
  73. $this->assertContains('<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">', $result);
  74. $this->assertSame('html', $profiler->end('html'));
  75. }
  76. /**
  77. * @runInSeparateProcess
  78. */
  79. public function testStartEndWithCommandProfiler()
  80. {
  81. Config::set('PROFILER', true);
  82. Config::set('PROFILER_DETAILS', true);
  83. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  84. $profiler = Profiler::getInstance();
  85. $profiler->start();
  86. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  87. $this->assertNotNull($cmdProfiler);
  88. $result = $profiler->end('<body></body>');
  89. $this->assertNotEquals('<body></body>', $result);
  90. $this->assertStringEndsNotWith(']<br/></div></body>', $result);
  91. $this->assertContains('Queries: 1 [0 ms]<br/>() [0ms] <br/>', $result);
  92. }
  93. /**
  94. * @runInSeparateProcess
  95. */
  96. public function testStartEndWithCommandProfilerWithNonDetails()
  97. {
  98. Config::set('PROFILER', true);
  99. Config::set('PROFILER_DETAILS', false);
  100. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  101. $profiler = Profiler::getInstance();
  102. $profiler->start();
  103. $result = $profiler->end('<body></body>');
  104. $this->assertNotEquals('<body></body>', $result);
  105. $this->assertContains('Queries not counted.', $result);
  106. }
  107. /**
  108. * @runInSeparateProcess
  109. */
  110. public function testStartEndWithCommandProfilerWithNonDetailsButExistingQueries()
  111. {
  112. Config::set('PROFILER', true);
  113. Config::set('PROFILER_DETAILS', false);
  114. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  115. $profiler = Profiler::getInstance();
  116. $profiler->start();
  117. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  118. $result = $profiler->end('<body></body>');
  119. $this->assertNotEquals('<body></body>', $result);
  120. $this->assertStringEndsNotWith(']<br/></div></body>', $result);
  121. $this->assertContains('Queries: 1', $result);
  122. }
  123. /**
  124. * @runInSeparateProcess
  125. */
  126. public function testStartEndWithCommandProfilerWithDetailsButNonQueries()
  127. {
  128. Config::set('PROFILER', true);
  129. Config::set('PROFILER_DETAILS', true);
  130. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  131. $profiler = Profiler::getInstance();
  132. $profiler->start();
  133. $result = $profiler->end('<body></body>');
  134. $this->assertNotEquals('<body></body>', $result);
  135. $this->assertStringEndsWith(']<br/></div></body>', $result);
  136. $this->assertContains('Queries: 0', $result);
  137. }
  138. /**
  139. * @runInSeparateProcess
  140. */
  141. public function testGetJSON()
  142. {
  143. Config::set('PROFILER', true);
  144. Config::set('PROFILER_DETAILS', true);
  145. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  146. $profiler = Profiler::getInstance();
  147. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  148. $fire_php_mock = $this->getMockBuilder('FirePHP')
  149. ->disableOriginalConstructor()
  150. ->setMethods(array('__construct', 'fb'))
  151. ->getMock();
  152. $fire_php_mock->expects($this->exactly(2))
  153. ->method('fb')
  154. ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // Expected "Queries: 1"
  155. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  156. $reflection_property->setAccessible(true);
  157. $reflection_property->setValue($fire_php_mock);
  158. $this->assertNull($profiler->getJson());
  159. }
  160. /**
  161. * @runInSeparateProcess
  162. */
  163. public function testGetJSONWithNonDetails()
  164. {
  165. Config::set('PROFILER', true);
  166. Config::set('PROFILER_DETAILS', false);
  167. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  168. $profiler = Profiler::getInstance();
  169. $fire_php_mock = $this->getMockBuilder('FirePHP')
  170. ->disableOriginalConstructor()
  171. ->setMethods(array('__construct', 'fb'))
  172. ->getMock();
  173. $fire_php_mock->expects($this->exactly(2))
  174. ->method('fb')
  175. ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries not counted"
  176. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  177. $reflection_property->setAccessible(true);
  178. $reflection_property->setValue($fire_php_mock);
  179. $this->assertNull($profiler->getJson());
  180. }
  181. /**
  182. * @runInSeparateProcess
  183. */
  184. public function testGetJSONWithNonDetailsButExistingQueries()
  185. {
  186. Config::set('PROFILER', true);
  187. Config::set('PROFILER_DETAILS', false);
  188. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  189. $profiler = Profiler::getInstance();
  190. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  191. $fire_php_mock = $this->getMockBuilder('FirePHP')
  192. ->disableOriginalConstructor()
  193. ->setMethods(array('__construct', 'fb'))
  194. ->getMock();
  195. $fire_php_mock->expects($this->exactly(2))
  196. ->method('fb')
  197. ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries: 1"
  198. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  199. $reflection_property->setAccessible(true);
  200. $reflection_property->setValue($fire_php_mock);
  201. $this->assertNull($profiler->getJson());
  202. }
  203. /**
  204. * @runInSeparateProcess
  205. */
  206. public function testGetJSONWithDetailsButNonQueries()
  207. {
  208. Config::set('PROFILER', true);
  209. Config::set('PROFILER_DETAILS', true);
  210. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  211. $profiler = Profiler::getInstance();
  212. $fire_php_mock = $this->getMockBuilder('FirePHP')
  213. ->disableOriginalConstructor()
  214. ->setMethods(array('__construct', 'fb'))
  215. ->getMock();
  216. $fire_php_mock->expects($this->exactly(2))
  217. ->method('fb')
  218. ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))); // expected "Queries: 0"
  219. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  220. $reflection_property->setAccessible(true);
  221. $reflection_property->setValue($fire_php_mock);
  222. $this->assertNull($profiler->getJson());
  223. }
  224. /**
  225. * @runInSeparateProcess
  226. */
  227. public function testGetCLI()
  228. {
  229. Config::set('PROFILER', true);
  230. Config::set('PROFILER_DETAILS', true);
  231. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  232. $profiler = Profiler::getInstance();
  233. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  234. $result = $profiler->getCli();
  235. $this->assertNotNull($result);
  236. $this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
  237. }
  238. /**
  239. * @runInSeparateProcess
  240. */
  241. public function testGetCLIWithNonDetails()
  242. {
  243. Config::set('PROFILER', true);
  244. Config::set('PROFILER_DETAILS', false);
  245. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  246. $profiler = Profiler::getInstance();
  247. $result = $profiler->getCli();
  248. $this->assertNotNull($result);
  249. $this->assertContains('Queries not counted', Profiler::getInstance()->getCli());
  250. }
  251. /**
  252. * @runInSeparateProcess
  253. */
  254. public function testGetCLIWithNonDetailsButExistingQueries()
  255. {
  256. Config::set('PROFILER', true);
  257. Config::set('PROFILER_DETAILS', false);
  258. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  259. $profiler = Profiler::getInstance();
  260. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  261. $result = $profiler->getCli();
  262. $this->assertNotNull($result);
  263. $this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
  264. }
  265. /**
  266. * @runInSeparateProcess
  267. */
  268. public function testGetCLIWithDetailsButNonQueries()
  269. {
  270. Config::set('PROFILER', true);
  271. Config::set('PROFILER_DETAILS', true);
  272. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  273. $profiler = Profiler::getInstance();
  274. $result = $profiler->getCli();
  275. $this->assertNotNull($result);
  276. $this->assertContains('Queries: 0', Profiler::getInstance()->getCli());
  277. }
  278. public function tearDown()
  279. {
  280. // if (!is_null(Config::get('DEBUG'))) {
  281. // $debug = Config::get('DEBUG') ? 'TRUE' : 'FALSE';
  282. // echo PHP_EOL . __CLASS__ . ' ' . $debug . PHP_EOL;
  283. // } else {
  284. // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
  285. // }
  286. unset_new_overload();
  287. }
  288. protected function newCallback($className)
  289. {
  290. switch ($className) {
  291. case 'CommandProfiler':
  292. return 'CommandProfilerMock';
  293. default:
  294. return $className;
  295. }
  296. }
  297. }
  298. //
  299. //
  300. //class CommandProfilerMock
  301. //{
  302. //
  303. // public function __construct($type, $command)
  304. // {
  305. // }
  306. //
  307. // public function getCommand()
  308. // {
  309. // return 'command';
  310. // }
  311. //
  312. // public function getType()
  313. // {
  314. // return 'type';
  315. // }
  316. //
  317. // public function getElapsed()
  318. // {
  319. // return 10;
  320. // }
  321. //}