Skip to content

Commit

Permalink
Merge pull request #1469 from magento-engcom/develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests
 - MAGETWO-72390: Remove the usage of the DataObject for response management #10808
 - MAGETWO-71545: Added 'application/json' Content-Type to Ajax responses in the Magento_UI module. #10521
 - MAGETWO-72388: Fix spelling mistake in AddressTest.php #10806
 - MAGETWO-72283: Code generate: support variadic parameter #10771
  • Loading branch information
ishakhsuvarov authored Sep 8, 2017
2 parents 5973d67 + aa2915b commit 7b6e2a0
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public function __construct(
*/
public function execute()
{
$response = new \Magento\Framework\DataObject();
$response->setError(0);

$resultJson = $this->resultJsonFactory->create();
$resultJson->setData($response);
$resultJson->setData(['error' => 0]);

return $resultJson;
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Customer/Test/Unit/Model/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testCustomerId()

public function testCustomer()
{
$this->address->unsetData('cusomer_id');
$this->address->unsetData('customer_id');
$this->assertFalse($this->address->getCustomer());

$this->address->setCustomerId(self::ORIG_CUSTOMER_ID);
Expand Down
27 changes: 25 additions & 2 deletions app/code/Magento/Ui/Controller/Adminhtml/Index/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@
use Magento\Ui\Controller\Adminhtml\AbstractAction;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Ui\Model\UiComponentTypeResolver;

/**
* Class Render
*/
class Render extends AbstractAction
{
/**
* @var \Magento\Ui\Model\UiComponentTypeResolver
*/
private $contentTypeResolver;

/**
* @param Context $context
* @param UiComponentFactory $factory
* @param UiComponentTypeResolver $contentTypeResolver
*/
public function __construct(
Context $context,
UiComponentFactory $factory,
UiComponentTypeResolver $contentTypeResolver
) {
parent::__construct($context, $factory);
$this->contentTypeResolver = $contentTypeResolver;
}

/**
* Action for AJAX request
*
Expand All @@ -27,9 +47,12 @@ public function execute()
return;
}

$component = $this->factory->create($this->_request->getParam('namespace'));
$component = $this->factory->create($this->getRequest()->getParam('namespace'));
$this->prepareComponent($component);
$this->_response->appendBody((string) $component->render());
$this->getResponse()->appendBody((string) $component->render());

$contentType = $this->contentTypeResolver->resolve($component->getContext());
$this->getResponse()->setHeader('Content-Type', $contentType, true);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions app/code/Magento/Ui/Model/UiComponentTypeResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Model;

use Magento\Framework\View\Element\UiComponent\ContextInterface as UiComponentContext;

/**
* Provides correct Content-Type header value for the Ui Component renderer based on the Accept Type of
* the Component Context. Additional types may be added to the type map via di.xml configuration for this resolver.
*
* This is a workaround for the lacking Content-Type processing in
* \Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeInterface
*/
class UiComponentTypeResolver
{
/**
* @var string
*/
const DEFAULT_CONTENT_TYPE = 'text/html';

/**
* @var array
*/
private $uiComponentTypeMap = [];

/**
* @param array $uiComponentTypeMap
*/
public function __construct(array $uiComponentTypeMap)
{
$this->uiComponentTypeMap = $uiComponentTypeMap;
}

/**
* @param UiComponentContext $componentContext
* @return string
*/
public function resolve(UiComponentContext $componentContext): string
{
$acceptType = $componentContext->getAcceptType();
return $this->uiComponentTypeMap[$acceptType] ?? static::DEFAULT_CONTENT_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
namespace Magento\Ui\Test\Unit\Controller\Adminhtml\Index;

use \Magento\Ui\Controller\Adminhtml\Index\Render;
use Magento\Ui\Controller\Adminhtml\Index\Render;
use Magento\Ui\Model\UiComponentTypeResolver;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

/**
* Class RenderTest
Expand All @@ -32,6 +34,11 @@ class RenderTest extends \PHPUnit\Framework\TestCase
*/
protected $uiFactoryMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|UiComponentTypeResolver
*/
private $uiComponentTypeResolverMock;

protected function setUp()
{
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
Expand All @@ -53,7 +60,11 @@ protected function setUp()
$this->uiFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
->disableOriginalConstructor()
->getMock();
$this->render = new Render($contextMock, $this->uiFactoryMock);

$this->uiComponentTypeResolverMock = $this->getMockBuilder(UiComponentTypeResolver::class)
->disableOriginalConstructor()
->getMock();
$this->render = new Render($contextMock, $this->uiFactoryMock, $this->uiComponentTypeResolverMock);
}

public function testExecuteAjaxRequest()
Expand Down Expand Up @@ -84,15 +95,22 @@ public function testExecuteAjaxRequest()
true,
['render']
);
$contextMock = $this->createMock(ContextInterface::class);

$viewMock->expects($this->once())
->method('render')
->willReturn($renderedData);
$viewMock->expects($this->once())
->method('getChildComponents')
->willReturn([]);
$viewMock->expects($this->atLeastOnce())->method('getContext')->willReturn($contextMock);
$this->uiFactoryMock->expects($this->once())
->method('create')
->willReturn($viewMock);
$this->uiComponentTypeResolverMock->expects($this->once())->method('resolve')->with($contextMock)
->willReturn('application/json');
$this->responseMock->expects($this->once())->method('setHeader')
->with('Content-Type', 'application/json', true);

$this->render->executeAjaxRequest();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Test\Unit\Model;

use Magento\Ui\Model\UiComponentTypeResolver;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

class UiComponentTypeResolverTest extends \PHPUnit\Framework\TestCase
{
/**
* @var UiComponentTypeResolver
*/
private $model;

/**
* @var array
*/
private $contentTypeMap = [];

protected function setUp()
{
$this->contentTypeMap = [
'xml' => 'application/xml',
'json' => 'application/json',
'html' => 'text/html'
];
$this->model = new UiComponentTypeResolver($this->contentTypeMap);
}

/**
* @param string $acceptType
* @param string $contentType
* @dataProvider resolveDataProvider
*/
public function testResolve(string $acceptType, string $contentType)
{
$uiComponentContextMock = $this->createMock(ContextInterface::class);
$uiComponentContextMock->expects($this->atLeastOnce())->method('getAcceptType')->willReturn($acceptType);

$this->assertEquals($contentType, $this->model->resolve($uiComponentContextMock));
}

/**
* @return array
*/
public function resolveDataProvider(): array
{
return [
['json', 'application/json'],
['xml', 'application/xml'],
['html', 'text/html'],
['undefined', UiComponentTypeResolver::DEFAULT_CONTENT_TYPE]
];
}
}
9 changes: 9 additions & 0 deletions app/code/Magento/Ui/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,13 @@
</argument>
</arguments>
</type>
<type name="Magento\Ui\Model\UiComponentTypeResolver">
<arguments>
<argument name="uiComponentTypeMap" xsi:type="array">
<item name="html" xsi:type="string">text/html</item>
<item name="json" xsi:type="string">application/json</item>
<item name="xml" xsi:type="string">application/xml</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ClassGenerator extends \Zend\Code\Generator\ClassGenerator implements
'type' => 'setType',
'defaultValue' => 'setDefaultValue',
'passedByReference' => 'setPassedByReference',
'variadic' => 'setVariadic',
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
$parameterInfo = [
'name' => $parameter->getName(),
'passedByReference' => $parameter->isPassedByReference(),
'type' => $parameter->getType()
'type' => $parameter->getType(),
'variadic' => $parameter->isVariadic()
];

if ($parameter->isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase

const FLAG_REFERENCE = 'passedByReference';

const FLAG_VARIADIC = 'variadic';

/**#@-*/

/**
Expand All @@ -38,6 +40,7 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
self::FLAG_FINAL => 'isFinal',
self::FLAG_ABSTRACT => 'isAbstract',
self::FLAG_REFERENCE => 'getPassedByReference',
self::FLAG_VARIADIC => 'getVariadic',
];

/**
Expand Down Expand Up @@ -65,7 +68,13 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
'final' => true,
'static' => true,
'parameters' => [
['name' => 'data', 'type' => 'array', 'defaultValue' => [], 'passedByReference' => true],
[
'name' => 'data',
'type' => 'array',
'defaultValue' => [],
'passedByReference' => true,
'variadic' => false
],
],
'body' => 'return 1;',
'docblock' => ['shortDescription' => 'test short description'],
Expand Down Expand Up @@ -205,6 +214,9 @@ public function testAddMethods()
$actualDefaultValue = $actualParameter->getDefaultValue();
$this->assertEquals($parameterData['defaultValue'], $actualDefaultValue->getValue());
}

// assert variadic flag
$this->_assertFlag(self::FLAG_VARIADIC, $parameterData, $actualParameter);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ protected function _getParameterList(array $parameters)
', ',
array_map(
function ($item) {
return "$" . $item['name'];
$output = '';
if ($item['variadic']) {
$output .= '... ';
}

$output .= "\${$item['name']}";
return $output;
},
$parameters
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,43 @@ class Interceptor extends \Magento\Framework\Interception\Code\Generator\Sample
return $this->___callPlugins('getReference', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function firstVariadicParameter(... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'firstVariadicParameter');
if (!$pluginInfo) {
return parent::firstVariadicParameter(... $variadicValue);
} else {
return $this->___callPlugins('firstVariadicParameter', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function secondVariadicParameter($value, ... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'secondVariadicParameter');
if (!$pluginInfo) {
return parent::secondVariadicParameter($value, ... $variadicValue);
} else {
return $this->___callPlugins('secondVariadicParameter', func_get_args(), $pluginInfo);
}
}

/**
* {@inheritdoc}
*/
public function byRefVariadic(&... $variadicValue)
{
$pluginInfo = $this->pluginList->getNext($this->subjectType, 'byRefVariadic');
if (!$pluginInfo) {
return parent::byRefVariadic(... $variadicValue);
} else {
return $this->___callPlugins('byRefVariadic', func_get_args(), $pluginInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Sample
{
private $attribute;
private $variadicAttribute;

public function getValue()
{
Expand All @@ -22,4 +23,20 @@ public function setValue($value)
public function & getReference()
{
}

public function firstVariadicParameter(...$variadicValue)
{
$this->variadicAttribute = $variadicValue;
}

public function secondVariadicParameter($value, ...$variadicValue)
{
$this->attribute = $value;
$this->variadicAttribute = $variadicValue;
}

public function byRefVariadic(& ...$variadicValue)
{
$this->variadicAttribute = $variadicValue;
}
}
Loading

0 comments on commit 7b6e2a0

Please sign in to comment.