Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomOptionProcessorTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class CustomOptionProcessorTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $objectFactory;
19 
24 
28  protected $extensionFactory;
29 
35 
37  protected $cartItem;
38 
41 
43  protected $productOption;
44 
46  protected $customOption;
47 
49  protected $buyRequest;
50 
52  protected $processor;
53 
55  private $serializer;
56 
57  protected function setUp()
58  {
59  $this->objectFactory = $this->getMockBuilder(\Magento\Framework\DataObject\Factory::class)
60  ->setMethods(['create'])
61  ->disableOriginalConstructor()
62  ->getMock();
63  $this->productOptionFactory = $this->getMockBuilder(\Magento\Quote\Model\Quote\ProductOptionFactory::class)
64  ->setMethods(['create'])
65  ->disableOriginalConstructor()
66  ->getMock();
67  $this->extensionFactory = $this->getMockBuilder(\Magento\Quote\Api\Data\ProductOptionExtensionFactory::class)
68  ->setMethods(['create'])
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->customOptionFactory = $this->getMockBuilder(
72  \Magento\Catalog\Model\CustomOptions\CustomOptionFactory::class
73  )
74  ->setMethods(['create'])
75  ->disableOriginalConstructor()
76  ->getMock();
77  $this->cartItem = $this->getMockBuilder(\Magento\Quote\Api\Data\CartItemInterface::class)
78  ->disableOriginalConstructor()
79  ->setMethods(['getOptionByCode', 'getProductOption', 'setProductOption'])
80  ->getMockForAbstractClass();
81  $this->extensibleAttribute = $this->getMockBuilder(
82  \Magento\Quote\Api\Data\ProductOptionExtensionInterface::class
83  )
84  ->disableOriginalConstructor()
85  ->setMethods(['setCustomOptions', 'getCustomOptions'])
86  ->getMockForAbstractClass();
87  $this->productOption = $this->getMockBuilder(\Magento\Quote\Model\Quote\ProductOption::class)
88  ->disableOriginalConstructor()
89  ->getMock();
90  $this->customOption = $this->getMockBuilder(\Magento\Catalog\Api\Data\CustomOptionInterface::class)
91  ->disableOriginalConstructor()
92  ->getMockForAbstractClass();
93  $this->buyRequest = $this->getMockBuilder(\Magento\Framework\DataObject::class)
94  ->disableOriginalConstructor()
95  ->getMock();
96  $this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
97  ->setMethods(['unserialize'])
98  ->getMockForAbstractClass();
99 
100  $this->processor = new CustomOptionProcessor(
101  $this->objectFactory,
102  $this->productOptionFactory,
103  $this->extensionFactory,
104  $this->customOptionFactory,
105  $this->serializer
106  );
107  }
108 
109  public function testConvertToBuyRequest()
110  {
111  $optionId = 23;
112  $optionValue = 'Option value';
113  $this->objectFactory->expects($this->once())
114  ->method('create')
115  ->willReturn($this->buyRequest);
116  $this->cartItem->expects($this->any())
117  ->method('getProductOption')
118  ->willReturn($this->productOption);
119  $this->productOption->expects($this->any())
120  ->method('getExtensionAttributes')
121  ->willReturn($this->extensibleAttribute);
122  $this->extensibleAttribute->expects($this->atLeastOnce())
123  ->method('getCustomOptions')
124  ->willReturn([$this->customOption]);
125  $this->customOption->expects($this->once())
126  ->method('getOptionId')
127  ->willReturn($optionId);
128  $this->customOption->expects($this->once())
129  ->method('getOptionValue')
130  ->willReturn($optionValue);
131 
132  $this->assertSame($this->buyRequest, $this->processor->convertToBuyRequest($this->cartItem));
133  }
134 
138  public function testProcessCustomOptions()
139  {
140  $optionId = 23;
141  $quoteItemOption = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item\Option::class)
142  ->disableOriginalConstructor()
143  ->getMock();
144  $this->cartItem->expects($this->atLeastOnce())
145  ->method('getOptionByCode')
146  ->with('info_buyRequest')
147  ->willReturn($quoteItemOption);
148  $quoteItemOption->expects($this->any())
149  ->method('getValue')
150  ->willReturn('{"options":{"' . $optionId . '":["5","6"]}}');
151  $this->serializer->expects($this->any())
152  ->method('unserialize')
153  ->willReturn(json_decode($quoteItemOption->getValue(), true));
154  $this->customOptionFactory->expects($this->once())
155  ->method('create')
156  ->willReturn($this->customOption);
157  $this->customOption->expects($this->once())
158  ->method('setOptionId')
159  ->with($optionId);
160  $this->customOption->expects($this->once())
161  ->method('setOptionValue')
162  ->with('5,6');
163  $this->cartItem->expects($this->atLeastOnce())
164  ->method('getProductOption')
165  ->willReturn(false);
166  $this->productOptionFactory->expects($this->once())
167  ->method('create')
168  ->willReturn($this->productOption);
169  $this->productOption->expects($this->once())
170  ->method('getExtensionAttributes')
171  ->willReturn(false);
172  $this->extensionFactory->expects($this->once())
173  ->method('create')
174  ->willReturn($this->extensibleAttribute);
175  $this->extensibleAttribute->expects($this->once())
176  ->method('setCustomOptions')
177  ->with([$optionId => $this->customOption]);
178  $this->productOption->expects($this->once())
179  ->method('setExtensionAttributes')
180  ->with($this->extensibleAttribute);
181  $this->cartItem->expects($this->once())
182  ->method('setProductOption')
183  ->with($this->productOption);
184 
185  $this->assertSame($this->cartItem, $this->processor->processOptions($this->cartItem));
186  }
187 }