Browse Source

Merge branch 'pr-cli' into 2.4

From pull request 1142
mark_story 13 years ago
parent
commit
92d215df4d
2 changed files with 27 additions and 5 deletions
  1. 21 0
      lib/Cake/Test/Case/BasicsTest.php
  2. 6 5
      lib/Cake/basics.php

+ 21 - 0
lib/Cake/Test/Case/BasicsTest.php

@@ -853,6 +853,7 @@ EXPECTED;
  * @return void
  */
 	public function testPr() {
+		$this->skipIf(php_sapi_name() == 'cli', 'Skipping web test in cli mode');
 		ob_start();
 		pr('this is a test');
 		$result = ob_get_clean();
@@ -867,6 +868,26 @@ EXPECTED;
 	}
 
 /**
+ * test pr()
+ *
+ * @return void
+ */
+	public function testPrCli() {
+		$this->skipIf(php_sapi_name() != 'cli', 'Skipping cli test in web mode');
+		ob_start();
+		pr('this is a test');
+		$result = ob_get_clean();
+		$expected = "\nthis is a test\n";
+		$this->assertEquals($expected, $result);
+
+		ob_start();
+		pr(array('this' => 'is', 'a' => 'test'));
+		$result = ob_get_clean();
+		$expected = "\nArray\n(\n    [this] => is\n    [a] => test\n)\n\n";
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * test stripslashes_deep()
  *
  * @return void

+ 6 - 5
lib/Cake/basics.php

@@ -230,8 +230,10 @@ if (!function_exists('pluginSplit')) {
 if (!function_exists('pr')) {
 
 /**
- * Print_r convenience function, which prints out <PRE> tags around
- * the output of given array. Similar to debug().
+ * print_r() convenience function
+ * 
+ * In terminals this will act the same as using print_r() directly, when not run on cli 
+ * print_r() will wrap <PRE> tags around the output of given array. Similar to debug().
  *
  * @see	debug()
  * @param array $var Variable to print out
@@ -239,9 +241,8 @@ if (!function_exists('pr')) {
  */
 	function pr($var) {
 		if (Configure::read('debug') > 0) {
-			echo '<pre>';
-			print_r($var);
-			echo '</pre>';
+			$template = php_sapi_name() !== 'cli' ? '<pre>%s</pre>' : "\n%s\n";
+			echo sprintf($template, print_r($var, true));
 		}
 	}