Enable testability for HTTPInputData and create a failing test for it :-)
This commit is contained in:
parent
fd3706b72e
commit
a0da84b6b0
3 changed files with 162 additions and 2 deletions
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
66
tests/Util/HTTPInputDataDouble.php
Normal file
66
tests/Util/HTTPInputDataDouble.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
74
tests/src/Util/HTTPInputDataTest.php
Normal file
74
tests/src/Util/HTTPInputDataTest.php
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue