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;
52 private $serviceTypeToEntityTypeMap;
54 protected function setUp()
56 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
57 $this->objectManagerMock = $this->getMockBuilder(\
Magento\Framework\ObjectManagerInterface::class)
58 ->disableOriginalConstructor()
60 $this->objectManagerMock->expects($this->any())
70 $cache = $this->getMockBuilder(\
Magento\Framework\
App\Cache\Type\Reflection::class)
71 ->disableOriginalConstructor()
73 $cache->expects($this->any())->method(
'load')->willReturn(
false);
75 $this->customAttributeTypeLocator = $this->getMockBuilder(
76 \
Magento\Eav\Model\TypeLocator::class
78 ->disableOriginalConstructor()
82 $this->attributeValueFactoryMock = $this->getMockBuilder(\
Magento\Framework\Api\AttributeValueFactory::class)
83 ->disableOriginalConstructor()
85 $this->attributeValueFactoryMock->expects($this->any())
93 $this->fieldNamer = $this->getMockBuilder(\
Magento\Framework\Reflection\FieldNamer::class)
94 ->disableOriginalConstructor()
99 \
Magento\Framework\Reflection\MethodsMap::class,
102 'typeProcessor' => $typeProcessor,
103 'attributeTypeResolver' => $this->attributeValueFactoryMock->create(),
107 $serializerMock = $this->createMock(SerializerInterface::class);
108 $serializerMock->method(
'serialize')
109 ->willReturn(
'serializedData');
110 $serializerMock->method(
'unserialize')
111 ->willReturn(
'unserializedData');
117 $this->serviceTypeToEntityTypeMap = $this->getMockBuilder(ServiceTypeToEntityTypeMap::class)
118 ->disableOriginalConstructor()
122 \
Magento\Framework\Webapi\ServiceInputProcessor::class,
124 'typeProcessor' => $typeProcessor,
125 'objectManager' => $this->objectManagerMock,
126 'customAttributeTypeLocator' => $this->customAttributeTypeLocator,
127 'attributeValueFactory' => $this->attributeValueFactoryMock,
128 'methodsMap' => $this->methodsMap,
129 'serviceTypeToEntityTypeMap' => $this->serviceTypeToEntityTypeMap
136 $this->serviceInputProcessor,
144 $data = [
'entityId' => 15,
'name' =>
'Test'];
145 $result = $this->serviceInputProcessor->process(
151 $this->assertEquals(15,
$result[0]);
152 $this->assertEquals(
'Test',
$result[1]);
158 $result = $this->serviceInputProcessor->process(
160 'simpleDefaultValue',
174 $result = $this->serviceInputProcessor->process(
182 public function testNestedDataProperties()
184 $data = [
'nested' => [
'details' => [
'entityId' => 15,
'name' =>
'Test']]];
185 $result = $this->serviceInputProcessor->process(
193 $this->assertEquals(1, count(
$result));
194 $this->assertNotEmpty(
$result[0]);
197 $this->assertTrue($arg instanceof
Nested);
202 $this->assertEquals(15,
$details->getEntityId());
203 $this->assertEquals(
'Test',
$details->getName());
208 $data = [
'simpleConstructor' => [
'entityId' => 15,
'name' =>
'Test']];
209 $result = $this->serviceInputProcessor->process(
218 $this->assertEquals(15, $arg->getEntityId());
219 $this->assertEquals(
'Test', $arg->getName());
222 public function testSimpleArrayProperties()
224 $data = [
'ids' => [1, 2, 3, 4]];
225 $result = $this->serviceInputProcessor->process(
232 $this->assertEquals(1, count(
$result));
235 $this->assertNotNull($ids);
236 $this->assertEquals(4, count($ids));
237 $this->assertEquals(
$data[
'ids'], $ids);
240 public function testAssociativeArrayProperties()
242 $data = [
'associativeArray' => [
'key' =>
'value',
'key_two' =>
'value_two']];
243 $result = $this->serviceInputProcessor->process(
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']);
258 public function testAssociativeArrayPropertiesWithItem()
260 $data = [
'associativeArray' => [
'item' =>
'value']];
261 $result = $this->serviceInputProcessor->process(
268 $this->assertEquals(1, count(
$result));
270 $associativeArray =
$result[0];
271 $this->assertNotNull($associativeArray);
272 $this->assertEquals(
'value', $associativeArray[0]);
275 public function testAssociativeArrayPropertiesWithItemArray()
277 $data = [
'associativeArray' => [
'item' => [
'value1',
'value2']]];
278 $result = $this->serviceInputProcessor->process(
279 \
Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
285 $this->assertEquals(1, count(
$result));
288 $this->assertNotNull($array);
289 $this->assertEquals(
'value1', $array[0]);
290 $this->assertEquals(
'value2', $array[1]);
293 public function testArrayOfDataObjectProperties()
297 [
'entityId' => 14,
'name' =>
'First'],
298 [
'entityId' => 15,
'name' =>
'Second'],
301 $result = $this->serviceInputProcessor->process(
302 \
Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
308 $this->assertEquals(1, count(
$result));
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());
324 public function testNestedSimpleArrayProperties()
326 $data = [
'arrayData' => [
'ids' => [1, 2, 3, 4]]];
327 $result = $this->serviceInputProcessor->process(
328 \
Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
334 $this->assertEquals(1, count(
$result));
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);
345 public function testNestedAssociativeArrayProperties()
348 'associativeArrayData' => [
'associativeArray' => [
'key' =>
'value',
'key2' =>
'value2']],
350 $result = $this->serviceInputProcessor->process(
351 \
Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
352 'nestedAssociativeArray',
357 $this->assertEquals(1, count(
$result));
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']);
368 public function testNestedArrayOfDataObjectProperties()
372 'items' => [[
'entityId' => 1,
'name' =>
'First'], [
'entityId' => 2,
'name' =>
'Second']],
375 $result = $this->serviceInputProcessor->process(
376 \
Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService::class,
382 $this->assertEquals(1, count(
$result));
385 $this->assertTrue($dataObjects instanceof DataArray);
387 $items = $dataObjects->getItems();
388 $this->assertEquals(2, count(
$items));
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());
411 $this->customAttributeTypeLocator->expects($this->any())->method(
'getType')->willReturn($customAttributeType);
412 $this->serviceTypeToEntityTypeMap->expects($this->any())->method(
'getEntityType')->willReturn($expectedObject);
414 $result = $this->serviceInputProcessor->process(
416 'ObjectWithCustomAttributesMethod',
421 $this->assertEquals($expectedObject,
$result[0]);
432 'customAttributeInteger' => [
433 'customAttributeType' =>
'integer',
436 'customAttributes' => [
446 'customAttributeIntegerCamelCaseCode' => [
447 'customAttributeType' =>
'integer',
450 'customAttributes' => [
460 'customAttributeObject' => [
461 'customAttributeType' => \Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\SimpleArray::class,
464 'customAttributes' => [
471 'customAttributeArrayOfObjects' => [
472 'customAttributeType' =>
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\Simple[]',
475 'customAttributes' => [
477 [
'entityId' => 14,
'name' =>
'First'],
478 [
'entityId' => 15,
'name' =>
'Second'],
484 [
'entityId' => 14,
'name' =>
'First'],
485 [
'entityId' => 15,
'name' =>
'Second'],
500 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
501 $customAttributeValue =
null;
504 $customAttributeValue =
$value;
521 $customAttributeValue = [$dataObjectSimple1, $dataObjectSimple2];
534 'custom_attributes' => [
536 \
Magento\Framework\Api\AttributeValue::class,
540 'value' => $customAttributeValue
557 $this->serviceInputProcessor->process(
559 'ObjectWithCustomAttributesMethod',
573 'customAttributes' => [
582 'customAttributes' => [
593 'customAttributes' => [