From 15216266d95d7660a9f89970bea3477949e79e0f Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 23 May 2021 22:40:41 +0200 Subject: [PATCH] Add ContentType Injection for HTTPInputData tests --- src/Util/HTTPInputData.php | 15 ++++++++++++++- tests/Util/HTTPInputDataDouble.php | 20 ++++++++++++++++++++ tests/src/Util/HTTPInputDataTest.php | 13 ++++++++----- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Util/HTTPInputData.php b/src/Util/HTTPInputData.php index 322a462f12..8886b61e4c 100644 --- a/src/Util/HTTPInputData.php +++ b/src/Util/HTTPInputData.php @@ -29,7 +29,7 @@ class HTTPInputData { public static function process() { - $content_parts = explode(';', $_SERVER['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded'); + $content_parts = explode(';', static::getContentType()); $boundary = ''; $encoding = ''; @@ -263,6 +263,7 @@ class HTTPInputData /** * Returns the current PHP input stream * Mainly used for test doubling + * * @return false|resource */ protected static function getPhpInputStream() @@ -273,10 +274,22 @@ class HTTPInputData /** * Returns the content of the current PHP input * Mainly used for test doubling + * * @return false|string */ protected static function getPhpInputContent() { return file_get_contents('php://input'); } + + /** + * Returns the content type string of the current call + * Mainly used for test doubling + * + * @return false|string + */ + protected static function getContentType() + { + return $_SERVER['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded'; + } } diff --git a/tests/Util/HTTPInputDataDouble.php b/tests/Util/HTTPInputDataDouble.php index c00fa6fad9..6db5c44850 100644 --- a/tests/Util/HTTPInputDataDouble.php +++ b/tests/Util/HTTPInputDataDouble.php @@ -33,9 +33,12 @@ class HTTPInputDataDouble extends HTTPInputData protected static $injectedStream = false; /** @var false|string */ protected static $injectedContent = false; + /** @var false|string */ + protected static $injectedContentType = false; /** * injects the PHP input stream for a test + * * @param false|resource $stream */ public static function setPhpInputStream($stream) @@ -45,6 +48,7 @@ class HTTPInputDataDouble extends HTTPInputData /** * injects the PHP input content for a test + * * @param false|string $content */ public static function setPhpInputContent($content) @@ -52,6 +56,16 @@ class HTTPInputDataDouble extends HTTPInputData self::$injectedContent = $content; } + /** + * injects the PHP input content type for a test + * + * @param false|string $contentType + */ + public static function setPhpInputContentType($contentType) + { + self::$injectedContentType = $contentType; + } + /** {@inheritDoc} */ protected static function getPhpInputStream() { @@ -63,4 +77,10 @@ class HTTPInputDataDouble extends HTTPInputData { return static::$injectedContent; } + + /** {@inheritDoc} */ + protected static function getContentType() + { + return static::$injectedContentType; + } } diff --git a/tests/src/Util/HTTPInputDataTest.php b/tests/src/Util/HTTPInputDataTest.php index a4e3ce20df..f50755a30f 100644 --- a/tests/src/Util/HTTPInputDataTest.php +++ b/tests/src/Util/HTTPInputDataTest.php @@ -27,6 +27,7 @@ use Friendica\Util\HTTPInputData; /** * Testing HTTPInputData + * * @see HTTPInputData */ class HTTPInputDataTest extends MockedTest @@ -35,6 +36,7 @@ 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() @@ -67,16 +69,17 @@ class HTTPInputDataTest extends MockedTest /** * Tests the HTTPInputData::process() method - * @see HTTPInputData::process() - * @param string $contenttype The content typer of the transmitted data + * + * @param string $contentType The content typer of the transmitted data * @param string $input The input, we got from the data stream * @param array $expected The expected output + * * @dataProvider dataStream + * @see HTTPInputData::process() */ - public function testHttpInput(string $contenttype, string $input, array $expected) + public function testHttpInput(string $contentType, string $input, array $expected) { - $_SERVER['CONTENT_TYPE'] = $contenttype; - + HTTPInputDataDouble::setPhpInputContentType($contentType); HTTPInputDataDouble::setPhpInputContent($input); $stream = fopen('php://memory', 'r+'); fwrite($stream, $input);