| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- <?php
- /**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP Project
- * @since 1.2.0
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Error;
- use Cake\Controller\Controller;
- use Cake\Core\Configure;
- use Cake\Error\Debugger;
- use Cake\Log\Log;
- use Cake\TestSuite\TestCase;
- use Cake\View\View;
- /**
- * DebuggerTestCaseDebugger class
- *
- */
- class DebuggerTestCaseDebugger extends Debugger
- {
- }
- class DebuggableThing
- {
- public function __debugInfo()
- {
- return ['foo' => 'bar', 'inner' => new self()];
- }
- }
- /**
- * DebuggerTest class
- *
- * !!! Be careful with changing code below as it may
- * !!! change line numbers which are used in the tests
- *
- */
- class DebuggerTest extends TestCase
- {
- protected $_restoreError = false;
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp()
- {
- parent::setUp();
- Configure::write('debug', true);
- Log::drop('stderr');
- Log::drop('stdout');
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown()
- {
- parent::tearDown();
- if ($this->_restoreError) {
- restore_error_handler();
- }
- }
- /**
- * testDocRef method
- *
- * @return void
- */
- public function testDocRef()
- {
- $this->skipIf(
- defined('HHVM_VERSION'),
- 'HHVM does not output doc references'
- );
- ini_set('docref_root', '');
- $this->assertEquals(ini_get('docref_root'), '');
- new Debugger();
- $this->assertEquals(ini_get('docref_root'), 'http://php.net/');
- }
- /**
- * test Excerpt writing
- *
- * @return void
- */
- public function testExcerpt()
- {
- $result = Debugger::excerpt(__FILE__, __LINE__ - 1, 2);
- $this->assertTrue(is_array($result));
- $this->assertCount(5, $result);
- $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
- $result = Debugger::excerpt(__FILE__, 2, 2);
- $this->assertTrue(is_array($result));
- $this->assertCount(4, $result);
- $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not highlight php code');
- $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?<\?php/';
- $this->assertRegExp($pattern, $result[0]);
- $result = Debugger::excerpt(__FILE__, 11, 2);
- $this->assertCount(5, $result);
- $pattern = '/<span style\="color\: \#\d{6}">\*<\/span>/';
- $this->assertRegExp($pattern, $result[0]);
- $return = Debugger::excerpt('[internal]', 2, 2);
- $this->assertTrue(empty($return));
- $result = Debugger::excerpt(__FILE__, __LINE__, 5);
- $this->assertCount(11, $result);
- $this->assertContains('Debugger', $result[5]);
- $this->assertContains('excerpt', $result[5]);
- $this->assertContains('__FILE__', $result[5]);
- }
- /**
- * Test that outputAs works.
- *
- * @return void
- */
- public function testOutputAs()
- {
- Debugger::outputAs('html');
- $this->assertEquals('html', Debugger::outputAs());
- }
- /**
- * Test that choosing a non-existent format causes an exception
- *
- * @expectedException \InvalidArgumentException
- * @return void
- */
- public function testOutputAsException()
- {
- Debugger::outputAs('Invalid junk');
- }
- /**
- * Tests that changes in output formats using Debugger::output() change the templates used.
- *
- * @return void
- */
- public function testAddFormat()
- {
- Debugger::addFormat('js', [
- 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
- '&line={:line}">{:path}</a>, line {:line}'
- ]);
- Debugger::outputAs('js');
- $result = Debugger::trace();
- $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
- Debugger::addFormat('xml', [
- 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
- '{:description}</error>',
- ]);
- Debugger::outputAs('xml');
- ob_start();
- $debugger = Debugger::getInstance();
- $debugger->outputError([
- 'level' => E_NOTICE,
- 'code' => E_NOTICE,
- 'file' => __FILE__,
- 'line' => __LINE__,
- 'description' => 'Undefined variable: foo',
- ]);
- $result = ob_get_clean();
- $expected = [
- '<error',
- '<code', '8', '/code',
- '<file', 'preg:/[^<]+/', '/file',
- '<line', '' . ((int)__LINE__ - 9), '/line',
- 'preg:/Undefined variable:\s+foo/',
- '/error'
- ];
- $this->assertHtml($expected, $result, true);
- }
- /**
- * Test adding a format that is handled by a callback.
- *
- * @return void
- */
- public function testAddFormatCallback()
- {
- Debugger::addFormat('callback', ['callback' => [$this, 'customFormat']]);
- Debugger::outputAs('callback');
- ob_start();
- $debugger = Debugger::getInstance();
- $debugger->outputError([
- 'error' => 'Notice',
- 'code' => E_NOTICE,
- 'level' => E_NOTICE,
- 'description' => 'Undefined variable $foo',
- 'file' => __FILE__,
- 'line' => __LINE__,
- ]);
- $result = ob_get_clean();
- $this->assertContains('Notice: I eated an error', $result);
- $this->assertContains('DebuggerTest.php', $result);
- }
- /**
- * Test method for testing addFormat with callbacks.
- *
- * @return void
- */
- public function customFormat($error, $strings)
- {
- echo $error['error'] . ': I eated an error ' . $error['file'];
- }
- /**
- * testTrimPath method
- *
- * @return void
- */
- public function testTrimPath()
- {
- $this->assertEquals('APP/', Debugger::trimPath(APP));
- $this->assertEquals('CORE' . DS . 'src' . DS, Debugger::trimPath(CAKE));
- $this->assertEquals('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
- }
- /**
- * testExportVar method
- *
- * @return void
- */
- public function testExportVar()
- {
- $Controller = new Controller();
- $Controller->helpers = ['Html', 'Form'];
- $View = $Controller->createView();
- $View->int = 2;
- $View->float = 1.333;
- $result = Debugger::exportVar($View);
- $expected = <<<TEXT
- object(Cake\View\View) {
- Blocks => object(Cake\View\ViewBlock) {}
- plugin => null
- name => ''
- passedArgs => []
- helpers => [
- (int) 0 => 'Html',
- (int) 1 => 'Form'
- ]
- templatePath => null
- template => null
- layout => 'default'
- layoutPath => null
- autoLayout => true
- subDir => null
- theme => null
- hasRendered => false
- uuids => []
- request => object(Cake\Network\Request) {}
- response => object(Cake\Network\Response) {}
- elementCache => 'default'
- viewClass => null
- viewVars => []
- Html => object(Cake\View\Helper\HtmlHelper) {}
- Form => object(Cake\View\Helper\FormHelper) {}
- int => (int) 2
- float => (float) 1.333
- [protected] _helpers => object(Cake\View\HelperRegistry) {}
- [protected] _ext => '.ctp'
- [protected] _passedVars => [
- (int) 0 => 'viewVars',
- (int) 1 => 'autoLayout',
- (int) 2 => 'helpers',
- (int) 3 => 'template',
- (int) 4 => 'layout',
- (int) 5 => 'name',
- (int) 6 => 'theme',
- (int) 7 => 'layoutPath',
- (int) 8 => 'templatePath',
- (int) 9 => 'plugin',
- (int) 10 => 'passedArgs'
- ]
- [protected] _paths => []
- [protected] _pathsForPlugin => []
- [protected] _parents => []
- [protected] _current => null
- [protected] _currentType => ''
- [protected] _stack => []
- [protected] _eventManager => object(Cake\Event\EventManager) {}
- [protected] _eventClass => '\Cake\Event\Event'
- [protected] _viewBuilder => null
- }
- TEXT;
- $this->assertTextEquals($expected, $result);
- $data = [
- 1 => 'Index one',
- 5 => 'Index five'
- ];
- $result = Debugger::exportVar($data);
- $expected = <<<TEXT
- [
- (int) 1 => 'Index one',
- (int) 5 => 'Index five'
- ]
- TEXT;
- $this->assertTextEquals($expected, $result);
- $data = [
- 'key' => [
- 'value'
- ]
- ];
- $result = Debugger::exportVar($data, 1);
- $expected = <<<TEXT
- [
- 'key' => [
- [maximum depth reached]
- ]
- ]
- TEXT;
- $this->assertTextEquals($expected, $result);
- $data = false;
- $result = Debugger::exportVar($data);
- $expected = <<<TEXT
- false
- TEXT;
- $this->assertTextEquals($expected, $result);
- $file = fopen('php://output', 'w');
- fclose($file);
- $result = Debugger::exportVar($file);
- $this->assertTextEquals('unknown', $result);
- }
- /**
- * Test exporting various kinds of false.
- *
- * @return void
- */
- public function testExportVarZero()
- {
- $data = [
- 'nothing' => '',
- 'null' => null,
- 'false' => false,
- 'szero' => '0',
- 'zero' => 0
- ];
- $result = Debugger::exportVar($data);
- $expected = <<<TEXT
- [
- 'nothing' => '',
- 'null' => null,
- 'false' => false,
- 'szero' => '0',
- 'zero' => (int) 0
- ]
- TEXT;
- $this->assertTextEquals($expected, $result);
- }
- /**
- * testLog method
- *
- * @return void
- */
- public function testLog()
- {
- $mock = $this->getMock('Cake\Log\Engine\BaseLog', ['log']);
- Log::config('test', ['engine' => $mock]);
- $mock->expects($this->at(0))
- ->method('log')
- ->with('debug', $this->logicalAnd(
- $this->stringContains('DebuggerTest::testLog'),
- $this->stringContains('cool')
- ));
- $mock->expects($this->at(1))
- ->method('log')
- ->with('debug', $this->logicalAnd(
- $this->stringContains('DebuggerTest::testLog'),
- $this->stringContains('[main]'),
- $this->stringContains("'whatever',"),
- $this->stringContains("'here'")
- ));
- Debugger::log('cool');
- Debugger::log(['whatever', 'here']);
- Log::drop('test');
- }
- /**
- * test log() depth
- *
- * @return void
- */
- public function testLogDepth()
- {
- $mock = $this->getMock('Cake\Log\Engine\BaseLog', ['log']);
- Log::config('test', ['engine' => $mock]);
- $mock->expects($this->at(0))
- ->method('log')
- ->with('debug', $this->logicalAnd(
- $this->stringContains('DebuggerTest::testLog'),
- $this->stringContains('test'),
- $this->logicalNot($this->stringContains('val'))
- ));
- $val = [
- 'test' => ['key' => 'val']
- ];
- Debugger::log($val, 'debug', 0);
- }
- /**
- * testDump method
- *
- * @return void
- */
- public function testDump()
- {
- $var = ['People' => [
- [
- 'name' => 'joeseph',
- 'coat' => 'technicolor',
- 'hair_color' => 'brown'
- ],
- [
- 'name' => 'Shaft',
- 'coat' => 'black',
- 'hair' => 'black'
- ]
- ]];
- ob_start();
- Debugger::dump($var);
- $result = ob_get_clean();
- $open = "\n";
- $close = "\n\n";
- $expected = <<<TEXT
- {$open}[
- 'People' => [
- (int) 0 => [
- 'name' => 'joeseph',
- 'coat' => 'technicolor',
- 'hair_color' => 'brown'
- ],
- (int) 1 => [
- 'name' => 'Shaft',
- 'coat' => 'black',
- 'hair' => 'black'
- ]
- ]
- ]{$close}
- TEXT;
- $this->assertTextEquals($expected, $result);
- ob_start();
- Debugger::dump($var, 1);
- $result = ob_get_clean();
- $expected = <<<TEXT
- {$open}[
- 'People' => [
- [maximum depth reached]
- ]
- ]{$close}
- TEXT;
- $this->assertTextEquals($expected, $result);
- }
- /**
- * test getInstance.
- *
- * @return void
- */
- public function testGetInstance()
- {
- $result = Debugger::getInstance();
- $this->assertInstanceOf('Cake\Error\Debugger', $result);
- $result = Debugger::getInstance(__NAMESPACE__ . '\DebuggerTestCaseDebugger');
- $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
- $result = Debugger::getInstance();
- $this->assertInstanceOf(__NAMESPACE__ . '\DebuggerTestCaseDebugger', $result);
- $result = Debugger::getInstance('Cake\Error\Debugger');
- $this->assertInstanceOf('Cake\Error\Debugger', $result);
- }
- /**
- * Test that exportVar() doesn't loop through recursive structures.
- *
- * @return void
- */
- public function testExportVarRecursion()
- {
- $output = Debugger::exportVar($GLOBALS);
- $this->assertContains("'GLOBALS' => [recursion]", $output);
- }
- /**
- * test trace exclude
- *
- * @return void
- */
- public function testTraceExclude()
- {
- $result = Debugger::trace();
- $this->assertRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
- $result = Debugger::trace([
- 'exclude' => ['Cake\Test\TestCase\Error\DebuggerTest::testTraceExclude']
- ]);
- $this->assertNotRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
- }
- /**
- * Tests that __debugInfo is used when available
- *
- * @return void
- */
- public function testDebugInfo()
- {
- $object = new DebuggableThing();
- $result = Debugger::exportVar($object, 2);
- $expected = <<<eos
- object(Cake\Test\TestCase\Error\DebuggableThing) {
- 'foo' => 'bar',
- 'inner' => object(Cake\Test\TestCase\Error\DebuggableThing) {}
- }
- eos;
- $this->assertEquals($expected, $result);
- }
- }
|