Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ServiceInputProcessorTest.php
Go to the documentation of this file.
1 <?php
8 
13 use Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\WebapiBuilderFactory;
17 use Magento\Webapi\Test\Unit\Service\Entity\DataArrayData;
19 use Magento\Webapi\Test\Unit\Service\Entity\NestedData;
22 use Magento\Webapi\Test\Unit\Service\Entity\SimpleArrayData;
23 use Magento\Webapi\Test\Unit\Service\Entity\SimpleData;
25 
29 class ServiceInputProcessorTest extends \PHPUnit\Framework\TestCase
30 {
33 
36 
39 
41  protected $objectManagerMock;
42 
44  protected $methodsMap;
45 
47  protected $fieldNamer;
48 
52  private $serviceTypeToEntityTypeMap;
53 
54  protected function setUp()
55  {
56  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
57  $this->objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->objectManagerMock->expects($this->any())
61  ->method('create')
62  ->willReturnCallback(
63  function ($className, $arguments = []) use ($objectManager) {
64  return $objectManager->getObject($className, $arguments);
65  }
66  );
67 
69  $typeProcessor = $objectManager->getObject(\Magento\Framework\Reflection\TypeProcessor::class);
70  $cache = $this->getMockBuilder(\Magento\Framework\App\Cache\Type\Reflection::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $cache->expects($this->any())->method('load')->willReturn(false);
74 
75  $this->customAttributeTypeLocator = $this->getMockBuilder(
76  \Magento\Eav\Model\TypeLocator::class
77  )
78  ->disableOriginalConstructor()
79  ->getMock();
80 
82  $this->attributeValueFactoryMock = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85  $this->attributeValueFactoryMock->expects($this->any())
86  ->method('create')
87  ->willReturnCallback(
88  function () use ($objectManager) {
89  return $objectManager->getObject(\Magento\Framework\Api\AttributeValue::class);
90  }
91  );
92 
93  $this->fieldNamer = $this->getMockBuilder(\Magento\Framework\Reflection\FieldNamer::class)
94  ->disableOriginalConstructor()
95  ->setMethods([])
96  ->getMock();
97 
98  $this->methodsMap = $objectManager->getObject(
99  \Magento\Framework\Reflection\MethodsMap::class,
100  [
101  'cache' => $cache,
102  'typeProcessor' => $typeProcessor,
103  'attributeTypeResolver' => $this->attributeValueFactoryMock->create(),
104  'fieldNamer' => $this->fieldNamer
105  ]
106  );
107  $serializerMock = $this->createMock(SerializerInterface::class);
108  $serializerMock->method('serialize')
109  ->willReturn('serializedData');
110  $serializerMock->method('unserialize')
111  ->willReturn('unserializedData');
112  $objectManager->setBackwardCompatibleProperty(
113  $this->methodsMap,
114  'serializer',
115  $serializerMock
116  );
117  $this->serviceTypeToEntityTypeMap = $this->getMockBuilder(ServiceTypeToEntityTypeMap::class)
118  ->disableOriginalConstructor()
119  ->getMock();
120 
121  $this->serviceInputProcessor = $objectManager->getObject(
122  \Magento\Framework\Webapi\ServiceInputProcessor::class,
123  [
124  'typeProcessor' => $typeProcessor,
125  'objectManager' => $this->objectManagerMock,
126  'customAttributeTypeLocator' => $this->customAttributeTypeLocator,
127  'attributeValueFactory' => $this->attributeValueFactoryMock,
128  'methodsMap' => $this->methodsMap,
129  'serviceTypeToEntityTypeMap' => $this->serviceTypeToEntityTypeMap
130  ]
131  );
132 
134  $nameFinder = $objectManager->getObject(\Magento\Framework\Reflection\NameFinder::class);
135  $objectManager->setBackwardCompatibleProperty(
136  $this->serviceInputProcessor,
137  'nameFinder',
138  $nameFinder
139  );
140  }
141 
142  public function testSimpleProperties()
143  {
144  $data = ['entityId' => 15, 'name' => 'Test'];
145  $result = $this->serviceInputProcessor->process(
146  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
147  'simple',
148  $data
149  );
150  $this->assertNotNull($result);
151  $this->assertEquals(15, $result[0]);
152  $this->assertEquals('Test', $result[1]);
153  }
154 
156  {
157  $data = [];
158  $result = $this->serviceInputProcessor->process(
159  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
160  'simpleDefaultValue',
161  $data
162  );
163  $this->assertNotNull($result);
164  $this->assertEquals(TestService::DEFAULT_VALUE, $result[0]);
165  }
166 
172  {
173  $data = [];
174  $result = $this->serviceInputProcessor->process(
175  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
176  'simple',
177  $data
178  );
179  $this->assertNull($result);
180  }
181 
182  public function testNestedDataProperties()
183  {
184  $data = ['nested' => ['details' => ['entityId' => 15, 'name' => 'Test']]];
185  $result = $this->serviceInputProcessor->process(
186  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
187  'nestedData',
188  $data
189  );
190  $this->assertNotNull($result);
191  $this->assertTrue($result[0] instanceof Nested);
193  $this->assertEquals(1, count($result));
194  $this->assertNotEmpty($result[0]);
196  $arg = $result[0];
197  $this->assertTrue($arg instanceof Nested);
199  $details = $arg->getDetails();
200  $this->assertNotNull($details);
201  $this->assertTrue($details instanceof Simple);
202  $this->assertEquals(15, $details->getEntityId());
203  $this->assertEquals('Test', $details->getName());
204  }
205 
207  {
208  $data = ['simpleConstructor' => ['entityId' => 15, 'name' => 'Test']];
209  $result = $this->serviceInputProcessor->process(
210  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
211  'simpleConstructor',
212  $data
213  );
214  $this->assertNotNull($result);
215  $arg = $result[0];
216 
217  $this->assertTrue($arg instanceof SimpleConstructor);
218  $this->assertEquals(15, $arg->getEntityId());
219  $this->assertEquals('Test', $arg->getName());
220  }
221 
222  public function testSimpleArrayProperties()
223  {
224  $data = ['ids' => [1, 2, 3, 4]];
225  $result = $this->serviceInputProcessor->process(
226  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
227  'simpleArray',
228  $data
229  );
230  $this->assertNotNull($result);
232  $this->assertEquals(1, count($result));
234  $ids = $result[0];
235  $this->assertNotNull($ids);
236  $this->assertEquals(4, count($ids));
237  $this->assertEquals($data['ids'], $ids);
238  }
239 
240  public function testAssociativeArrayProperties()
241  {
242  $data = ['associativeArray' => ['key' => 'value', 'key_two' => 'value_two']];
243  $result = $this->serviceInputProcessor->process(
244  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
245  'associativeArray',
246  $data
247  );
248  $this->assertNotNull($result);
250  $this->assertEquals(1, count($result));
252  $associativeArray = $result[0];
253  $this->assertNotNull($associativeArray);
254  $this->assertEquals('value', $associativeArray['key']);
255  $this->assertEquals('value_two', $associativeArray['key_two']);
256  }
257 
258  public function testAssociativeArrayPropertiesWithItem()
259  {
260  $data = ['associativeArray' => ['item' => 'value']];
261  $result = $this->serviceInputProcessor->process(
262  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
263  'associativeArray',
264  $data
265  );
266  $this->assertNotNull($result);
268  $this->assertEquals(1, count($result));
270  $associativeArray = $result[0];
271  $this->assertNotNull($associativeArray);
272  $this->assertEquals('value', $associativeArray[0]);
273  }
274 
275  public function testAssociativeArrayPropertiesWithItemArray()
276  {
277  $data = ['associativeArray' => ['item' => ['value1','value2']]];
278  $result = $this->serviceInputProcessor->process(
279  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
280  'associativeArray',
281  $data
282  );
283  $this->assertNotNull($result);
285  $this->assertEquals(1, count($result));
287  $array = $result[0];
288  $this->assertNotNull($array);
289  $this->assertEquals('value1', $array[0]);
290  $this->assertEquals('value2', $array[1]);
291  }
292 
293  public function testArrayOfDataObjectProperties()
294  {
295  $data = [
296  'dataObjects' => [
297  ['entityId' => 14, 'name' => 'First'],
298  ['entityId' => 15, 'name' => 'Second'],
299  ],
300  ];
301  $result = $this->serviceInputProcessor->process(
302  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
303  'dataArray',
304  $data
305  );
306  $this->assertNotNull($result);
308  $this->assertEquals(1, count($result));
310  $dataObjects = $result[0];
311  $this->assertEquals(2, count($dataObjects));
313  $first = $dataObjects[0];
315  $second = $dataObjects[1];
316  $this->assertTrue($first instanceof Simple);
317  $this->assertEquals(14, $first->getEntityId());
318  $this->assertEquals('First', $first->getName());
319  $this->assertTrue($second instanceof Simple);
320  $this->assertEquals(15, $second->getEntityId());
321  $this->assertEquals('Second', $second->getName());
322  }
323 
324  public function testNestedSimpleArrayProperties()
325  {
326  $data = ['arrayData' => ['ids' => [1, 2, 3, 4]]];
327  $result = $this->serviceInputProcessor->process(
328  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
329  'nestedSimpleArray',
330  $data
331  );
332  $this->assertNotNull($result);
334  $this->assertEquals(1, count($result));
336  $dataObject = $result[0];
337  $this->assertTrue($dataObject instanceof SimpleArray);
339  $ids = $dataObject->getIds();
340  $this->assertNotNull($ids);
341  $this->assertEquals(4, count($ids));
342  $this->assertEquals($data['arrayData']['ids'], $ids);
343  }
344 
345  public function testNestedAssociativeArrayProperties()
346  {
347  $data = [
348  'associativeArrayData' => ['associativeArray' => ['key' => 'value', 'key2' => 'value2']],
349  ];
350  $result = $this->serviceInputProcessor->process(
351  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
352  'nestedAssociativeArray',
353  $data
354  );
355  $this->assertNotNull($result);
357  $this->assertEquals(1, count($result));
359  $dataObject = $result[0];
360  $this->assertTrue($dataObject instanceof AssociativeArray);
362  $associativeArray = $dataObject->getAssociativeArray();
363  $this->assertNotNull($associativeArray);
364  $this->assertEquals('value', $associativeArray['key']);
365  $this->assertEquals('value2', $associativeArray['key2']);
366  }
367 
368  public function testNestedArrayOfDataObjectProperties()
369  {
370  $data = [
371  'dataObjects' => [
372  'items' => [['entityId' => 1, 'name' => 'First'], ['entityId' => 2, 'name' => 'Second']],
373  ],
374  ];
375  $result = $this->serviceInputProcessor->process(
376  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
377  'nestedDataArray',
378  $data
379  );
380  $this->assertNotNull($result);
382  $this->assertEquals(1, count($result));
384  $dataObjects = $result[0];
385  $this->assertTrue($dataObjects instanceof DataArray);
387  $items = $dataObjects->getItems();
388  $this->assertEquals(2, count($items));
390  $first = $items[0];
392  $second = $items[1];
393  $this->assertTrue($first instanceof Simple);
394  $this->assertEquals(1, $first->getEntityId());
395  $this->assertEquals('First', $first->getName());
396  $this->assertTrue($second instanceof Simple);
397  $this->assertEquals(2, $second->getEntityId());
398  $this->assertEquals('Second', $second->getName());
399  }
400 
409  public function testCustomAttributesProperties($customAttributeType, $inputData, $expectedObject)
410  {
411  $this->customAttributeTypeLocator->expects($this->any())->method('getType')->willReturn($customAttributeType);
412  $this->serviceTypeToEntityTypeMap->expects($this->any())->method('getEntityType')->willReturn($expectedObject);
413 
414  $result = $this->serviceInputProcessor->process(
415  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
416  'ObjectWithCustomAttributesMethod',
417  $inputData
418  );
419 
420  $this->assertTrue($result[0] instanceof ObjectWithCustomAttributes);
421  $this->assertEquals($expectedObject, $result[0]);
422  }
423 
430  {
431  return [
432  'customAttributeInteger' => [
433  'customAttributeType' => 'integer',
434  'inputData' => [
435  'param' => [
436  'customAttributes' => [
437  [
438  'attribute_code' => TestService::CUSTOM_ATTRIBUTE_CODE,
439  'value' => TestService::DEFAULT_VALUE
440  ]
441  ]
442  ]
443  ],
444  'expectedObject'=> $this->getObjectWithCustomAttributes('integer', TestService::DEFAULT_VALUE),
445  ],
446  'customAttributeIntegerCamelCaseCode' => [
447  'customAttributeType' => 'integer',
448  'inputData' => [
449  'param' => [
450  'customAttributes' => [
451  [
452  'attributeCode' => TestService::CUSTOM_ATTRIBUTE_CODE,
453  'value' => TestService::DEFAULT_VALUE
454  ]
455  ]
456  ]
457  ],
458  'expectedObject'=> $this->getObjectWithCustomAttributes('integer', TestService::DEFAULT_VALUE),
459  ],
460  'customAttributeObject' => [
461  'customAttributeType' => \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\SimpleArray::class,
462  'inputData' => [
463  'param' => [
464  'customAttributes' => [
465  ['attribute_code' => TestService::CUSTOM_ATTRIBUTE_CODE, 'value' => ['ids' => [1, 2, 3, 4]]]
466  ]
467  ]
468  ],
469  'expectedObject'=> $this->getObjectWithCustomAttributes('SimpleArray', ['ids' => [1, 2, 3, 4]]),
470  ],
471  'customAttributeArrayOfObjects' => [
472  'customAttributeType' => 'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple[]',
473  'inputData' => [
474  'param' => [
475  'customAttributes' => [
476  ['attribute_code' => TestService::CUSTOM_ATTRIBUTE_CODE, 'value' => [
477  ['entityId' => 14, 'name' => 'First'],
478  ['entityId' => 15, 'name' => 'Second'],
479  ]]
480  ]
481  ]
482  ],
483  'expectedObject'=> $this->getObjectWithCustomAttributes('Simple[]', [
484  ['entityId' => 14, 'name' => 'First'],
485  ['entityId' => 15, 'name' => 'Second'],
486  ]),
487  ],
488  ];
489  }
490 
498  protected function getObjectWithCustomAttributes($type, $value = [])
499  {
500  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
501  $customAttributeValue = null;
502  switch ($type) {
503  case 'integer':
504  $customAttributeValue = $value;
505  break;
506  case 'SimpleArray':
507  $customAttributeValue = $objectManager->getObject(
508  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\SimpleArray::class,
509  ['data' => $value]
510  );
511  break;
512  case 'Simple[]':
513  $dataObjectSimple1 = $objectManager->getObject(
514  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple::class,
515  ['data' => $value[0]]
516  );
517  $dataObjectSimple2 = $objectManager->getObject(
518  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple::class,
519  ['data' => $value[1]]
520  );
521  $customAttributeValue = [$dataObjectSimple1, $dataObjectSimple2];
522  break;
523  case 'emptyData':
524  return $objectManager->getObject(
525  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\ObjectWithCustomAttributes::class,
526  ['data' => []]
527  );
528  default:
529  return null;
530  }
531  return $objectManager->getObject(
532  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\ObjectWithCustomAttributes::class,
533  ['data' => [
534  'custom_attributes' => [
536  \Magento\Framework\Api\AttributeValue::class,
537  ['data' =>
538  [
539  'attribute_code' => TestService::CUSTOM_ATTRIBUTE_CODE,
540  'value' => $customAttributeValue
541  ]
542  ]
543  )
544  ]
545  ]]
546  );
547  }
548 
555  public function testCustomAttributesExceptions($inputData)
556  {
557  $this->serviceInputProcessor->process(
558  \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
559  'ObjectWithCustomAttributesMethod',
560  $inputData
561  );
562  }
563 
568  {
569  return [
570  [
571  'inputData' => [
572  'param' => [
573  'customAttributes' => [
574  []
575  ]
576  ]
577  ]
578  ],
579  [
580  'inputData' => [
581  'param' => [
582  'customAttributes' => [
583  [
584  'value' => TestService::DEFAULT_VALUE
585  ]
586  ]
587  ]
588  ]
589  ],
590  [
591  'inputData' => [
592  'param' => [
593  'customAttributes' => [
594  [
595  'attribute_code' => TestService::CUSTOM_ATTRIBUTE_CODE,
596  ]
597  ]
598  ]
599  ]
600  ]
601  ];
602  }
603 }
$objectManager
Definition: bootstrap.php:17
testCustomAttributesProperties($customAttributeType, $inputData, $expectedObject)
$details
Definition: vault.phtml:10
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$arguments
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
$items