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.

187 lines
5.1 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__) . '/../../../exception/GeneralException.php';
  12. require_once dirname(__FILE__) . '/../../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php';
  13. require_once dirname(__FILE__) . '/../../../util/profiler/Profiler.php';
  14. /**
  15. * @TODO: Profiler->getJson() hardly depends on FB library
  16. */
  17. class ProfilerTest extends PHPUnit_Framework_TestCase
  18. {
  19. public function run(PHPUnit_Framework_TestResult $result = NULL)
  20. {
  21. $this->setPreserveGlobalState(false);
  22. return parent::run($result);
  23. }
  24. public function setUp()
  25. {
  26. set_new_overload(array($this, 'newCallback'));
  27. }
  28. /**
  29. * @runInSeparateProcess
  30. */
  31. public function testGetInstaceNoDebug()
  32. {
  33. if (!defined('DEBUG')) {
  34. define('DEBUG', false);
  35. }
  36. $this->setExpectedException('GeneralException', 'Need to turn on DEBUG before use.');
  37. Profiler::getInstance();
  38. }
  39. /**
  40. * @runInSeparateProcess
  41. */
  42. public function testGetInstance()
  43. {
  44. if (!defined('DEBUG')) {
  45. define('DEBUG', true);
  46. }
  47. $profiler = Profiler::getInstance();
  48. $this->assertInstanceOf('Profiler', $profiler);
  49. $this->assertSame($profiler, Profiler::getInstance());
  50. }
  51. /**
  52. * @runInSeparateProcess
  53. */
  54. public function testProfilerCommand()
  55. {
  56. if (!defined('DEBUG')) {
  57. define('DEBUG', true);
  58. }
  59. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  60. $profiler = Profiler::getInstance();
  61. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  62. $this->assertInstanceOf('CommandProfiler', $cmdProfiler);
  63. }
  64. /**
  65. * @runInSeparateProcess
  66. */
  67. public function testStartEndNoCommandProfiler()
  68. {
  69. if (!defined('DEBUG')) {
  70. define('DEBUG', true);
  71. }
  72. $profiler = Profiler::getInstance();
  73. $profiler->start();
  74. $result = $profiler->end('<body></body>');
  75. $this->assertNotEquals('<body></body>', $result);
  76. $this->assertStringStartsWith('<body>', $result);
  77. $this->assertStringEndsWith(']<br/></div></body>', $result);
  78. $this->assertContains('<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">', $result);
  79. $this->assertSame('html', $profiler->end('html'));
  80. }
  81. /**
  82. * @runInSeparateProcess
  83. */
  84. public function testStartEndWithCommandProfiler()
  85. {
  86. if (!defined('DEBUG')) {
  87. define('DEBUG', true);
  88. }
  89. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  90. $profiler = Profiler::getInstance();
  91. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  92. $profiler->start();
  93. $result = $profiler->end('<body></body>');
  94. $this->assertNotEquals('<body></body>', $result);
  95. $this->assertStringEndsNotWith(']<br/></div></body>', $result);
  96. $this->assertContains('Queries: 1 [0 ms]<br/>() [0ms] <br/>', $result);
  97. }
  98. /**
  99. * @runInSeparateProcess
  100. */
  101. public function testGetJSON()
  102. {
  103. if (!defined('DEBUG')) {
  104. define('DEBUG', true);
  105. }
  106. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  107. $profiler = Profiler::getInstance();
  108. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  109. $this->assertNull($profiler->getJson());
  110. }
  111. /**
  112. * @runInSeparateProcess
  113. */
  114. public function testGetCLI()
  115. {
  116. if (!defined('DEBUG')) {
  117. define('DEBUG', true);
  118. }
  119. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  120. $profiler = Profiler::getInstance();
  121. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  122. $result = $profiler->getCli();
  123. $this->assertNotNull($result);
  124. }
  125. public function tearDown()
  126. {
  127. // if (defined('DEBUG')) {
  128. // $debug = DEBUG ? 'TRUE' : 'FALSE';
  129. // echo PHP_EOL . __CLASS__ . ' ' . $debug . PHP_EOL;
  130. // } else {
  131. // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
  132. // }
  133. unset_new_overload();
  134. }
  135. protected function newCallback($className)
  136. {
  137. switch ($className) {
  138. case 'CommandProfiler':
  139. return 'CommandProfilerMock';
  140. default:
  141. return $className;
  142. }
  143. }
  144. }
  145. //
  146. //
  147. //class CommandProfilerMock
  148. //{
  149. //
  150. // public function __construct($type, $command)
  151. // {
  152. // }
  153. //
  154. // public function getCommand()
  155. // {
  156. // return 'command';
  157. // }
  158. //
  159. // public function getType()
  160. // {
  161. // return 'type';
  162. // }
  163. //
  164. // public function getElapsed()
  165. // {
  166. // return 10;
  167. // }
  168. //}