Skip to content

Commit f1fc5a9

Browse files
committed
Restore tests for Debugger::addFormat()
1 parent ea19e9c commit f1fc5a9

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

tests/TestCase/Error/DebuggerTest.php

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,83 @@ public function testOutputAsException()
146146
Debugger::outputAs('Invalid junk');
147147
}
148148

149+
/**
150+
* Tests that changes in output formats using Debugger::output() change the templates used.
151+
*
152+
* @return void
153+
*/
154+
public function testAddFormat()
155+
{
156+
Debugger::addFormat('js', array(
157+
'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
158+
'&line={:line}">{:path}</a>, line {:line}'
159+
));
160+
Debugger::outputAs('js');
161+
162+
$result = Debugger::trace();
163+
$this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
164+
165+
Debugger::addFormat('xml', array(
166+
'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
167+
'{:description}</error>',
168+
));
169+
Debugger::outputAs('xml');
170+
171+
ob_start();
172+
$debugger = Debugger::getInstance();
173+
$debugger->outputError([
174+
'level' => E_NOTICE,
175+
'code' => E_NOTICE,
176+
'file' => __FILE__,
177+
'line' => __LINE__,
178+
'description' => 'Undefined variable: foo',
179+
]);
180+
$result = ob_get_clean();
181+
182+
$expected = array(
183+
'<error',
184+
'<code', '8', '/code',
185+
'<file', 'preg:/[^<]+/', '/file',
186+
'<line', '' . ((int)__LINE__ - 9), '/line',
187+
'preg:/Undefined variable:\s+foo/',
188+
'/error'
189+
);
190+
$this->assertHtml($expected, $result, true);
191+
}
192+
193+
/**
194+
* Test adding a format that is handled by a callback.
195+
*
196+
* @return void
197+
*/
198+
public function testAddFormatCallback()
199+
{
200+
Debugger::addFormat('callback', array('callback' => array($this, 'customFormat')));
201+
Debugger::outputAs('callback');
202+
203+
ob_start();
204+
$debugger = Debugger::getInstance();
205+
$debugger->outputError([
206+
'error' => 'Notice',
207+
'code' => E_NOTICE,
208+
'level' => E_NOTICE,
209+
'description' => 'Undefined variable $foo',
210+
'file' => __FILE__,
211+
'line' => __LINE__,
212+
]);
213+
$result = ob_get_clean();
214+
$this->assertContains('Notice: I eated an error', $result);
215+
$this->assertContains('DebuggerTest.php', $result);
216+
}
217+
149218
/**
150219
* Test method for testing addFormat with callbacks.
151220
*
152221
* @return void
153222
*/
154223
public function customFormat($error, $strings)
155224
{
156-
return $error['error'] . ': I eated an error ' . $error['file'];
225+
echo $error['error'] . ': I eated an error ' . $error['file'];
157226
}
158227

159228
/**

0 commit comments

Comments
 (0)