Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #78 from webimpress/hotfix/76
Browse files Browse the repository at this point in the history
Hotfix #76 - Parsing string data
  • Loading branch information
weierophinney committed Oct 11, 2016
2 parents b46b247 + addefbf commit 17542ea
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
7 changes: 0 additions & 7 deletions src/ContentTypeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,7 @@ public function __invoke(MvcEvent $e)
break;
}

// Stolen from AbstractRestfulController
parse_str($content, $bodyParams);
if (!is_array($bodyParams)
|| (1 == count($bodyParams) && isset($bodyParams[0]))
) {
$bodyParams = $content;
}
break;
default:
break;
Expand All @@ -117,7 +111,6 @@ public function __invoke(MvcEvent $e)
return $bodyParams;
}

$bodyParams = $bodyParams ?: [];
$parameterData->setBodyParams($bodyParams);
$e->setParam('ZFContentNegotiationParameterData', $parameterData);
}
Expand Down
45 changes: 45 additions & 0 deletions test/ContentTypeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,49 @@ public function testMergesHalEmbeddedPropertiesIntoTopLevelObjectWhenDecodingHal

$this->assertEquals($expected, $params->getBodyParams());
}

public function methodsWithStringContent()
{
return [
'delete-string' => ['DELETE', 'String Content', 'String_Content'],
'delete-zero-key' => ['DELETE', '0=', 0],
'delete-empty-value' => ['DELETE', 'ids=', 'ids'],
'patch-string' => ['PATCH', '@String-Content', '@String-Content'],
'patch-zero-key' => ['PATCH', '0=', 0],
'patch-empty-value' => ['PATCH', 'name=', 'name'],
'put-string' => ['PUT', 'string.content', 'string_content'],
'put-zero-key' => ['PUT', '0=', 0],
'put-empty-value' => ['PUT', 'key=', 'key'],
];
}

/**
* @dataProvider methodsWithStringContent
*
* @param string $method
* @param string $data
* @param string $key
*/
public function testStringContentIsParsedCorrectlyToAnArray($method, $data, $key)
{
$listener = $this->listener;

$request = new Request();
$request->setMethod($method);
$request->setContent($data);

$event = new MvcEvent();
$event->setRequest($request);
$event->setRouteMatch($this->createRouteMatch([]));

$result = $listener($event);
$this->assertNull($result);

/** @var \ZF\ContentNegotiation\ParameterDataContainer $params */
$params = $event->getParam('ZFContentNegotiationParameterData');
$array = $params->getBodyParams();

$this->assertArrayHasKey($key, $array);
$this->assertSame('', $array[$key]);
}
}

0 comments on commit 17542ea

Please sign in to comment.