]> git.mxchange.org Git - friendica.git/commitdiff
Add ContentType Injection for HTTPInputData tests
authorPhilipp <admin@philipp.info>
Sun, 23 May 2021 20:40:41 +0000 (22:40 +0200)
committerPhilipp <admin@philipp.info>
Sun, 23 May 2021 20:40:41 +0000 (22:40 +0200)
src/Util/HTTPInputData.php
tests/Util/HTTPInputDataDouble.php
tests/src/Util/HTTPInputDataTest.php

index 322a462f12a0a79771ab2f9eb74208d0f1538b37..8886b61e4c54ce236fee96bd50438906ba66c9d4 100644 (file)
@@ -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';
+       }
 }
index c00fa6fad9297e88b80399b2c68540db3d6716e7..6db5c44850e0f7816804dd5e76e221b857f3d32e 100644 (file)
@@ -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;
+       }
 }
index a4e3ce20df8f3adae4d51d69c0aaae0a6ccf1cbb..f50755a30f94f36c5eedddbdcf7883a60126a76f 100644 (file)
@@ -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);