From a0da84b6b021149e390b3a13514979d58385855f Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 23 May 2021 19:58:09 +0200 Subject: [PATCH] Enable testability for HTTPInputData and create a failing test for it :-) --- src/Util/HTTPInputData.php | 24 ++++++++- tests/Util/HTTPInputDataDouble.php | 66 +++++++++++++++++++++++++ tests/src/Util/HTTPInputDataTest.php | 74 ++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 tests/Util/HTTPInputDataDouble.php create mode 100644 tests/src/Util/HTTPInputDataTest.php diff --git a/src/Util/HTTPInputData.php b/src/Util/HTTPInputData.php index 5b151a2015..aebae9ac20 100644 --- a/src/Util/HTTPInputData.php +++ b/src/Util/HTTPInputData.php @@ -58,7 +58,7 @@ class HTTPInputData } // can be handled by built in PHP functionality - $content = file_get_contents('php://input'); + $content = static::getPhpInputContent(); $variables = json_decode($content); @@ -73,7 +73,7 @@ class HTTPInputData { $result = ['variables' => [], 'files' => []]; - $stream = fopen('php://input', 'rb'); + $stream = static::getPhpInputStream(); $sanity = fgets($stream, strlen($boundary) + 5); @@ -259,4 +259,24 @@ class HTTPInputData return $variables; } + + /** + * Returns the current PHP input stream + * Mainly used for test doubling + * @return false|resource + */ + protected static function getPhpInputStream() + { + return fopen('php://input', 'rb'); + } + + /** + * Returns the content of tje current PHP input + * Mainly used for test doubling + * @return false|string + */ + protected static function getPhpInputContent() + { + return file_get_contents('php://input'); + } } diff --git a/tests/Util/HTTPInputDataDouble.php b/tests/Util/HTTPInputDataDouble.php new file mode 100644 index 0000000000..c00fa6fad9 --- /dev/null +++ b/tests/Util/HTTPInputDataDouble.php @@ -0,0 +1,66 @@ +. + * + */ + +namespace Friendica\Test\Util; + +use Friendica\Util\HTTPInputData; + +/** + * This class is used to enable testability for HTTPInputData + * It overrides the two PHP input functionality with custom content + */ +class HTTPInputDataDouble extends HTTPInputData +{ + /** @var false|resource */ + protected static $injectedStream = false; + /** @var false|string */ + protected static $injectedContent = false; + + /** + * injects the PHP input stream for a test + * @param false|resource $stream + */ + public static function setPhpInputStream($stream) + { + self::$injectedStream = $stream; + } + + /** + * injects the PHP input content for a test + * @param false|string $content + */ + public static function setPhpInputContent($content) + { + self::$injectedContent = $content; + } + + /** {@inheritDoc} */ + protected static function getPhpInputStream() + { + return static::$injectedStream; + } + + /** {@inheritDoc} */ + protected static function getPhpInputContent() + { + return static::$injectedContent; + } +} diff --git a/tests/src/Util/HTTPInputDataTest.php b/tests/src/Util/HTTPInputDataTest.php new file mode 100644 index 0000000000..f1deaa7ffa --- /dev/null +++ b/tests/src/Util/HTTPInputDataTest.php @@ -0,0 +1,74 @@ +. + * + */ + +namespace Friendica\Test\src\Util; + +use Friendica\Test\MockedTest; +use Friendica\Test\Util\HTTPInputDataDouble; +use Friendica\Util\HTTPInputData; + +/** + * Testing HTTPInputData + * @see HTTPInputData + */ +class HTTPInputDataTest extends MockedTest +{ + /** + * Returns the data stream for the unit test + * Each array element of the first hierarchy represents one test run + * Each array element of the second hierarchy represents the parameters, passed to the test function + * @return array[] + */ + public function dataStream() + { + return [ + 'example' => [ + 'input' => 'anything you want', + 'expected' => [ + 'variables' => [ + 'var1' => 'value', + 'var2' => 'value', + ], + 'files' => [] + ] + ] + ]; + } + + /** + * Tests the HTTPInputData::process() method + * @see HTTPInputData::process() + * @param string $input The input, we got from the data stream + * @param array $expected The expected output + * @dataProvider dataStream + */ + public function testHttpInput(string $input, array $expected) + { + HTTPInputDataDouble::setPhpInputContent($input); + $stream = fopen('php://memory', 'r+'); + fwrite($stream, $input); + rewind($stream); + + HTTPInputDataDouble::setPhpInputStream($stream); + $output = HTTPInputDataDouble::process(); + $this->assertEqualsCanonicalizing($output, $expected); + } +}