Skip to content

Commit

Permalink
Merge pull request #297 from markomarkovic/73429b357f06cd76f35d81482d…
Browse files Browse the repository at this point in the history
…d789b1737fe2b9

Fixed inconsistent debug parameters and empty '(line )'
  • Loading branch information
markstory committed Nov 17, 2011
2 parents 8981f49 + 4e702b2 commit 2c5350b
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 17 deletions.
214 changes: 202 additions & 12 deletions lib/Cake/Test/Case/BasicsTest.php
Expand Up @@ -683,26 +683,216 @@ public function testDebug() {
ob_start();
debug('this-is-a-test');
$result = ob_get_clean();
$pattern = '/(.+?Test(\/|\\\)Case(\/|\\\)BasicsTest\.php|';
$pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
$pattern .= '.*line.*' . (__LINE__ - 4) . '.*this-is-a-test.*/s';
$this->assertRegExp($pattern, $result);
$expectedHtml = <<<EXPECTED
<div class="cake-debug-output">
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
<pre class="cake-debug">
this-is-a-test
</pre>
</div>
EXPECTED;
$expectedText = <<<EXPECTED
%s (line %d)
########## DEBUG ##########
this-is-a-test
###########################
EXPECTED;
if (php_sapi_name() == 'cli') {
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 19);
} else {
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 21);
}
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>');
$result = ob_get_clean();
$expectedHtml = <<<EXPECTED
<div class="cake-debug-output">
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
<pre class="cake-debug">
&lt;div&gt;this-is-a-test&lt;/div&gt;
</pre>
</div>
EXPECTED;
$expectedText = <<<EXPECTED
%s (line %d)
########## DEBUG ##########
<div>this-is-a-test</div>
###########################
EXPECTED;
if (php_sapi_name() == 'cli') {
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 19);
} else {
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 21);
}
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', true);
$result = ob_get_clean();
$pattern = '/(.+?Test(\/|\\\)Case(\/|\\\)BasicsTest\.php|';
$pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
$pattern .= '.*line.*' . (__LINE__ -4) . '.*&lt;div&gt;this-is-a-test&lt;\/div&gt;.*/s';
$this->assertRegExp($pattern, $result);
$expected = <<<EXPECTED
<div class="cake-debug-output">
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
<pre class="cake-debug">
&lt;div&gt;this-is-a-test&lt;/div&gt;
</pre>
</div>
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', true, true);
$result = ob_get_clean();
$expected = <<<EXPECTED
<div class="cake-debug-output">
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
<pre class="cake-debug">
&lt;div&gt;this-is-a-test&lt;/div&gt;
</pre>
</div>
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', true, false);
$result = ob_get_clean();
$expected = <<<EXPECTED
<div class="cake-debug-output">
<pre class="cake-debug">
&lt;div&gt;this-is-a-test&lt;/div&gt;
</pre>
</div>
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', null);
$result = ob_get_clean();
$expectedHtml = <<<EXPECTED
<div class="cake-debug-output">
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
<pre class="cake-debug">
&lt;div&gt;this-is-a-test&lt;/div&gt;
</pre>
</div>
EXPECTED;
$expectedText = <<<EXPECTED
%s (line %d)
########## DEBUG ##########
<div>this-is-a-test</div>
###########################
EXPECTED;
if (php_sapi_name() == 'cli') {
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 19);
} else {
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 21);
}
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', null);
$result = ob_get_clean();
$expectedHtml = <<<EXPECTED
<div class="cake-debug-output">
<span><strong>%s</strong> (line <strong>%d</strong>)</span>
<pre class="cake-debug">
&lt;div&gt;this-is-a-test&lt;/div&gt;
</pre>
</div>
EXPECTED;
$expectedText = <<<EXPECTED
%s (line %d)
########## DEBUG ##########
<div>this-is-a-test</div>
###########################
EXPECTED;
if (php_sapi_name() == 'cli') {
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 19);
} else {
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 21);
}
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', null, false);
$result = ob_get_clean();
$expectedHtml = <<<EXPECTED
<div class="cake-debug-output">
<pre class="cake-debug">
&lt;div&gt;this-is-a-test&lt;/div&gt;
</pre>
</div>
EXPECTED;
$expectedText = <<<EXPECTED
########## DEBUG ##########
<div>this-is-a-test</div>
###########################
EXPECTED;
if (php_sapi_name() == 'cli') {
$expected = sprintf($expectedText, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 19);
} else {
$expected = sprintf($expectedHtml, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 21);
}
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', false);
$result = ob_get_clean();
$pattern = '/(.+?Test(\/|\\\)Case(\/|\\\)BasicsTest\.php|';
$pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
$pattern .= '.*line.*' . (__LINE__ - 4) . '.*\<div\>this-is-a-test\<\/div\>.*/s';
$this->assertRegExp($pattern, $result);
$expected = <<<EXPECTED
%s (line %d)
########## DEBUG ##########
<div>this-is-a-test</div>
###########################
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', false, true);
$result = ob_get_clean();
$expected = <<<EXPECTED
%s (line %d)
########## DEBUG ##########
<div>this-is-a-test</div>
###########################
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
$this->assertEqual($expected, $result);

ob_start();
debug('<div>this-is-a-test</div>', false, false);
$result = ob_get_clean();
$expected = <<<EXPECTED
########## DEBUG ##########
<div>this-is-a-test</div>
###########################
EXPECTED;
$expected = sprintf($expected, substr(__FILE__, strlen(ROOT) + 1), __LINE__ - 10);
$this->assertEqual($expected, $result);
}

/**
Expand Down
18 changes: 13 additions & 5 deletions lib/Cake/basics.php
Expand Up @@ -76,39 +76,47 @@ function debug($var = false, $showHtml = null, $showFrom = true) {
if (Configure::read('debug') > 0) {
$file = '';
$line = '';
$lineInfo = '';
if ($showFrom) {
$calledFrom = debug_backtrace();
$file = substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1);
$line = $calledFrom[0]['line'];
}
$html = <<<HTML
<div class="cake-debug-output">
<span><strong>%s</strong> (line <strong>%s</strong>)</span>
%s
<pre class="cake-debug">
%s
</pre>
</div>
HTML;
$text = <<<TEXT
$text = <<<TEXT
%s (line %s)
%s
########## DEBUG ##########
%s
###########################
TEXT;
$template = $html;
if (php_sapi_name() == 'cli') {
if (php_sapi_name() == 'cli' || $showHtml === false) {
$template = $text;
if ($showFrom) {
$lineInfo = sprintf('%s (line %s)', $file, $line);
}
}
if ($showHtml === null && $template !== $text) {
$showHtml = true;
}
$var = print_r($var, true);
if ($showHtml) {
$template = $html;
$var = h($var);
if ($showFrom) {
$lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
}
}
printf($template, $file, $line, $var);
printf($template, $lineInfo, $var);
}
}

Expand Down

0 comments on commit 2c5350b

Please sign in to comment.