ItemConverterTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Test\Unit\Model\Cart\Totals;
  8. class ItemConverterTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $configPoolMock;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $eventManagerMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $totalsFactoryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $dataObjectHelperMock;
  26. /**
  27. * @var \Magento\Quote\Model\Cart\Totals\ItemConverter
  28. */
  29. private $model;
  30. /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
  31. private $serializerMock;
  32. protected function setUp()
  33. {
  34. $this->configPoolMock = $this->createMock(\Magento\Catalog\Helper\Product\ConfigurationPool::class);
  35. $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  36. $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
  37. $this->totalsFactoryMock = $this->createPartialMock(
  38. \Magento\Quote\Api\Data\TotalsItemInterfaceFactory::class,
  39. ['create']
  40. );
  41. $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
  42. $this->model = new \Magento\Quote\Model\Cart\Totals\ItemConverter(
  43. $this->configPoolMock,
  44. $this->eventManagerMock,
  45. $this->totalsFactoryMock,
  46. $this->dataObjectHelperMock,
  47. $this->serializerMock
  48. );
  49. }
  50. public function testModelToDataObject()
  51. {
  52. $productType = 'simple';
  53. $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
  54. $itemMock->expects($this->once())->method('toArray')->will($this->returnValue(['options' => []]));
  55. $itemMock->expects($this->any())->method('getProductType')->will($this->returnValue($productType));
  56. $simpleConfigMock = $this->createMock(\Magento\Catalog\Helper\Product\Configuration::class);
  57. $defaultConfigMock = $this->createMock(\Magento\Catalog\Helper\Product\Configuration::class);
  58. $this->configPoolMock->expects($this->any())->method('getByProductType')
  59. ->will($this->returnValueMap([['simple', $simpleConfigMock], ['default', $defaultConfigMock]]));
  60. $options = ['1' => ['label' => 'option1'], '2' => ['label' => 'option2']];
  61. $simpleConfigMock->expects($this->once())->method('getOptions')->with($itemMock)
  62. ->will($this->returnValue($options));
  63. $option = ['data' => 'optionsData', 'label' => ''];
  64. $defaultConfigMock->expects($this->any())->method('getFormattedOptionValue')->will($this->returnValue($option));
  65. $this->eventManagerMock->expects($this->once())->method('dispatch')
  66. ->with('items_additional_data', ['item' => $itemMock]);
  67. $this->totalsFactoryMock->expects($this->once())->method('create');
  68. $expectedData = [
  69. 'options' => '{"1":{"data":"optionsData","label":"option1"},"2":{"data":"optionsData","label":"option2"}}'
  70. ];
  71. $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray')
  72. ->with(null, $expectedData, \Magento\Quote\Api\Data\TotalsItemInterface::class);
  73. $optionData = [
  74. '1' => [
  75. 'data' => 'optionsData',
  76. 'label' => 'option1'
  77. ],
  78. '2' => [
  79. 'data' => 'optionsData',
  80. 'label' => 'option2'
  81. ]
  82. ];
  83. $this->serializerMock->expects($this->once())->method('serialize')
  84. ->will($this->returnValue(json_encode($optionData)));
  85. $this->model->modelToDataObject($itemMock);
  86. }
  87. }