CompareTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model\Quote\Item;
  7. /**
  8. * Class CompareTest
  9. */
  10. class CompareTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Quote\Model\Quote\Item\Compare
  14. */
  15. private $helper;
  16. /**
  17. * @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $itemMock;
  20. /**
  21. * @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $comparedMock;
  24. /**
  25. * @var \Magento\Quote\Model\Quote\Item\Option|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $optionMock;
  28. /**
  29. * @var \Magento\Framework\Serialize\JsonValidator|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $jsonValidatorMock;
  32. /**
  33. * test setUp
  34. */
  35. protected function setUp()
  36. {
  37. $this->itemMock = $this->createPartialMock(
  38. \Magento\Quote\Model\Quote\Item::class,
  39. ['__wakeup', 'getProductId', 'getOptions']
  40. );
  41. $this->comparedMock = $this->createPartialMock(
  42. \Magento\Quote\Model\Quote\Item::class,
  43. ['__wakeup', 'getProductId', 'getOptions']
  44. );
  45. $this->optionMock = $this->createPartialMock(
  46. \Magento\Quote\Model\Quote\Item\Option::class,
  47. ['__wakeup', 'getCode', 'getValue']
  48. );
  49. $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  50. ->setMethods(['unserialize'])
  51. ->disableOriginalConstructor()
  52. ->getMockForAbstractClass();
  53. $serializer->expects($this->any())
  54. ->method('unserialize')
  55. ->willReturnCallback(
  56. function ($value) {
  57. return json_decode($value, true);
  58. }
  59. );
  60. $this->jsonValidatorMock = $this->getMockBuilder(\Magento\Framework\Serialize\JsonValidator::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  64. $this->helper = $objectManagerHelper->getObject(
  65. \Magento\Quote\Model\Quote\Item\Compare::class,
  66. [
  67. 'serializer' => $serializer,
  68. 'jsonValidator' => $this->jsonValidatorMock
  69. ]
  70. );
  71. }
  72. /**
  73. * @param string $code
  74. * @param mixed $value
  75. * @return \PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected function getOptionMock($code, $value)
  78. {
  79. $optionMock = clone $this->optionMock;
  80. $optionMock->expects($this->once())
  81. ->method('getCode')
  82. ->will($this->returnValue($code));
  83. $optionMock->expects($this->once())
  84. ->method('getValue')
  85. ->will($this->returnValue($value));
  86. return $optionMock;
  87. }
  88. /**
  89. * test compare two different products
  90. */
  91. public function testCompareDifferentProduct()
  92. {
  93. $this->itemMock->expects($this->once())
  94. ->method('getProductId')
  95. ->will($this->returnValue(1));
  96. $this->itemMock->expects($this->once())
  97. ->method('getProductId')
  98. ->will($this->returnValue(2));
  99. $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
  100. }
  101. /**
  102. * test compare two items with different options
  103. */
  104. public function testCompareProductWithDifferentOptions()
  105. {
  106. $this->itemMock->expects($this->any())
  107. ->method('getProductId')
  108. ->will($this->returnValue(1));
  109. $this->comparedMock->expects($this->any())
  110. ->method('getProductId')
  111. ->will($this->returnValue(1));
  112. $this->itemMock->expects($this->any())
  113. ->method('getOptions')
  114. ->will(
  115. $this->returnValue(
  116. [
  117. $this->getOptionMock('option-1', 1),
  118. $this->getOptionMock('option-2', 'option-value'),
  119. $this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2]))
  120. ]
  121. )
  122. );
  123. $this->comparedMock->expects($this->any())
  124. ->method('getOptions')
  125. ->will($this->returnValue(
  126. [
  127. $this->getOptionMock('option-4', 1),
  128. $this->getOptionMock('option-2', 'option-value'),
  129. $this->getOptionMock('option-3', json_encode([
  130. 'value' => 'value-1',
  131. 'qty' => 2,
  132. ])),
  133. ]
  134. ));
  135. $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
  136. }
  137. /**
  138. * test compare two items first with options and second without options
  139. */
  140. public function testCompareItemWithComparedWithoutOption()
  141. {
  142. $this->itemMock->expects($this->any())
  143. ->method('getProductId')
  144. ->will($this->returnValue(1));
  145. $this->comparedMock->expects($this->any())
  146. ->method('getProductId')
  147. ->will($this->returnValue(1));
  148. $this->itemMock->expects($this->any())
  149. ->method('getOptions')
  150. ->will(
  151. $this->returnValue(
  152. [
  153. $this->getOptionMock('option-1', 1),
  154. $this->getOptionMock('option-2', 'option-value'),
  155. $this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2])),
  156. ]
  157. )
  158. );
  159. $this->comparedMock->expects($this->any())
  160. ->method('getOptions')
  161. ->will($this->returnValue([]));
  162. $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
  163. }
  164. /**
  165. * test compare two items first without options and second with options
  166. */
  167. public function testCompareItemWithoutOptionWithCompared()
  168. {
  169. $this->itemMock->expects($this->any())
  170. ->method('getProductId')
  171. ->will($this->returnValue(1));
  172. $this->comparedMock->expects($this->any())
  173. ->method('getProductId')
  174. ->will($this->returnValue(1));
  175. $this->comparedMock->expects($this->any())
  176. ->method('getOptions')
  177. ->will($this->returnValue(
  178. [
  179. $this->getOptionMock('option-1', 1),
  180. $this->getOptionMock('option-2', 'option-value'),
  181. $this->getOptionMock(
  182. 'option-3',
  183. json_encode(['value' => 'value-1', 'qty' => 2])
  184. ),
  185. ]
  186. ));
  187. $this->itemMock->expects($this->any())
  188. ->method('getOptions')
  189. ->will($this->returnValue([]));
  190. $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
  191. }
  192. /**
  193. * Verify that compare ignores empty options.
  194. */
  195. public function testCompareWithEmptyValues()
  196. {
  197. $itemOptionValue = '{"non-empty-option":"test","empty_option":""}';
  198. $comparedOptionValue = '{"non-empty-option":"test"}';
  199. $this->jsonValidatorMock->expects($this->any())
  200. ->method('isValid')
  201. ->willReturn(true);
  202. $this->itemMock->expects($this->any())
  203. ->method('getProductId')
  204. ->will($this->returnValue(1));
  205. $this->comparedMock->expects($this->any())
  206. ->method('getProductId')
  207. ->will($this->returnValue(1));
  208. $this->itemMock->expects($this->once())
  209. ->method('getOptions')
  210. ->willReturn(
  211. [
  212. $this->getOptionMock('option-1', $itemOptionValue)
  213. ]
  214. );
  215. $this->comparedMock->expects($this->once())
  216. ->method('getOptions')
  217. ->willReturn(
  218. [
  219. $this->getOptionMock('option-1', $comparedOptionValue)
  220. ]
  221. );
  222. $this->assertTrue($this->helper->compare($this->itemMock, $this->comparedMock));
  223. }
  224. }