Skip to content

Commit

Permalink
MAGETWO-85534: #8647: [GitHub] Order of how arguments are merged in m…
Browse files Browse the repository at this point in the history
…ultiple di.xml-… #995
  • Loading branch information
ishakhsuvarov authored Dec 13, 2017
2 parents f4e97f3 + d97f5a9 commit f65cf27
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,9 @@ protected function _getRendererClass()
$acceptTypes = [$acceptTypes];
}
foreach ($acceptTypes as $acceptType) {
foreach ($this->_renders as $rendererConfig) {
$rendererType = $rendererConfig['type'];
if ($acceptType == $rendererType || $acceptType == current(
explode('/', $rendererType)
) . '/*' || $acceptType == '*/*'
) {
return $rendererConfig['model'];
}
$renderer = $this->getRendererConfig($acceptType);
if ($renderer !== null) {
return $renderer['model'];
}
}
/** If server does not have renderer for any of the accepted types it SHOULD send 406 (not acceptable). */
Expand All @@ -93,4 +88,30 @@ protected function _getRendererClass()
\Magento\Framework\Webapi\Exception::HTTP_NOT_ACCEPTABLE
);
}

/**
* Get renderer config by accept type.
*
* @param string $acceptType
* @return array|null
*/
private function getRendererConfig($acceptType)
{
// If Accept type = '*/*' then return default renderer.
if ($acceptType == '*/*' && isset($this->_renders['default'])) {
return $this->_renders['default'];
}

foreach ($this->_renders as $rendererConfig) {
$rendererType = $rendererConfig['type'];
if ($acceptType == $rendererType
|| $acceptType == current(explode('/', $rendererType)) . '/*'
|| $acceptType == '*/*'
) {
return $rendererConfig;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ protected function setUp()
)->disableOriginalConstructor()->getMock();

$renders = [
'default' => ['type' => '*/*', 'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class],
'application_xml' => [
'type' => 'application/xml',
'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Xml::class,
],
'application_json' => [
'type' => 'application/json',
'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class,
],
'default' => [
'type' => '*/*',
'model' => \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class
],
];

$this->_factory = new \Magento\Framework\Webapi\Rest\Response\RendererFactory(
Expand All @@ -42,29 +49,43 @@ protected function setUp()

/**
* Test GET method.
*
* @param array $acceptTypes
* @param string $model
* @dataProvider getTestDataProvider
*/
public function testGet()
public function testGet($acceptTypes, $model)
{
$acceptTypes = ['application/json'];

/** Mock request getAcceptTypes method to return specified value. */
$this->_requestMock->expects($this->once())->method('getAcceptTypes')->will($this->returnValue($acceptTypes));
/** Mock renderer. */
$rendererMock = $this->getMockBuilder(
\Magento\Framework\Webapi\Rest\Response\Renderer\Json::class
)->disableOriginalConstructor()->getMock();
$rendererMock = $this->getMockBuilder($model)->disableOriginalConstructor()->getMock();
/** Mock object to return mocked renderer. */
$this->_objectManagerMock->expects(
$this->once()
)->method(
'get'
)->with(
\Magento\Framework\Webapi\Rest\Response\Renderer\Json::class
$model
)->will(
$this->returnValue($rendererMock)
);
$this->_factory->get();
}

/**
* Data provider for method testGet
*
* @return array
*/
public function getTestDataProvider()
{
return [
[['*/*'], \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class],
[['application/json'], \Magento\Framework\Webapi\Rest\Response\Renderer\Json::class],
[['application/xml'], \Magento\Framework\Webapi\Rest\Response\Renderer\Xml::class],
];
}

/**
* Test GET method with wrong Accept HTTP Header.
Expand Down

0 comments on commit f65cf27

Please sign in to comment.