Skip to content

Commit 7b6e2a0

Browse files
Merge pull request #1469 from magento-engcom/develop-prs
[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
2 parents 5973d67 + aa2915b commit 7b6e2a0

File tree

15 files changed

+295
-12
lines changed

15 files changed

+295
-12
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Validate.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ public function __construct(
3636
*/
3737
public function execute()
3838
{
39-
$response = new \Magento\Framework\DataObject();
40-
$response->setError(0);
41-
4239
$resultJson = $this->resultJsonFactory->create();
43-
$resultJson->setData($response);
40+
$resultJson->setData(['error' => 0]);
4441

4542
return $resultJson;
4643
}

app/code/Magento/Customer/Test/Unit/Model/AddressTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function testCustomerId()
8282

8383
public function testCustomer()
8484
{
85-
$this->address->unsetData('cusomer_id');
85+
$this->address->unsetData('customer_id');
8686
$this->assertFalse($this->address->getCustomer());
8787

8888
$this->address->setCustomerId(self::ORIG_CUSTOMER_ID);

app/code/Magento/Ui/Controller/Adminhtml/Index/Render.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,32 @@
99
use Magento\Ui\Controller\Adminhtml\AbstractAction;
1010
use Magento\Framework\View\Element\UiComponentFactory;
1111
use Magento\Framework\View\Element\UiComponentInterface;
12+
use Magento\Ui\Model\UiComponentTypeResolver;
1213

1314
/**
1415
* Class Render
1516
*/
1617
class Render extends AbstractAction
1718
{
19+
/**
20+
* @var \Magento\Ui\Model\UiComponentTypeResolver
21+
*/
22+
private $contentTypeResolver;
23+
24+
/**
25+
* @param Context $context
26+
* @param UiComponentFactory $factory
27+
* @param UiComponentTypeResolver $contentTypeResolver
28+
*/
29+
public function __construct(
30+
Context $context,
31+
UiComponentFactory $factory,
32+
UiComponentTypeResolver $contentTypeResolver
33+
) {
34+
parent::__construct($context, $factory);
35+
$this->contentTypeResolver = $contentTypeResolver;
36+
}
37+
1838
/**
1939
* Action for AJAX request
2040
*
@@ -27,9 +47,12 @@ public function execute()
2747
return;
2848
}
2949

30-
$component = $this->factory->create($this->_request->getParam('namespace'));
50+
$component = $this->factory->create($this->getRequest()->getParam('namespace'));
3151
$this->prepareComponent($component);
32-
$this->_response->appendBody((string) $component->render());
52+
$this->getResponse()->appendBody((string) $component->render());
53+
54+
$contentType = $this->contentTypeResolver->resolve($component->getContext());
55+
$this->getResponse()->setHeader('Content-Type', $contentType, true);
3356
}
3457

3558
/**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Ui\Model;
7+
8+
use Magento\Framework\View\Element\UiComponent\ContextInterface as UiComponentContext;
9+
10+
/**
11+
* Provides correct Content-Type header value for the Ui Component renderer based on the Accept Type of
12+
* the Component Context. Additional types may be added to the type map via di.xml configuration for this resolver.
13+
*
14+
* This is a workaround for the lacking Content-Type processing in
15+
* \Magento\Framework\View\Element\UiComponent\ContentType\ContentTypeInterface
16+
*/
17+
class UiComponentTypeResolver
18+
{
19+
/**
20+
* @var string
21+
*/
22+
const DEFAULT_CONTENT_TYPE = 'text/html';
23+
24+
/**
25+
* @var array
26+
*/
27+
private $uiComponentTypeMap = [];
28+
29+
/**
30+
* @param array $uiComponentTypeMap
31+
*/
32+
public function __construct(array $uiComponentTypeMap)
33+
{
34+
$this->uiComponentTypeMap = $uiComponentTypeMap;
35+
}
36+
37+
/**
38+
* @param UiComponentContext $componentContext
39+
* @return string
40+
*/
41+
public function resolve(UiComponentContext $componentContext): string
42+
{
43+
$acceptType = $componentContext->getAcceptType();
44+
return $this->uiComponentTypeMap[$acceptType] ?? static::DEFAULT_CONTENT_TYPE;
45+
}
46+
}

app/code/Magento/Ui/Test/Unit/Controller/Adminhtml/Index/RenderTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66
namespace Magento\Ui\Test\Unit\Controller\Adminhtml\Index;
77

8-
use \Magento\Ui\Controller\Adminhtml\Index\Render;
8+
use Magento\Ui\Controller\Adminhtml\Index\Render;
9+
use Magento\Ui\Model\UiComponentTypeResolver;
10+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
911

1012
/**
1113
* Class RenderTest
@@ -32,6 +34,11 @@ class RenderTest extends \PHPUnit\Framework\TestCase
3234
*/
3335
protected $uiFactoryMock;
3436

37+
/**
38+
* @var \PHPUnit_Framework_MockObject_MockObject|UiComponentTypeResolver
39+
*/
40+
private $uiComponentTypeResolverMock;
41+
3542
protected function setUp()
3643
{
3744
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
@@ -53,7 +60,11 @@ protected function setUp()
5360
$this->uiFactoryMock = $this->getMockBuilder(\Magento\Framework\View\Element\UiComponentFactory::class)
5461
->disableOriginalConstructor()
5562
->getMock();
56-
$this->render = new Render($contextMock, $this->uiFactoryMock);
63+
64+
$this->uiComponentTypeResolverMock = $this->getMockBuilder(UiComponentTypeResolver::class)
65+
->disableOriginalConstructor()
66+
->getMock();
67+
$this->render = new Render($contextMock, $this->uiFactoryMock, $this->uiComponentTypeResolverMock);
5768
}
5869

5970
public function testExecuteAjaxRequest()
@@ -84,15 +95,22 @@ public function testExecuteAjaxRequest()
8495
true,
8596
['render']
8697
);
98+
$contextMock = $this->createMock(ContextInterface::class);
99+
87100
$viewMock->expects($this->once())
88101
->method('render')
89102
->willReturn($renderedData);
90103
$viewMock->expects($this->once())
91104
->method('getChildComponents')
92105
->willReturn([]);
106+
$viewMock->expects($this->atLeastOnce())->method('getContext')->willReturn($contextMock);
93107
$this->uiFactoryMock->expects($this->once())
94108
->method('create')
95109
->willReturn($viewMock);
110+
$this->uiComponentTypeResolverMock->expects($this->once())->method('resolve')->with($contextMock)
111+
->willReturn('application/json');
112+
$this->responseMock->expects($this->once())->method('setHeader')
113+
->with('Content-Type', 'application/json', true);
96114

97115
$this->render->executeAjaxRequest();
98116
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Ui\Test\Unit\Model;
7+
8+
use Magento\Ui\Model\UiComponentTypeResolver;
9+
use Magento\Framework\View\Element\UiComponent\ContextInterface;
10+
11+
class UiComponentTypeResolverTest extends \PHPUnit\Framework\TestCase
12+
{
13+
/**
14+
* @var UiComponentTypeResolver
15+
*/
16+
private $model;
17+
18+
/**
19+
* @var array
20+
*/
21+
private $contentTypeMap = [];
22+
23+
protected function setUp()
24+
{
25+
$this->contentTypeMap = [
26+
'xml' => 'application/xml',
27+
'json' => 'application/json',
28+
'html' => 'text/html'
29+
];
30+
$this->model = new UiComponentTypeResolver($this->contentTypeMap);
31+
}
32+
33+
/**
34+
* @param string $acceptType
35+
* @param string $contentType
36+
* @dataProvider resolveDataProvider
37+
*/
38+
public function testResolve(string $acceptType, string $contentType)
39+
{
40+
$uiComponentContextMock = $this->createMock(ContextInterface::class);
41+
$uiComponentContextMock->expects($this->atLeastOnce())->method('getAcceptType')->willReturn($acceptType);
42+
43+
$this->assertEquals($contentType, $this->model->resolve($uiComponentContextMock));
44+
}
45+
46+
/**
47+
* @return array
48+
*/
49+
public function resolveDataProvider(): array
50+
{
51+
return [
52+
['json', 'application/json'],
53+
['xml', 'application/xml'],
54+
['html', 'text/html'],
55+
['undefined', UiComponentTypeResolver::DEFAULT_CONTENT_TYPE]
56+
];
57+
}
58+
}

app/code/Magento/Ui/etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,13 @@
426426
</argument>
427427
</arguments>
428428
</type>
429+
<type name="Magento\Ui\Model\UiComponentTypeResolver">
430+
<arguments>
431+
<argument name="uiComponentTypeMap" xsi:type="array">
432+
<item name="html" xsi:type="string">text/html</item>
433+
<item name="json" xsi:type="string">application/json</item>
434+
<item name="xml" xsi:type="string">application/xml</item>
435+
</argument>
436+
</arguments>
437+
</type>
429438
</config>

lib/internal/Magento/Framework/Code/Generator/ClassGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ClassGenerator extends \Zend\Code\Generator\ClassGenerator implements
5959
'type' => 'setType',
6060
'defaultValue' => 'setDefaultValue',
6161
'passedByReference' => 'setPassedByReference',
62+
'variadic' => 'setVariadic',
6263
];
6364

6465
/**

lib/internal/Magento/Framework/Code/Generator/EntityAbstract.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ protected function _getMethodParameterInfo(\ReflectionParameter $parameter)
313313
$parameterInfo = [
314314
'name' => $parameter->getName(),
315315
'passedByReference' => $parameter->isPassedByReference(),
316-
'type' => $parameter->getType()
316+
'type' => $parameter->getType(),
317+
'variadic' => $parameter->isVariadic()
317318
];
318319

319320
if ($parameter->isArray()) {

lib/internal/Magento/Framework/Code/Test/Unit/Generator/ClassGeneratorTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
2020

2121
const FLAG_REFERENCE = 'passedByReference';
2222

23+
const FLAG_VARIADIC = 'variadic';
24+
2325
/**#@-*/
2426

2527
/**
@@ -38,6 +40,7 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
3840
self::FLAG_FINAL => 'isFinal',
3941
self::FLAG_ABSTRACT => 'isAbstract',
4042
self::FLAG_REFERENCE => 'getPassedByReference',
43+
self::FLAG_VARIADIC => 'getVariadic',
4144
];
4245

4346
/**
@@ -65,7 +68,13 @@ class ClassGeneratorTest extends \PHPUnit\Framework\TestCase
6568
'final' => true,
6669
'static' => true,
6770
'parameters' => [
68-
['name' => 'data', 'type' => 'array', 'defaultValue' => [], 'passedByReference' => true],
71+
[
72+
'name' => 'data',
73+
'type' => 'array',
74+
'defaultValue' => [],
75+
'passedByReference' => true,
76+
'variadic' => false
77+
],
6978
],
7079
'body' => 'return 1;',
7180
'docblock' => ['shortDescription' => 'test short description'],
@@ -205,6 +214,9 @@ public function testAddMethods()
205214
$actualDefaultValue = $actualParameter->getDefaultValue();
206215
$this->assertEquals($parameterData['defaultValue'], $actualDefaultValue->getValue());
207216
}
217+
218+
// assert variadic flag
219+
$this->_assertFlag(self::FLAG_VARIADIC, $parameterData, $actualParameter);
208220
}
209221
}
210222

0 commit comments

Comments
 (0)