Browse Source

Move helpers to Shim.

mscherer 6 years ago
parent
commit
be2a60486d

+ 0 - 1
composer.json

@@ -53,7 +53,6 @@
 		"cs-check": "phpcs -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php --ignore=/tests/test_files/ src/ tests/ config/",
 		"cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php --ignore=/tests/test_files/ src/ tests/ config/"
 	},
-	"prefer-stable": true,
 	"minimum-stability": "dev",
 	"config": {
 		"process-timeout": 600

+ 3 - 100
docs/TestSuite/Testing.md

@@ -2,108 +2,11 @@
 
 Let's you test even faster.
 
-## ToolsTestTrait
-
-This trait adds the following methods to your test suite:
-
-### osFix()
-
-In case you need to format certain os specific files to "\n" before comparing
-them as strings.
-
-### debug()
-
-This is very useful when debugging certain tests when writing them.
-
-```php
-$result = $this->get($id);
-$this->debug($result);
-
-$this->assertSomething(...);
-```
-Here the debug statement is harmless by default. Only when you run phpunit with `-v` or `-vv`,
-additional debug output is printed to the screen.
-
-By default this trait is attached to IntegrationTestCase, TestCase and ConsoleOutput.
-
-Tip: This verbose debug feature is best used in combination with `--filter testMethodToTest`, as
-otherwise there might be too much output on the screen. So better filter down to the actual method
-you are currently working on or debugging:
-```
-php phpunit.phar --filter testFooBar /path/to/SomeTest.php -vv
-```
-
-### isDebug()
-With this method you can apply additional testing code if a `--debug` flag has been set.
-
-As an example you can by default mock out some API call, but with the debug flat set use
-the real API connection (for local testing). This way you can quickly confirm that the API
-connection is not only still working in simulated (mocking) the way it used to, but also
-that it's still the real deal.
-```php
-$this->Api = new ApiClass();
-
-if (!$this->isDebug()) {
-    $this->Api = $this->getMock('ApiClass');
-    $this->Api->expects(...)->...;
-}
-```
-
-
 ## IntegrationTestCase
 
-See the above trait features.
-
-For details on this see [Shim plugin](https://github.com/dereuromark/cakephp-shim).
-
-## TestCase
-`assertNotWithinMargin()` as the opposite of `assertWithinMargin()` is available.
-
-Also see the above trait features.
-
-## ConsoleOutput
-By default, this class captures the output to stderr or stdout internally in an array.
-
+You can globally set
 ```php
-namespace App\Test\TestCase\Shell;
-
-use App\Shell\FooBarShell;
-use Cake\Console\ConsoleIo;
-use Tools\TestSuite\ConsoleOutput;
-use Tools\TestSuite\TestCase;
-
-class FooBarShellTest extends TestCase {
-
-    /**
-     * @return void
-     */
-    public function setUp(): void {
-        parent::setUp();
-
-        $this->out = new ConsoleOutput();
-        $this->err = new ConsoleOutput();
-        $io = new ConsoleIo($this->out, $this->err);
-
-        $this->Shell = $this->getMock(
-            'App\Shell\FooBarShell',
-            ['in', '_stop'],
-            [$io]
-        );
-    }
-```
-Note that we mock the `in` and `_stop` methods, though, to allow handling those by mocking them out in the test cases.
-
-You can afterwards check on the output:
-```php
-$this->Shell->runCommand(['baz']);
-
-$output = $this->err->output();
-$this->assertEmpty($output);
-
-$output = $this->out->output();
-$expected = 'FooBars';
-$this->assertStringContainsString($expected, $output);
+    protected $disableErrorHandlerMiddleware = true;
 ```
 
-Also see the above trait features. By using `-v` or `-vv` you can directly see any stderr or stdout output on the screen.
-Otherwise they will not be displayed automatically.
+This is a quick way to disable all error handler middlewares on this integration test case.

+ 0 - 57
src/TestSuite/ConsoleOutput.php

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

+ 0 - 2
src/TestSuite/IntegrationTestCase.php

@@ -9,8 +9,6 @@ use Shim\TestSuite\IntegrationTestCase as ShimIntegrationTestCase;
  */
 abstract class IntegrationTestCase extends ShimIntegrationTestCase {
 
-	use ToolsTestTrait;
-
 	/**
 	 * Globally disabling error handler middleware to see the actual errors instead of cloaking.
 	 *

+ 2 - 5
src/TestSuite/TestCase.php

@@ -2,13 +2,10 @@
 
 namespace Tools\TestSuite;
 
-use Cake\TestSuite\TestCase as CakeTestCase;
+use Shim\TestSuite\TestCase as ShimTestCase;
 
 /**
  * Tools TestCase class
  */
-abstract class TestCase extends CakeTestCase {
-
-	use ToolsTestTrait;
-
+abstract class TestCase extends ShimTestCase {
 }

+ 0 - 120
src/TestSuite/ToolsTestTrait.php

@@ -1,120 +0,0 @@
-<?php
-
-namespace Tools\TestSuite;
-
-use ReflectionClass;
-
-/**
- * Utility methods for easier testing in CakePHP & PHPUnit
- */
-trait ToolsTestTrait {
-
-	/**
-	 * OsFix method
-	 *
-	 * @param string $string
-	 * @return string
-	 */
-	protected static function osFix(string $string): string {
-		return str_replace(["\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(): bool {
-		return !empty($_SERVER['argv']) && in_array('--debug', $_SERVER['argv'], true);
-	}
-
-	/**
-	 * Checks if verbose 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): bool {
-		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.
-	 *
-	 * Forces flushing the output if -v or -vv is set.
-	 *
-	 * @param mixed $data
-	 * @return void
-	 */
-	protected static function debug($data): void {
-		if (!static::isVerbose()) {
-			return;
-		}
-		$showFrom = static::isVerbose(true);
-
-		debug($data, null, $showFrom);
-	}
-
-	/**
-	 * Call protected/private method of a class.
-	 *
-	 * So
-	 *   $this->invokeMethod($user, 'cryptPassword', array('passwordToCrypt'));
-	 * is equal to
-	 *   $user->cryptPassword('passwordToCrypt');
-	 * (assuming the method was directly publicly accessible
-	 *
-	 * @param object &$object Instantiated object that we will run method on.
-	 * @param string $methodName Method name to call.
-	 * @param array $parameters Array of parameters to pass into method.
-	 *
-	 * @return mixed Method return.
-	 */
-	protected function invokeMethod(&$object, string $methodName, array $parameters = []) {
-		$reflection = new ReflectionClass(get_class($object));
-		$method = $reflection->getMethod($methodName);
-		$method->setAccessible(true);
-
-		return $method->invokeArgs($object, $parameters);
-	}
-
-	/**
-	 * Gets protected/private property of a class.
-	 *
-	 * So
-	 *   $this->invokeProperty($object, '_foo');
-	 * is equal to
-	 *   $object->_foo
-	 * (assuming the property was directly publicly accessible)
-	 *
-	 * @param object &$object Instantiated object that we want the property off.
-	 * @param string $name Property name to fetch.
-	 *
-	 * @return mixed Property value.
-	 */
-	protected function invokeProperty(&$object, string $name) {
-		$reflection = new ReflectionClass(get_class($object));
-		$property = $reflection->getProperty($name);
-		$property->setAccessible(true);
-
-		return $property->getValue($object);
-	}
-
-}

+ 0 - 3
tests/TestCase/Error/Middleware/ErrorHandlerMiddlewareTest.php

@@ -7,12 +7,9 @@ use Cake\Http\Exception\NotFoundException;
 use Cake\Http\ServerRequest;
 use Tools\Error\Middleware\ErrorHandlerMiddleware;
 use Tools\TestSuite\TestCase;
-use Tools\TestSuite\ToolsTestTrait;
 
 class ErrorHandlerMiddlewareTest extends TestCase {
 
-	use ToolsTestTrait;
-
 	/**
 	 * @var \Tools\Error\Middleware\ErrorHandlerMiddleware
 	 */

+ 3 - 3
tests/TestCase/Shell/InflectShellTest.php

@@ -3,8 +3,8 @@
 namespace Tools\Test\TestCase\Shell;
 
 use Cake\Console\ConsoleIo;
+use Shim\TestSuite\ConsoleOutput;
 use Tools\Shell\InflectShell;
-use Tools\TestSuite\ConsoleOutput;
 use Tools\TestSuite\TestCase;
 
 class InflectShellTest extends TestCase {
@@ -15,12 +15,12 @@ class InflectShellTest extends TestCase {
 	protected $Shell;
 
 	/**
-	 * @var \Tools\TestSuite\ConsoleOutput
+	 * @var \Shim\TestSuite\ConsoleOutput
 	 */
 	protected $out;
 
 	/**
-	 * @var \Tools\TestSuite\ConsoleOutput
+	 * @var \Shim\TestSuite\ConsoleOutput
 	 */
 	protected $err;
 

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

@@ -1,89 +0,0 @@
-<?php
-
-namespace Tools\Test\TestCase\TestSuite;
-
-use Tools\TestSuite\TestCase;
-
-class ToolsTestTraitTest extends TestCase {
-
-	/**
-	 * @return void
-	 */
-	public function setUp(): void {
-		parent::setUp();
-
-		$this->serverArgBackup = !empty($_SERVER['argv']) ? $_SERVER['argv'] : null;
-		$_SERVER['argv'] = [];
-	}
-
-	/**
-	 * @return void
-	 */
-	public function tearDown(): void {
-		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'] = ['--debug'];
-		$result = $this->isDebug();
-		$this->assertTrue($result);
-	}
-
-	/**
-	 * ToolsTestTraitTest::testIsVerbose()
-	 *
-	 * @return void
-	 */
-	public function testIsVerbose() {
-		$_SERVER['argv'] = ['--debug'];
-		$result = $this->isVerbose();
-		$this->assertFalse($result);
-
-		$_SERVER['argv'] = ['-v'];
-		$result = $this->isVerbose();
-		$this->assertTrue($result);
-
-		$_SERVER['argv'] = ['-vv'];
-		$result = $this->isVerbose();
-		$this->assertTrue($result);
-
-		$_SERVER['argv'] = ['-v', '-vv'];
-		$result = $this->isVerbose();
-		$this->assertTrue($result);
-
-		$_SERVER['argv'] = ['-v'];
-		$result = $this->isVerbose(true);
-		$this->assertFalse($result);
-
-		$_SERVER['argv'] = ['-vv'];
-		$result = $this->isVerbose(true);
-		$this->assertTrue($result);
-
-		$_SERVER['argv'] = ['-v', '-vv'];
-		$result = $this->isVerbose(true);
-		$this->assertTrue($result);
-	}
-
-}

+ 0 - 3
tests/TestCase/View/Helper/MeterHelperTest.php

@@ -5,14 +5,11 @@ namespace Tools\Test\TestCase\View\Helper;
 use Cake\View\View;
 use InvalidArgumentException;
 use Tools\TestSuite\TestCase;
-use Tools\TestSuite\ToolsTestTrait;
 use Tools\Utility\Number;
 use Tools\View\Helper\MeterHelper;
 
 class MeterHelperTest extends TestCase {
 
-	use ToolsTestTrait;
-
 	/**
 	 * @var \Cake\View\View
 	 */