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.

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