ExtensibleDataObjectConverterTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Api\Test\Unit;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Framework\Api\AbstractExtensibleObject;
  9. use Magento\Framework\Api\AttributeValue;
  10. class ExtensibleDataObjectConverterTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /** @var \Magento\Framework\Api\ExtensibleDataObjectConverter */
  13. protected $converter;
  14. /** @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $processor;
  16. /** @var \Magento\Framework\Api\ExtensibleDataInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $dataObject;
  18. protected function setUp()
  19. {
  20. $this->processor = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $this->dataObject = $this->getMockBuilder(\Magento\Framework\Api\ExtensibleDataInterface::class)
  24. ->getMock();
  25. $objectManager = new ObjectManager($this);
  26. $this->converter = $objectManager->getObject(
  27. \Magento\Framework\Api\ExtensibleDataObjectConverter::class,
  28. [
  29. 'dataObjectProcessor' => $this->processor,
  30. ]
  31. );
  32. }
  33. /**
  34. * Test toNestedArray() method without custom attributes.
  35. */
  36. public function testToNestedArray()
  37. {
  38. $dataArray = [
  39. 'attribute_key' => 'attribute_value',
  40. ];
  41. $this->processor->expects($this->any())
  42. ->method('buildOutputDataArray')
  43. ->with($this->dataObject)
  44. ->willReturn($dataArray);
  45. $this->assertEquals(
  46. $dataArray,
  47. $this->converter->toNestedArray($this->dataObject)
  48. );
  49. }
  50. /**
  51. * Test toNestedArray() method with custom attributes and with skipped custom attribute.
  52. */
  53. public function testToNestedArrayCustom()
  54. {
  55. $dataArray = [
  56. 'attribute_key' => 'attribute_value',
  57. AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY => [
  58. [
  59. AttributeValue::ATTRIBUTE_CODE => 'custom_attribute_code',
  60. AttributeValue::VALUE => 'custom_attribute_value',
  61. ],
  62. [
  63. AttributeValue::ATTRIBUTE_CODE => 'custom_attribute_code_multi',
  64. AttributeValue::VALUE => [
  65. 'custom_attribute_value_multi_1',
  66. 'custom_attribute_value_multi_2',
  67. ],
  68. ],
  69. [
  70. AttributeValue::ATTRIBUTE_CODE => 'custom_attribute_code_skip',
  71. AttributeValue::VALUE => 'custom_attribute_value_skip',
  72. ],
  73. ],
  74. ];
  75. $resultArray = [
  76. 'attribute_key' => 'attribute_value',
  77. 'custom_attribute_code' => 'custom_attribute_value',
  78. 'custom_attribute_code_multi' => [
  79. 'custom_attribute_value_multi_1',
  80. 'custom_attribute_value_multi_2',
  81. ],
  82. ];
  83. $this->processor->expects($this->any())
  84. ->method('buildOutputDataArray')
  85. ->with($this->dataObject)
  86. ->willReturn($dataArray);
  87. $this->assertEquals(
  88. $resultArray,
  89. $this->converter->toNestedArray($this->dataObject, ['custom_attribute_code_skip'])
  90. );
  91. }
  92. }