Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ItemConverterTest.php
Go to the documentation of this file.
1 <?php
8 
9 class ItemConverterTest extends \PHPUnit\Framework\TestCase
10 {
14  protected $configPoolMock;
15 
19  protected $eventManagerMock;
20 
24  protected $totalsFactoryMock;
25 
30 
34  private $model;
35 
37  private $serializerMock;
38 
39  protected function setUp()
40  {
41  $this->configPoolMock = $this->createMock(\Magento\Catalog\Helper\Product\ConfigurationPool::class);
42  $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
43  $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
44  $this->totalsFactoryMock = $this->createPartialMock(
45  \Magento\Quote\Api\Data\TotalsItemInterfaceFactory::class,
46  ['create']
47  );
48 
49  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
50 
51  $this->model = new \Magento\Quote\Model\Cart\Totals\ItemConverter(
52  $this->configPoolMock,
53  $this->eventManagerMock,
54  $this->totalsFactoryMock,
55  $this->dataObjectHelperMock,
56  $this->serializerMock
57  );
58  }
59 
60  public function testModelToDataObject()
61  {
62  $productType = 'simple';
63 
64  $itemMock = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
65  $itemMock->expects($this->once())->method('toArray')->will($this->returnValue(['options' => []]));
66  $itemMock->expects($this->any())->method('getProductType')->will($this->returnValue($productType));
67 
68  $simpleConfigMock = $this->createMock(\Magento\Catalog\Helper\Product\Configuration::class);
69  $defaultConfigMock = $this->createMock(\Magento\Catalog\Helper\Product\Configuration::class);
70 
71  $this->configPoolMock->expects($this->any())->method('getByProductType')
72  ->will($this->returnValueMap([['simple', $simpleConfigMock], ['default', $defaultConfigMock]]));
73 
74  $options = ['1' => ['label' => 'option1'], '2' => ['label' => 'option2']];
75  $simpleConfigMock->expects($this->once())->method('getOptions')->with($itemMock)
76  ->will($this->returnValue($options));
77 
78  $option = ['data' => 'optionsData', 'label' => ''];
79  $defaultConfigMock->expects($this->any())->method('getFormattedOptionValue')->will($this->returnValue($option));
80 
81  $this->eventManagerMock->expects($this->once())->method('dispatch')
82  ->with('items_additional_data', ['item' => $itemMock]);
83 
84  $this->totalsFactoryMock->expects($this->once())->method('create');
85 
86  $expectedData = [
87  'options' => '{"1":{"data":"optionsData","label":"option1"},"2":{"data":"optionsData","label":"option2"}}'
88  ];
89  $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray')
90  ->with(null, $expectedData, \Magento\Quote\Api\Data\TotalsItemInterface::class);
91 
92  $optionData = [
93  '1' => [
94  'data' => 'optionsData',
95  'label' => 'option1'
96  ],
97  '2' => [
98  'data' => 'optionsData',
99  'label' => 'option2'
100  ]
101  ];
102  $this->serializerMock->expects($this->once())->method('serialize')
103  ->will($this->returnValue(json_encode($optionData)));
104 
105  $this->model->modelToDataObject($itemMock);
106  }
107 }
$optionData