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.

175 lines
5.0 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('DEBUG', false);
  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. Config::set('DEBUG', 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('DEBUG', 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('DEBUG', true);
  66. $profiler = Profiler::getInstance();
  67. $profiler->start();
  68. $result = $profiler->end('<body></body>');
  69. $this->assertNotEquals('<body></body>', $result);
  70. $this->assertStringStartsWith('<body>', $result);
  71. $this->assertStringEndsWith(']<br/></div></body>', $result);
  72. $this->assertContains('<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">', $result);
  73. $this->assertSame('html', $profiler->end('html'));
  74. }
  75. /**
  76. * @runInSeparateProcess
  77. */
  78. public function testStartEndWithCommandProfiler()
  79. {
  80. Config::set('DEBUG', true);
  81. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  82. $profiler = Profiler::getInstance();
  83. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  84. $profiler->start();
  85. $result = $profiler->end('<body></body>');
  86. $this->assertNotEquals('<body></body>', $result);
  87. $this->assertStringEndsNotWith(']<br/></div></body>', $result);
  88. $this->assertContains('Queries: 1 [0 ms]<br/>() [0ms] <br/>', $result);
  89. }
  90. /**
  91. * @runInSeparateProcess
  92. */
  93. public function testGetJSON()
  94. {
  95. Config::set('DEBUG', true);
  96. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  97. $profiler = Profiler::getInstance();
  98. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  99. $this->assertNull($profiler->getJson());
  100. }
  101. /**
  102. * @runInSeparateProcess
  103. */
  104. public function testGetCLI()
  105. {
  106. Config::set('DEBUG', true);
  107. $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
  108. $profiler = Profiler::getInstance();
  109. $cmdProfiler = $profiler->profilerCommand('command', 'type');
  110. $result = $profiler->getCli();
  111. $this->assertNotNull($result);
  112. }
  113. public function tearDown()
  114. {
  115. // if (defined('DEBUG')) {
  116. // $debug = DEBUG ? 'TRUE' : 'FALSE';
  117. // echo PHP_EOL . __CLASS__ . ' ' . $debug . PHP_EOL;
  118. // } else {
  119. // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
  120. // }
  121. unset_new_overload();
  122. }
  123. protected function newCallback($className)
  124. {
  125. switch ($className) {
  126. case 'CommandProfiler':
  127. return 'CommandProfilerMock';
  128. default:
  129. return $className;
  130. }
  131. }
  132. }
  133. //
  134. //
  135. //class CommandProfilerMock
  136. //{
  137. //
  138. // public function __construct($type, $command)
  139. // {
  140. // }
  141. //
  142. // public function getCommand()
  143. // {
  144. // return 'command';
  145. // }
  146. //
  147. // public function getType()
  148. // {
  149. // return 'type';
  150. // }
  151. //
  152. // public function getElapsed()
  153. // {
  154. // return 10;
  155. // }
  156. //}