Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
CompareTest Class Reference
Inheritance diagram for CompareTest:

Public Member Functions

 testCompareDifferentProduct ()
 
 testCompareProductWithDifferentOptions ()
 
 testCompareItemWithComparedWithoutOption ()
 
 testCompareItemWithoutOptionWithCompared ()
 
 testCompareWithEmptyValues ()
 

Protected Member Functions

 setUp ()
 
 getOptionMock ($code, $value)
 

Detailed Description

Class CompareTest

Definition at line 12 of file CompareTest.php.

Member Function Documentation

◆ getOptionMock()

getOptionMock (   $code,
  $value 
)
protected
Parameters
string$code
mixed$value
Returns
\PHPUnit_Framework_MockObject_MockObject

Definition at line 87 of file CompareTest.php.

88  {
89  $optionMock = clone $this->optionMock;
90  $optionMock->expects($this->once())
91  ->method('getCode')
92  ->will($this->returnValue($code));
93  $optionMock->expects($this->once())
94  ->method('getValue')
95  ->will($this->returnValue($value));
96  return $optionMock;
97  }
$value
Definition: gender.phtml:16
$code
Definition: info.phtml:12

◆ setUp()

setUp ( )
protected

test setUp

Definition at line 42 of file CompareTest.php.

43  {
44  $this->itemMock = $this->createPartialMock(
45  \Magento\Quote\Model\Quote\Item::class,
46  ['__wakeup', 'getProductId', 'getOptions']
47  );
48  $this->comparedMock = $this->createPartialMock(
49  \Magento\Quote\Model\Quote\Item::class,
50  ['__wakeup', 'getProductId', 'getOptions']
51  );
52  $this->optionMock = $this->createPartialMock(
53  \Magento\Quote\Model\Quote\Item\Option::class,
54  ['__wakeup', 'getCode', 'getValue']
55  );
56  $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
57  ->setMethods(['unserialize'])
58  ->disableOriginalConstructor()
59  ->getMockForAbstractClass();
60  $serializer->expects($this->any())
61  ->method('unserialize')
62  ->willReturnCallback(
63  function ($value) {
64  return json_decode($value, true);
65  }
66  );
67 
68  $this->jsonValidatorMock = $this->getMockBuilder(\Magento\Framework\Serialize\JsonValidator::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71 
72  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
73  $this->helper = $objectManagerHelper->getObject(
74  \Magento\Quote\Model\Quote\Item\Compare::class,
75  [
76  'serializer' => $serializer,
77  'jsonValidator' => $this->jsonValidatorMock
78  ]
79  );
80  }
$value
Definition: gender.phtml:16

◆ testCompareDifferentProduct()

testCompareDifferentProduct ( )

test compare two different products

Definition at line 102 of file CompareTest.php.

103  {
104  $this->itemMock->expects($this->once())
105  ->method('getProductId')
106  ->will($this->returnValue(1));
107  $this->itemMock->expects($this->once())
108  ->method('getProductId')
109  ->will($this->returnValue(2));
110 
111  $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
112  }

◆ testCompareItemWithComparedWithoutOption()

testCompareItemWithComparedWithoutOption ( )

test compare two items first with options and second without options

Definition at line 155 of file CompareTest.php.

156  {
157  $this->itemMock->expects($this->any())
158  ->method('getProductId')
159  ->will($this->returnValue(1));
160  $this->comparedMock->expects($this->any())
161  ->method('getProductId')
162  ->will($this->returnValue(1));
163  $this->itemMock->expects($this->any())
164  ->method('getOptions')
165  ->will(
166  $this->returnValue(
167  [
168  $this->getOptionMock('option-1', 1),
169  $this->getOptionMock('option-2', 'option-value'),
170  $this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2])),
171  ]
172  )
173  );
174  $this->comparedMock->expects($this->any())
175  ->method('getOptions')
176  ->will($this->returnValue([]));
177  $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
178  }

◆ testCompareItemWithoutOptionWithCompared()

testCompareItemWithoutOptionWithCompared ( )

test compare two items first without options and second with options

Definition at line 183 of file CompareTest.php.

184  {
185  $this->itemMock->expects($this->any())
186  ->method('getProductId')
187  ->will($this->returnValue(1));
188  $this->comparedMock->expects($this->any())
189  ->method('getProductId')
190  ->will($this->returnValue(1));
191  $this->comparedMock->expects($this->any())
192  ->method('getOptions')
193  ->will($this->returnValue(
194  [
195  $this->getOptionMock('option-1', 1),
196  $this->getOptionMock('option-2', 'option-value'),
197  $this->getOptionMock(
198  'option-3',
199  json_encode(['value' => 'value-1', 'qty' => 2])
200  ),
201  ]
202  ));
203  $this->itemMock->expects($this->any())
204  ->method('getOptions')
205  ->will($this->returnValue([]));
206  $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
207  }

◆ testCompareProductWithDifferentOptions()

testCompareProductWithDifferentOptions ( )

test compare two items with different options

Definition at line 117 of file CompareTest.php.

118  {
119  $this->itemMock->expects($this->any())
120  ->method('getProductId')
121  ->will($this->returnValue(1));
122  $this->comparedMock->expects($this->any())
123  ->method('getProductId')
124  ->will($this->returnValue(1));
125 
126  $this->itemMock->expects($this->any())
127  ->method('getOptions')
128  ->will(
129  $this->returnValue(
130  [
131  $this->getOptionMock('option-1', 1),
132  $this->getOptionMock('option-2', 'option-value'),
133  $this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2]))
134  ]
135  )
136  );
137  $this->comparedMock->expects($this->any())
138  ->method('getOptions')
139  ->will($this->returnValue(
140  [
141  $this->getOptionMock('option-4', 1),
142  $this->getOptionMock('option-2', 'option-value'),
143  $this->getOptionMock('option-3', json_encode([
144  'value' => 'value-1',
145  'qty' => 2,
146  ])),
147  ]
148  ));
149  $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
150  }

◆ testCompareWithEmptyValues()

testCompareWithEmptyValues ( )

Verify that compare ignores empty options.

Definition at line 212 of file CompareTest.php.

213  {
214  $itemOptionValue = '{"non-empty-option":"test","empty_option":""}';
215  $comparedOptionValue = '{"non-empty-option":"test"}';
216 
217  $this->jsonValidatorMock->expects($this->any())
218  ->method('isValid')
219  ->willReturn(true);
220 
221  $this->itemMock->expects($this->any())
222  ->method('getProductId')
223  ->will($this->returnValue(1));
224  $this->comparedMock->expects($this->any())
225  ->method('getProductId')
226  ->will($this->returnValue(1));
227 
228  $this->itemMock->expects($this->once())
229  ->method('getOptions')
230  ->willReturn(
231  [
232  $this->getOptionMock('option-1', $itemOptionValue)
233  ]
234  );
235  $this->comparedMock->expects($this->once())
236  ->method('getOptions')
237  ->willReturn(
238  [
239  $this->getOptionMock('option-1', $comparedOptionValue)
240  ]
241  );
242 
243  $this->assertTrue($this->helper->compare($this->itemMock, $this->comparedMock));
244  }

The documentation for this class was generated from the following file: