]> git.mxchange.org Git - friendica.git/commitdiff
Enable testability for HTTPInputData and create a failing test for it :-)
authorPhilipp <admin@philipp.info>
Sun, 23 May 2021 17:58:09 +0000 (19:58 +0200)
committerPhilipp <admin@philipp.info>
Sun, 23 May 2021 17:58:09 +0000 (19:58 +0200)
src/Util/HTTPInputData.php
tests/Util/HTTPInputDataDouble.php [new file with mode: 0644]
tests/src/Util/HTTPInputDataTest.php [new file with mode: 0644]

index 5b151a2015532631ab6f1bae5b1993be95e8613a..aebae9ac20e834b44ff73dead96e8d9341f63352 100644 (file)
@@ -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 (file)
index 0000000..c00fa6f
--- /dev/null
@@ -0,0 +1,66 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+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 (file)
index 0000000..f1deaa7
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+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);
+       }
+}