Anton Grebnev
13 years ago
2 changed files with 232 additions and 0 deletions
@ -0,0 +1,42 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage UnitTests |
|||
* @since 2011-10-28 |
|||
* |
|||
* Unit tests for CommandProfiler class |
|||
*/ |
|||
|
|||
require_once dirname(__FILE__) . '/../../../util/profiler/CommandProfiler.php'; |
|||
|
|||
class CommandProfilerTest extends PHPUnit_Framework_TestCase |
|||
{ |
|||
|
|||
private $profiler; |
|||
|
|||
public function setUp() |
|||
{ |
|||
$this->profiler = new CommandProfiler('method', 'exec'); |
|||
$this->profiler->end(); |
|||
} |
|||
|
|||
public function testGetEllapsed() |
|||
{ |
|||
$this->assertGreaterThan(0, $this->profiler->getElapsed()); |
|||
} |
|||
|
|||
public function testGetType() |
|||
{ |
|||
$this->assertEquals('method', $this->profiler->getType()); |
|||
$this->assertNotEquals('argument', $this->profiler->getType()); |
|||
} |
|||
|
|||
public function testGetCommand() |
|||
{ |
|||
$this->assertEquals('exec', $this->profiler->getCommand()); |
|||
$this->assertNotEquals('grep', $this->profiler->getCommand()); |
|||
} |
|||
} |
@ -0,0 +1,190 @@ |
|||
<?php |
|||
|
|||
/* |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage UnitTests |
|||
* @since 2011-10-28 |
|||
* |
|||
* Unit tests for CommandProfiler class |
|||
*/ |
|||
|
|||
require_once dirname(__FILE__) . '/../../../exception/GeneralException.php'; |
|||
require_once dirname(__FILE__) . '/../../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php'; |
|||
require_once dirname(__FILE__) . '/../../../util/profiler/Profiler.php'; |
|||
|
|||
/** |
|||
* @TODO: Profiler->getJson() hardly depends on FB library |
|||
*/ |
|||
class ProfilerTest extends PHPUnit_Framework_TestCase |
|||
{ |
|||
|
|||
public function run(PHPUnit_Framework_TestResult $result = NULL) |
|||
{ |
|||
$this->setPreserveGlobalState(false); |
|||
return parent::run($result); |
|||
} |
|||
|
|||
public function setUp() |
|||
{ |
|||
set_new_overload(array($this, 'newCallback')); |
|||
} |
|||
|
|||
/** |
|||
* @expectedException GeneralException |
|||
* @expectedExceptionMessage Need to turn on DEBUG before use. |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testGetInstaceNoDebug() |
|||
{ |
|||
if (!defined('DEBUG')) { |
|||
define('DEBUG', false); |
|||
} |
|||
Profiler::getInstance(); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testGetInstance() |
|||
{ |
|||
if (!defined('DEBUG')) { |
|||
define('DEBUG', true); |
|||
} |
|||
$profiler = Profiler::getInstance(); |
|||
$this->assertInstanceOf('Profiler', $profiler); |
|||
$this->assertSame($profiler, Profiler::getInstance()); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testProfilerCommand() |
|||
{ |
|||
if (!defined('DEBUG')) { |
|||
define('DEBUG', true); |
|||
} |
|||
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); |
|||
$profiler = Profiler::getInstance(); |
|||
$cmdProfiler = $profiler->profilerCommand('command', 'type'); |
|||
$this->assertInstanceOf('CommandProfiler', $cmdProfiler); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testStartEndNoCommandProfiler() |
|||
{ |
|||
if (!defined('DEBUG')) { |
|||
define('DEBUG', true); |
|||
} |
|||
$profiler = Profiler::getInstance(); |
|||
$profiler->start(); |
|||
$result = $profiler->end('<body></body>'); |
|||
$this->assertNotEquals('<body></body>', $result); |
|||
$this->assertStringStartsWith('<body>', $result); |
|||
$this->assertStringEndsWith(']<br/></div></body>', $result); |
|||
$this->assertContains('<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">', $result); |
|||
|
|||
|
|||
$this->assertEquals('html', $profiler->end('html')); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testStartEndWithCommandProfiler() |
|||
{ |
|||
if (!defined('DEBUG')) { |
|||
define('DEBUG', true); |
|||
} |
|||
|
|||
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); |
|||
$profiler = Profiler::getInstance(); |
|||
$cmdProfiler = $profiler->profilerCommand('command', 'type'); |
|||
|
|||
$profiler->start(); |
|||
$result = $profiler->end('<body></body>'); |
|||
$this->assertNotEquals('<body></body>', $result); |
|||
$this->assertStringEndsNotWith(']<br/></div></body>', $result); |
|||
$this->assertContains('Queries: 1 [0 ms]<br/>() [0ms] <br/>', $result); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testGetJSON() |
|||
{ |
|||
if (!defined('DEBUG')) { |
|||
define('DEBUG', true); |
|||
} |
|||
|
|||
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); |
|||
$profiler = Profiler::getInstance(); |
|||
$cmdProfiler = $profiler->profilerCommand('command', 'type'); |
|||
$this->assertNull($profiler->getJson()); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testGetCLI() |
|||
{ |
|||
if (!defined('DEBUG')) { |
|||
define('DEBUG', true); |
|||
} |
|||
|
|||
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); |
|||
$profiler = Profiler::getInstance(); |
|||
$cmdProfiler = $profiler->profilerCommand('command', 'type'); |
|||
$result = $profiler->getCli(); |
|||
echo $result; |
|||
$this->assertNotNull($result); |
|||
} |
|||
|
|||
public function tearDown() |
|||
{ |
|||
// if (defined('DEBUG')) {
|
|||
// $debug = DEBUG ? 'TRUE' : 'FALSE';
|
|||
// echo PHP_EOL . __CLASS__ . ' ' . $debug . PHP_EOL;
|
|||
// } else {
|
|||
// echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
|
|||
// }
|
|||
unset_new_overload(); |
|||
} |
|||
|
|||
protected function newCallback($className) |
|||
{ |
|||
switch ($className) { |
|||
case 'CommandProfiler': |
|||
return 'CommandProfilerMock'; |
|||
default: |
|||
return $className; |
|||
} |
|||
} |
|||
} |
|||
//
|
|||
//
|
|||
//class CommandProfilerMock
|
|||
//{
|
|||
//
|
|||
// public function __construct($type, $command)
|
|||
// {
|
|||
// }
|
|||
//
|
|||
// public function getCommand()
|
|||
// {
|
|||
// return 'command';
|
|||
// }
|
|||
//
|
|||
// public function getType()
|
|||
// {
|
|||
// return 'type';
|
|||
// }
|
|||
//
|
|||
// public function getElapsed()
|
|||
// {
|
|||
// return 10;
|
|||
// }
|
|||
//}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue