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.

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