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.

357 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. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  86. $profiler->start();
  87. $result = $profiler->end('<body></body>');
  88. $this->assertNotEquals('<body></body>', $result);
  89. $this->assertStringEndsNotWith(']<br/></div></body>', $result);
  90. $this->assertContains('Queries: 1 [0 ms]<br/>() [0ms] <br/>', $result);
  91. }
  92. /**
  93. * @runInSeparateProcess
  94. */
  95. public function testStartEndWithCommandProfilerWithNonDetails()
  96. {
  97. Config::set('PROFILER', true);
  98. Config::set('PROFILER_DETAILS', false);
  99. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  100. $profiler = Profiler::getInstance();
  101. $profiler->start();
  102. $result = $profiler->end('<body></body>');
  103. $this->assertNotEquals('<body></body>', $result);
  104. $this->assertContains('Queries not counted.', $result);
  105. }
  106. /**
  107. * @runInSeparateProcess
  108. */
  109. public function testStartEndWithCommandProfilerWithNonDetailsButExistingQueries()
  110. {
  111. Config::set('PROFILER', true);
  112. Config::set('PROFILER_DETAILS', false);
  113. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  114. $profiler = Profiler::getInstance();
  115. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  116. $profiler->start();
  117. $result = $profiler->end('<body></body>');
  118. $this->assertNotEquals('<body></body>', $result);
  119. $this->assertStringEndsNotWith(']<br/></div></body>', $result);
  120. $this->assertContains('Queries: 1', $result);
  121. }
  122. /**
  123. * @runInSeparateProcess
  124. */
  125. public function testStartEndWithCommandProfilerWithDetailsButNonQueries()
  126. {
  127. Config::set('PROFILER', true);
  128. Config::set('PROFILER_DETAILS', true);
  129. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  130. $profiler = Profiler::getInstance();
  131. $profiler->start();
  132. $result = $profiler->end('<body></body>');
  133. $this->assertNotEquals('<body></body>', $result);
  134. $this->assertStringEndsWith(']<br/></div></body>', $result);
  135. $this->assertContains('Queries: 0', $result);
  136. }
  137. /**
  138. * @runInSeparateProcess
  139. */
  140. public function testGetJSON()
  141. {
  142. Config::set('PROFILER', true);
  143. Config::set('PROFILER_DETAILS', true);
  144. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  145. $profiler = Profiler::getInstance();
  146. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  147. $fire_php_mock = $this->getMockBuilder('FirePHP')
  148. ->disableOriginalConstructor()
  149. ->setMethods(array('__construct', 'fb'))
  150. ->getMock();
  151. $fire_php_mock->expects($this->exactly(2))
  152. ->method('fb')
  153. ->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // Expected "Queries: 1"
  154. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  155. $reflection_property->setAccessible(true);
  156. $reflection_property->setValue($fire_php_mock);
  157. $this->assertNull($profiler->getJson());
  158. }
  159. /**
  160. * @runInSeparateProcess
  161. */
  162. public function testGetJSONWithNonDetails()
  163. {
  164. Config::set('PROFILER', true);
  165. Config::set('PROFILER_DETAILS', false);
  166. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  167. $profiler = Profiler::getInstance();
  168. $fire_php_mock = $this->getMockBuilder('FirePHP')
  169. ->disableOriginalConstructor()
  170. ->setMethods(array('__construct', 'fb'))
  171. ->getMock();
  172. $fire_php_mock->expects($this->exactly(2))
  173. ->method('fb')
  174. ->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"
  175. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  176. $reflection_property->setAccessible(true);
  177. $reflection_property->setValue($fire_php_mock);
  178. $this->assertNull($profiler->getJson());
  179. }
  180. /**
  181. * @runInSeparateProcess
  182. */
  183. public function testGetJSONWithNonDetailsButExistingQueries()
  184. {
  185. Config::set('PROFILER', true);
  186. Config::set('PROFILER_DETAILS', false);
  187. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  188. $profiler = Profiler::getInstance();
  189. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  190. $fire_php_mock = $this->getMockBuilder('FirePHP')
  191. ->disableOriginalConstructor()
  192. ->setMethods(array('__construct', 'fb'))
  193. ->getMock();
  194. $fire_php_mock->expects($this->exactly(2))
  195. ->method('fb')
  196. ->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"
  197. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  198. $reflection_property->setAccessible(true);
  199. $reflection_property->setValue($fire_php_mock);
  200. $this->assertNull($profiler->getJson());
  201. }
  202. /**
  203. * @runInSeparateProcess
  204. */
  205. public function testGetJSONWithDetailsButNonQueries()
  206. {
  207. Config::set('PROFILER', true);
  208. Config::set('PROFILER_DETAILS', true);
  209. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  210. $profiler = Profiler::getInstance();
  211. $fire_php_mock = $this->getMockBuilder('FirePHP')
  212. ->disableOriginalConstructor()
  213. ->setMethods(array('__construct', 'fb'))
  214. ->getMock();
  215. $fire_php_mock->expects($this->exactly(2))
  216. ->method('fb')
  217. ->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"
  218. $reflection_property = new ReflectionProperty('FirePHP', 'instance');
  219. $reflection_property->setAccessible(true);
  220. $reflection_property->setValue($fire_php_mock);
  221. $this->assertNull($profiler->getJson());
  222. }
  223. /**
  224. * @runInSeparateProcess
  225. */
  226. public function testGetCLI()
  227. {
  228. Config::set('PROFILER', true);
  229. Config::set('PROFILER_DETAILS', true);
  230. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  231. $profiler = Profiler::getInstance();
  232. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  233. $result = $profiler->getCli();
  234. $this->assertNotNull($result);
  235. $this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
  236. }
  237. /**
  238. * @runInSeparateProcess
  239. */
  240. public function testGetCLIWithNonDetails()
  241. {
  242. Config::set('PROFILER', true);
  243. Config::set('PROFILER_DETAILS', false);
  244. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  245. $profiler = Profiler::getInstance();
  246. $result = $profiler->getCli();
  247. $this->assertNotNull($result);
  248. $this->assertContains('Queries not counted', Profiler::getInstance()->getCli());
  249. }
  250. /**
  251. * @runInSeparateProcess
  252. */
  253. public function testGetCLIWithNonDetailsButExistingQueries()
  254. {
  255. Config::set('PROFILER', true);
  256. Config::set('PROFILER_DETAILS', false);
  257. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  258. $profiler = Profiler::getInstance();
  259. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  260. $result = $profiler->getCli();
  261. $this->assertNotNull($result);
  262. $this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
  263. }
  264. /**
  265. * @runInSeparateProcess
  266. */
  267. public function testGetCLIWithDetailsButNonQueries()
  268. {
  269. Config::set('PROFILER', true);
  270. Config::set('PROFILER_DETAILS', true);
  271. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  272. $profiler = Profiler::getInstance();
  273. $result = $profiler->getCli();
  274. $this->assertNotNull($result);
  275. $this->assertContains('Queries: 0', Profiler::getInstance()->getCli());
  276. }
  277. public function tearDown()
  278. {
  279. // if (!is_null(Config::get('DEBUG'))) {
  280. // $debug = Config::get('DEBUG') ? 'TRUE' : 'FALSE';
  281. // echo PHP_EOL . __CLASS__ . ' ' . $debug . PHP_EOL;
  282. // } else {
  283. // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
  284. // }
  285. unset_new_overload();
  286. }
  287. protected function newCallback($className)
  288. {
  289. switch ($className) {
  290. case 'CommandProfiler':
  291. return 'CommandProfilerMock';
  292. default:
  293. return $className;
  294. }
  295. }
  296. }
  297. //
  298. //
  299. //class CommandProfilerMock
  300. //{
  301. //
  302. // public function __construct($type, $command)
  303. // {
  304. // }
  305. //
  306. // public function getCommand()
  307. // {
  308. // return 'command';
  309. // }
  310. //
  311. // public function getType()
  312. // {
  313. // return 'type';
  314. // }
  315. //
  316. // public function getElapsed()
  317. // {
  318. // return 10;
  319. // }
  320. //}