* @link http://netmonsters.ru * @package Majestic * @subpackage UnitTests * @since 2011-10-28 * * Unit tests for CommandProfiler class */ require_once dirname(__FILE__) . '/../../../Registry.php'; require_once dirname(__FILE__) . '/../../../Config.php'; 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')); } /** * @runInSeparateProcess */ public function testGetInstaceNoDebug() { Config::set('DEBUG', 0); $this->setExpectedException('GeneralException', 'Need to turn on DEBUG before use.'); Profiler::getInstance(); } /** * @runInSeparateProcess */ public function testGetInstance() { Config::set('DEBUG', 1); $profiler = Profiler::getInstance(); $this->assertInstanceOf('Profiler', $profiler); $this->assertSame($profiler, Profiler::getInstance()); } /** * @runInSeparateProcess */ public function testProfilerCommand() { Config::set('DEBUG', 1); $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() { Config::set('DEBUG', 1); $profiler = Profiler::getInstance(); $profiler->start(); $result = $profiler->end(''); $this->assertNotEquals('', $result); $this->assertStringStartsWith('', $result); $this->assertStringEndsWith(']
', $result); $this->assertContains('
', $result); $this->assertSame('html', $profiler->end('html')); } /** * @runInSeparateProcess */ public function testStartEndWithCommandProfiler() { Config::set('DEBUG', 1); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); $cmdProfiler = $profiler->profilerCommand('command', 'type'); $profiler->start(); $result = $profiler->end(''); $this->assertNotEquals('', $result); $this->assertStringEndsNotWith(']
', $result); $this->assertContains('Queries: 1 [0 ms]
() [0ms]
', $result); } /** * @runInSeparateProcess */ public function testGetJSON() { Config::set('DEBUG', 1); $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() { Config::set('DEBUG', 1); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); $cmdProfiler = $profiler->profilerCommand('command', 'type'); $result = $profiler->getCli(); $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; // } //}