Browse Source

ToolsTestTrait

euromark 11 years ago
parent
commit
c6f2ee4ebc
2 changed files with 121 additions and 4 deletions
  1. 37 4
      src/TestSuite/ToolsTestTrait.php
  2. 84 0
      tests/TestCase/TestSuite/ToolsTestTraitTest.php

+ 37 - 4
src/TestSuite/ToolsTestTrait.php

@@ -12,11 +12,45 @@ trait ToolsTestTrait {
 	 * @param string $string
 	 * @return string
 	 */
-	protected static function _osFix($string) {
+	protected static function osFix($string) {
 		return str_replace(array("\r\n", "\r"), "\n", $string);
 	}
 
 	/**
+	 * Checks if debug flag is set.
+	 *
+	 * Flag is set via `--debug`.
+	 * Allows additional stuff like non-mocking when enabling debug.
+	 *
+	 * @return bool Success
+	 */
+	protected static function isDebug() {
+		return !empty($_SERVER['argv']) && in_array('--debug', $_SERVER['argv'], true);
+	}
+
+	/**
+	 * Checks if debug flag is set.
+	 *
+	 * Flags are `-v` and `-vv`.
+	 * Allows additional stuff like non-mocking when enabling debug.
+	 *
+	 * @param bool $onlyVeryVerbose If only -vv should be counted.
+	 * @return bool Success
+	 */
+	protected static function isVerbose($onlyVeryVerbose = false) {
+		if (empty($_SERVER['argv'])) {
+			return false;
+		}
+		if (!$onlyVeryVerbose && in_array('-v', $_SERVER['argv'], true)) {
+			return true;
+		}
+		if (in_array('-vv', $_SERVER['argv'], true)) {
+			return true;
+		}
+		return false;
+	}
+
+	/**
 	 * Outputs debug information during a test run.
 	 * This is a convenience output handler since debug() itself is not desired
 	 * for tests in general.
@@ -28,11 +62,10 @@ trait ToolsTestTrait {
 	 * @return void
 	 */
 	protected static function debug($data) {
-		$output = !empty($_SERVER['argv']) && (in_array('-v', $_SERVER['argv'], true) || in_array('-vv', $_SERVER['argv'], true));
-		if (!$output) {
+		if (!static::isVerbose()) {
 			return;
 		}
-		$showFrom = in_array('-vv', $_SERVER['argv'], true);
+		$showFrom = static::isVerbose(true);
 
 		debug($data, null, $showFrom);
 	}

+ 84 - 0
tests/TestCase/TestSuite/ToolsTestTraitTest.php

@@ -0,0 +1,84 @@
+<?php
+namespace Tools\TestCase\TestSuite;
+
+use Tools\TestSuite\TestCase;
+
+class ToolsTestTraitTest extends TestCase {
+
+	public $TestCase;
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->serverArgBackup = !empty($_SERVER['argv']) ? $_SERVER['argv'] : null;
+		$_SERVER['argv'] = array();
+	}
+
+	public function tearDown() {
+		parent::tearDown();
+
+		$_SERVER['argv'] = $this->serverArgBackup;
+	}
+
+	/**
+	 * MimeTest::testOsFix()
+	 *
+	 * @return void
+	 */
+	public function testOsFix() {
+		$string = "Foo\r\nbar";
+		$result = $this->osFix($string);
+		$expected = "Foo\nbar";
+		$this->assertSame($expected, $result);
+	}
+
+	/**
+	 * ToolsTestTraitTest::testIsDebug()
+	 *
+	 * @return void
+	 */
+	public function testIsDebug() {
+		$result = $this->isDebug();
+		$this->assertFalse($result);
+
+		$_SERVER['argv'] = array('--debug');
+		$result = $this->isDebug();
+		$this->assertTrue($result);
+	}
+
+	/**
+	 * ToolsTestTraitTest::testIsVerbose()
+	 *
+	 * @return void
+	 */
+	public function testIsVerbose() {
+		$_SERVER['argv'] = array('--debug');
+		$result = $this->isVerbose();
+		$this->assertFalse($result);
+
+		$_SERVER['argv'] = array('-v');
+		$result = $this->isVerbose();
+		$this->assertTrue($result);
+
+		$_SERVER['argv'] = array('-vv');
+		$result = $this->isVerbose();
+		$this->assertTrue($result);
+
+		$_SERVER['argv'] = array('-v', '-vv');
+		$result = $this->isVerbose();
+		$this->assertTrue($result);
+
+		$_SERVER['argv'] = array('-v');
+		$result = $this->isVerbose(true);
+		$this->assertFalse($result);
+
+		$_SERVER['argv'] = array('-vv');
+		$result = $this->isVerbose(true);
+		$this->assertTrue($result);
+
+		$_SERVER['argv'] = array('-v', '-vv');
+		$result = $this->isVerbose(true);
+		$this->assertTrue($result);
+	}
+
+}