Skip to content

Commit 651e37c

Browse files
author
Yu Tang
committed
MAGETWO-28256: Bundle Integration API Refactoring
- Remove unnecessary factory call in ServiceInputProcessor
1 parent 8c329ca commit 651e37c

File tree

2 files changed

+9
-55
lines changed

2 files changed

+9
-55
lines changed

lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ protected function _createFromArray($className, $data)
137137
if (is_subclass_of($className, self::EXTENSION_ATTRIBUTES_TYPE)) {
138138
$className = substr($className, 0, -strlen('Interface'));
139139
}
140-
$factory = $this->objectManager->get($className . 'Factory');
141-
$object = $factory->create();
140+
$object = $this->objectManager->create($className);
142141

143142
foreach ($data as $propertyName => $value) {
144143
// Converts snake_case to uppercase CamelCase to help form getter/setter method names

lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public function setUp()
4242
$this->objectManagerMock = $this->getMockBuilder('\Magento\Framework\ObjectManagerInterface')
4343
->disableOriginalConstructor()
4444
->getMock();
45+
$this->objectManagerMock->expects($this->any())
46+
->method('create')
47+
->willReturnCallback(
48+
function ($className) use ($objectManager) {
49+
return $objectManager->getObject($className);
50+
}
51+
);
52+
4553
/** @var \Magento\Framework\Reflection\TypeProcessor $typeProcessor */
4654
$typeProcessor = $objectManager->getObject('Magento\Framework\Reflection\TypeProcessor');
4755
$cache = $this->getMockBuilder('Magento\Framework\App\Cache\Type\Webapi')
@@ -119,12 +127,6 @@ public function testNonExistentPropertiesWithoutDefaultArgumentValue()
119127

120128
public function testNestedDataProperties()
121129
{
122-
$this->setupFactory(
123-
[
124-
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Nested',
125-
'\Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple',
126-
]
127-
);
128130
$data = ['nested' => ['details' => ['entityId' => 15, 'name' => 'Test']]];
129131
$result = $this->serviceInputProcessor->process(
130132
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService',
@@ -167,7 +169,6 @@ public function testSimpleArrayProperties()
167169

168170
public function testAssociativeArrayProperties()
169171
{
170-
$this->setupFactory(['Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple']);
171172
$data = ['associativeArray' => ['key' => 'value', 'key_two' => 'value_two']];
172173
$result = $this->serviceInputProcessor->process(
173174
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService',
@@ -186,7 +187,6 @@ public function testAssociativeArrayProperties()
186187

187188
public function testAssociativeArrayPropertiesWithItem()
188189
{
189-
$this->setupFactory(['Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\AssociativeArray']);
190190
$data = ['associativeArray' => ['item' => 'value']];
191191
$result = $this->serviceInputProcessor->process(
192192
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService',
@@ -204,7 +204,6 @@ public function testAssociativeArrayPropertiesWithItem()
204204

205205
public function testAssociativeArrayPropertiesWithItemArray()
206206
{
207-
$this->setupFactory(['Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\AssociativeArray']);
208207
$data = ['associativeArray' => ['item' => ['value1','value2']]];
209208
$result = $this->serviceInputProcessor->process(
210209
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService',
@@ -223,11 +222,6 @@ public function testAssociativeArrayPropertiesWithItemArray()
223222

224223
public function testArrayOfDataObjectProperties()
225224
{
226-
$this->setupFactory(
227-
[
228-
'\Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple'
229-
]
230-
);
231225
$data = [
232226
'dataObjects' => [
233227
['entityId' => 14, 'name' => 'First'],
@@ -259,7 +253,6 @@ public function testArrayOfDataObjectProperties()
259253

260254
public function testNestedSimpleArrayProperties()
261255
{
262-
$this->setupFactory(['Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\SimpleArray']);
263256
$data = ['arrayData' => ['ids' => [1, 2, 3, 4]]];
264257
$result = $this->serviceInputProcessor->process(
265258
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService',
@@ -281,7 +274,6 @@ public function testNestedSimpleArrayProperties()
281274

282275
public function testNestedAssociativeArrayProperties()
283276
{
284-
$this->setupFactory(['Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\AssociativeArray']);
285277
$data = [
286278
'associativeArrayData' => ['associativeArray' => ['key' => 'value', 'key2' => 'value2']],
287279
];
@@ -305,12 +297,6 @@ public function testNestedAssociativeArrayProperties()
305297

306298
public function testNestedArrayOfDataObjectProperties()
307299
{
308-
$this->setupFactory(
309-
[
310-
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\DataArray',
311-
'\Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple',
312-
]
313-
);
314300
$data = [
315301
'dataObjects' => [
316302
'items' => [['entityId' => 1, 'name' => 'First'], ['entityId' => 2, 'name' => 'Second']],
@@ -352,14 +338,6 @@ public function testNestedArrayOfDataObjectProperties()
352338
*/
353339
public function testCustomAttributesProperties($customAttributeType, $inputData, $expectedObject)
354340
{
355-
$this->setupFactory(
356-
[
357-
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\ObjectWithCustomAttributes',
358-
'\Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple',
359-
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple',
360-
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\SimpleArray',
361-
]
362-
);
363341
$this->customAttributeTypeLocator->expects($this->any())->method('getType')->willReturn($customAttributeType);
364342

365343
$result = $this->serviceInputProcessor->process(
@@ -521,27 +499,4 @@ protected function getObjectWithCustomAttributes($type, $value = [])
521499
]]
522500
);
523501
}
524-
protected function setupFactory(array $classNames)
525-
{
526-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
527-
528-
$returnValueMap = [];
529-
foreach ($classNames as $className) {
530-
$factoryMock = $this->getMockBuilder($className . 'Factory')
531-
->setMethods(['create'])
532-
->disableOriginalConstructor()
533-
->getMock();
534-
$factoryMock->expects($this->any())
535-
->method('create')
536-
->willReturnCallback(
537-
function () use ($objectManager, $className) {
538-
return $objectManager->getObject($className);
539-
}
540-
);
541-
$returnValueMap[] = [$className . 'Factory', $factoryMock];
542-
}
543-
$this->objectManagerMock->expects($this->any())
544-
->method('get')
545-
->will($this->returnValueMap($returnValueMap));
546-
}
547502
}

0 commit comments

Comments
 (0)