Add ContentType Injection for HTTPInputData tests

This commit is contained in:
Philipp Holzer 2021-05-23 22:40:41 +02:00
parent a69e128fe4
commit 15216266d9
No known key found for this signature in database
GPG Key ID: 9A28B7D4FF5667BD
3 changed files with 42 additions and 6 deletions

View File

@ -29,7 +29,7 @@ class HTTPInputData
{ {
public static function process() public static function process()
{ {
$content_parts = explode(';', $_SERVER['CONTENT_TYPE'] ?? 'application/x-www-form-urlencoded'); $content_parts = explode(';', static::getContentType());
$boundary = ''; $boundary = '';
$encoding = ''; $encoding = '';
@ -263,6 +263,7 @@ class HTTPInputData
/** /**
* Returns the current PHP input stream * Returns the current PHP input stream
* Mainly used for test doubling * Mainly used for test doubling
*
* @return false|resource * @return false|resource
*/ */
protected static function getPhpInputStream() protected static function getPhpInputStream()
@ -273,10 +274,22 @@ class HTTPInputData
/** /**
* Returns the content of the current PHP input * Returns the content of the current PHP input
* Mainly used for test doubling * Mainly used for test doubling
*
* @return false|string * @return false|string
*/ */
protected static function getPhpInputContent() protected static function getPhpInputContent()
{ {
return file_get_contents('php://input'); 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';
}
} }

View File

@ -33,9 +33,12 @@ class HTTPInputDataDouble extends HTTPInputData
protected static $injectedStream = false; protected static $injectedStream = false;
/** @var false|string */ /** @var false|string */
protected static $injectedContent = false; protected static $injectedContent = false;
/** @var false|string */
protected static $injectedContentType = false;
/** /**
* injects the PHP input stream for a test * injects the PHP input stream for a test
*
* @param false|resource $stream * @param false|resource $stream
*/ */
public static function setPhpInputStream($stream) public static function setPhpInputStream($stream)
@ -45,6 +48,7 @@ class HTTPInputDataDouble extends HTTPInputData
/** /**
* injects the PHP input content for a test * injects the PHP input content for a test
*
* @param false|string $content * @param false|string $content
*/ */
public static function setPhpInputContent($content) public static function setPhpInputContent($content)
@ -52,6 +56,16 @@ class HTTPInputDataDouble extends HTTPInputData
self::$injectedContent = $content; 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} */ /** {@inheritDoc} */
protected static function getPhpInputStream() protected static function getPhpInputStream()
{ {
@ -63,4 +77,10 @@ class HTTPInputDataDouble extends HTTPInputData
{ {
return static::$injectedContent; return static::$injectedContent;
} }
/** {@inheritDoc} */
protected static function getContentType()
{
return static::$injectedContentType;
}
} }

View File

@ -27,6 +27,7 @@ use Friendica\Util\HTTPInputData;
/** /**
* Testing HTTPInputData * Testing HTTPInputData
*
* @see HTTPInputData * @see HTTPInputData
*/ */
class HTTPInputDataTest extends MockedTest class HTTPInputDataTest extends MockedTest
@ -35,6 +36,7 @@ class HTTPInputDataTest extends MockedTest
* Returns the data stream for the unit test * Returns the data stream for the unit test
* Each array element of the first hierarchy represents one test run * 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 * Each array element of the second hierarchy represents the parameters, passed to the test function
*
* @return array[] * @return array[]
*/ */
public function dataStream() public function dataStream()
@ -67,16 +69,17 @@ class HTTPInputDataTest extends MockedTest
/** /**
* Tests the HTTPInputData::process() method * 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 string $input The input, we got from the data stream
* @param array $expected The expected output * @param array $expected The expected output
*
* @dataProvider dataStream * @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); HTTPInputDataDouble::setPhpInputContent($input);
$stream = fopen('php://memory', 'r+'); $stream = fopen('php://memory', 'r+');
fwrite($stream, $input); fwrite($stream, $input);