]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Util/HTTPInputDataTest.php
Merge remote-tracking branch 'upstream/2021.12-rc' into api-fixes
[friendica.git] / tests / src / Util / HTTPInputDataTest.php
index a343e1a76e41e66ad3d0e89f1487895c75e0097c..0d7c3938b50f94e5f13a0c51661f7c86e44b207e 100644 (file)
@@ -27,6 +27,7 @@ use Friendica\Util\HTTPInputData;
 
 /**
  * Testing HTTPInputData
+ *
  * @see        HTTPInputData
  */
 class HTTPInputDataTest extends MockedTest
@@ -35,17 +36,90 @@ 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'    => file_get_contents(__DIR__ . '/../../datasets/http/example1.httpinput'),
-                               'expected' => [
+                       'multipart' => [
+                               'contenttype' => 'multipart/form-data;boundary=43395968-f65c-437e-b536-5b33e3e3c7e5;charset=utf8',
+                               'input'       => file_get_contents(__DIR__ . '/../../datasets/http/multipart.httpinput'),
+                               'expected'    => [
+                                       'variables' => [
+                                               'display_name'      => 'User Name',
+                                               'note'              => 'About me',
+                                               'locked'            => 'false',
+                                               'fields_attributes' => [
+                                                       0 => [
+                                                               'name'  => 'variable 1',
+                                                               'value' => 'value 1',
+                                                       ],
+                                                       1 => [
+                                                               'name'  => 'variable 2',
+                                                               'value' => 'value 2',
+                                                       ]
+                                               ]
+                                       ],
+                                       'files' => []
+                               ]
+                       ],
+                       'multipart-file' => [
+                               'contenttype' => 'multipart/form-data;boundary=6d4d5a40-651a-4468-a62e-5a6ca2bf350d;charset=utf8',
+                               'input'       => file_get_contents(__DIR__ . '/../../datasets/http/multipart-file.httpinput'),
+                               'expected'    => [
+                                       'variables' => [
+                                               'display_name'      => 'Vorname Nachname',
+                                               'note'              => 'About me',
+                                               'fields_attributes' => [
+                                                       0 => [
+                                                               'name'  => 'variable 1',
+                                                               'value' => 'value 1',
+                                                       ],
+                                                       1 => [
+                                                               'name'  => 'variable 2',
+                                                               'value' => 'value 2',
+                                                       ]
+                                               ]
+                                       ],
+                                       'files' => [
+                                               'avatar' => [
+                                                       'name'     => '8ZUCS34Y5XNH',
+                                                       'type'     => 'image/png',
+                                                       'tmp_name' => '8ZUCS34Y5XNH',
+                                                       'error'    => 0,
+                                                       'size'     => 349330
+                                               ],
+                                               'header' => [
+                                                       'name'     => 'V2B6Z1IICGPM',
+                                                       'type'     => 'image/png',
+                                                       'tmp_name' => 'V2B6Z1IICGPM',
+                                                       'error'    => 0,
+                                                       'size'     => 1323635
+                                               ]
+                                       ]
+                               ]
+                       ],
+                       'form-urlencoded' => [
+                               'contenttype' => 'application/x-www-form-urlencoded;charset=utf8',
+                               'input'       => file_get_contents(__DIR__ . '/../../datasets/http/form-urlencoded.httpinput'),
+                               'expected'    => [
                                        'variables' => [
-                                               'var1' => 'value1',
-                                               'var2' => 'value2',
+                                               'title' => 'Test2',
+                                       ],
+                                       'files' => []
+                               ]
+                       ],
+                       'form-urlencoded-json' => [
+                               'contenttype' => 'application/x-www-form-urlencoded;charset=utf8',
+                               'input'       => file_get_contents(__DIR__ . '/../../datasets/http/form-urlencoded-json.httpinput'),
+                               'expected'    => [
+                                       'variables' => [
+                                               'media_ids'    => [],
+                                               'sensitive'    => false,
+                                               'status'       => 'Test Status',
+                                               'visibility'   => 'private',
+                                               'spoiler_text' => 'Title'
                                        ],
                                        'files' => []
                                ]
@@ -55,20 +129,25 @@ class HTTPInputDataTest extends MockedTest
 
        /**
         * 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
+        *
+        * @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 $input, array $expected)
+       public function testHttpInput(string $contentType, string $input, array $expected)
        {
-               HTTPInputDataDouble::setPhpInputContent($input);
+               $httpInput = new HTTPInputDataDouble(['CONTENT_TYPE' => $contentType]);
+               $httpInput->setPhpInputContent($input);
+
                $stream = fopen('php://memory', 'r+');
                fwrite($stream, $input);
                rewind($stream);
 
-               HTTPInputDataDouble::setPhpInputStream($stream);
-               $output = HTTPInputDataDouble::process();
+               $httpInput->setPhpInputStream($stream);
+               $output = $httpInput->process();
                $this->assertEqualsCanonicalizing($expected, $output);
        }
 }