ソースを参照

Refactor tests.

euromark 11 年 前
コミット
060e946e96

+ 2 - 1
.gitignore

@@ -1,4 +1,5 @@
 /composer.lock
 /phpunit.phar
 /vendor/
-/tmp/
+/tmp/
+/plugins/Setup

+ 4 - 1
composer.json

@@ -14,9 +14,12 @@
 	],
 	"require":{
 		"php": ">=5.4",
-		"composer/installers": "*",
+		"cakephp/plugin-installer": "*",
 		"cakephp/cakephp": "3.0.*-dev"
 	},
+	"require-dev":{
+		"dereuromark/cakephp-setup": "dev-cake3"
+	},
 	"autoload": {
 		"psr-4": {
 			"Tools\\": "src"

+ 50 - 0
src/TestSuite/ConsoleOutput.php

@@ -0,0 +1,50 @@
+<?php
+namespace Tools\TestSuite;
+
+use Cake\Console\ConsoleOutput as CakeConsoleOutput;
+use Tools\TestSuite\Traits\ToolsTestTrait;
+
+/**
+ * Use for testing as
+ *
+ *  use Tools\TestSuite\ConsoleOutput;
+ *
+ *  $stdOut = new ConsoleOutput();
+ *  $stdErr = new ConsoleOutput();
+ *  $io = new ConsoleIo($stdOut, $stdErr);
+ *
+ * @license MIT
+ * @author Mark Scherer
+ */
+class ConsoleOutput extends CakeConsoleOutput {
+
+	use ToolsTestTrait;
+
+	/**
+	 * Holds all output messages.
+	 *
+	 * @var array
+	 */
+	public $output = array();
+
+	/**
+	 * Overwrite _write to output the message to debug instead of CLI.
+	 *
+	 * @param string $message
+	 * @return void
+	 */
+	protected function _write($message) {
+		$this->debug($message);
+		$this->output[] = $message;
+	}
+
+	/**
+	 * Helper method to return the debug output as string.
+	 *
+	 * @return string
+	 */
+	public function output() {
+		return implode('', $this->output);
+	}
+
+}

+ 3 - 0
src/TestSuite/IntegrationTestCase.php

@@ -3,6 +3,7 @@ namespace Tools\TestSuite;
 
 use Cake\TestSuite\IntegrationTestCase as CakeIntegrationTestCase;
 use Cake\Routing\Router;
+use Tools\TestSuite\Traits\ToolsTestTrait;
 
 /**
  * Tools TestCase class
@@ -10,6 +11,8 @@ use Cake\Routing\Router;
  */
 abstract class IntegrationTestCase extends CakeIntegrationTestCase {
 
+	use ToolsTestTrait;
+
 	/**
 	 * Create a request object with the configured options and parameters.
 	 *

+ 3 - 30
src/TestSuite/TestCase.php

@@ -2,6 +2,7 @@
 namespace Tools\TestSuite;
 
 use Cake\TestSuite\TestCase as CakeTestCase;
+use Tools\TestSuite\Traits\ToolsTestTrait;
 
 /**
  * Tools TestCase class
@@ -9,6 +10,8 @@ use Cake\TestSuite\TestCase as CakeTestCase;
  */
 abstract class TestCase extends CakeTestCase {
 
+	use ToolsTestTrait;
+
 	/**
 	 * Opposite wrapper method of assertWithinMargin.
 	 *
@@ -24,34 +27,4 @@ abstract class TestCase extends CakeTestCase {
 		return static::assertFalse((($expected <= $upper) && ($expected >= $lower)), $message);
 	}
 
-	/**
-	 * OsFix method
-	 *
-	 * @param string $string
-	 * @return string
-	 */
-	protected static function _osFix($string) {
-		return str_replace(array("\r\n", "\r"), "\n", $string);
-	}
-
-	/**
-	 * Outputs debug information during a web tester (browser) test case
-	 * since PHPUnit>=3.6 swallowes all output by default.
-	 * This is a convenience output handler since debug() or pr() have no effect
-	 *
-	 * @param mixed $data
-	 * @param bool $force Should the output be flushed (forced)
-	 * @return void
-	 */
-	protected static function debug($data, $force = false) {
-		if (php_sapi_name() === 'cli') {
-			return;
-		}
-		debug($data, null, false);
-		if (!$force) {
-			return;
-		}
-		ob_flush();
-	}
-
 }

+ 26 - 31
src/TestSuite/Traits/ToolsTestTrait.php

@@ -1,45 +1,40 @@
 <?php
 namespace Tools\TestSuite\Traits;
 
-use Cake\Controller\Controller;
-use Cake\Network\Response;
-use Cake\Routing\Router;
-
 /**
  * Utility methods for easier testing in CakePHP & PHPUnit
  */
 trait ToolsTestTrait {
 
-/**
- * Assert a redirect happened
- *
- * `$actual` can be a string, Controller or Response instance
- *
- * @param  string $expected
- * @param  mixed  $actual
- * @return void
- */
-	public function assertRedirect($expected, $actual = null) {
-		if ($actual === null) {
-			$actual = $this->controller;
-		}
-
-		if ($actual instanceof Controller) {
-			$actual = $actual->response->location();
-		}
-
-		if ($actual instanceof Response) {
-			$actual = $actual->location();
-		}
+	/**
+	 * OsFix method
+	 *
+	 * @param string $string
+	 * @return string
+	 */
+	protected static function _osFix($string) {
+		return str_replace(array("\r\n", "\r"), "\n", $string);
+	}
 
-		if (empty($actual)) {
-			throw new \Exception('assertRedirect: Expected "actual" to be a non-empty string');
+	/**
+	 * Outputs debug information during a test run.
+	 * This is a convenience output handler since debug() itself is not desired
+	 * for tests in general.
+	 *
+	 * Force flushing the output
+	 *
+	 * @param mixed $data
+	 * @param bool $force Should the output be flushed (forced)
+	 * @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) {
+			return;
 		}
+		$showFrom = in_array('-vv', $_SERVER['argv'], true);
 
-		if (is_array($expected)) {
-			$expected = Router::url($expected);
-		}
-		$this->assertEquals($expected, $actual,	'Was not redirected to ' . $expected);
+		debug($data, null, $showFrom);
 	}
 
 }

+ 10 - 0
tests/TestCase/Network/Email/EmailTest.php

@@ -410,6 +410,11 @@ html-part
 		//TODO
 	}
 
+	/**
+	 * EmailTest::testWrapLongEmailContent()
+	 *
+	 * @return void
+	 */
 	public function testWrapLongEmailContent() {
 		$this->Email = new TestEmail();
 
@@ -429,6 +434,11 @@ HTML;
 		$this->assertTrue(count($is) >= 5);
 	}
 
+	/**
+	 * EmailTest::testWrapCustomized()
+	 *
+	 * @return void
+	 */
 	public function testWrapCustomized() {
 		$this->Email = new TestEmail();
 

+ 11 - 26
tests/TestCase/Shell/InflectShellTest.php

@@ -2,25 +2,9 @@
 namespace Tools\Test\TestCase\Shell;
 
 use Tools\Shell\InflectShell;
+use Tools\TestSuite\ConsoleOutput;
 use Cake\Console\ConsoleIo;
-use Cake\Console\ConsoleOutput;
-use Cake\Console\Shell;
-use Cake\Core\Plugin;
-use Cake\TestSuite\TestCase;
-
-/**
- * Class TestCompletionStringOutput
- *
- */
-class TestInflectOutput extends ConsoleOutput {
-
-	public $output = '';
-
-	protected function _write($message) {
-		$this->output .= $message;
-	}
-
-}
+use Tools\TestSuite\TestCase;
 
 /**
  */
@@ -34,12 +18,13 @@ class InflectShellTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 
-		$this->out = new TestInflectOutput();
-		$io = new ConsoleIo($this->out);
+		$this->out = new ConsoleOutput();
+		$this->err = new ConsoleOutput();
+		$io = new ConsoleIo($this->out, $this->err);
 
 		$this->Shell = $this->getMock(
 			'Tools\Shell\InflectShell',
-			['in', 'err', '_stop'],
+			['in', '_stop'],
 			[$io]
 		);
 	}
@@ -64,27 +49,27 @@ class InflectShellTest extends TestCase {
 			->will($this->returnValue('FooBar'));
 
 		$this->Shell->runCommand(['pluralize']);
-		$output = $this->out->output;
+		$output = $this->out->output();
 		$expected = 'FooBars';
 		$this->assertContains($expected, $output);
 
 		$this->Shell->runCommand(['underscore']);
-		$output = $this->out->output;
+		$output = $this->out->output();
 		$expected = 'foo_bar';
 		$this->assertContains($expected, $output);
 
 		$this->Shell->runCommand(['dasherize']);
-		$output = $this->out->output;
+		$output = $this->out->output();
 		$expected = 'foo-bar';
 		$this->assertContains($expected, $output);
 
 		$this->Shell->runCommand(['slug']);
-		$output = $this->out->output;
+		$output = $this->out->output();
 		$expected = 'foo-bar';
 		$this->assertContains($expected, $output);
 
 		$this->Shell->runCommand(['tableize']);
-		$output = $this->out->output;
+		$output = $this->out->output();
 		$expected = 'foo_bar';
 		$this->assertContains($expected, $output);
 	}