123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Test\Unit\Model\Quote\Item;
- /**
- * Class CompareTest
- */
- class CompareTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Quote\Model\Quote\Item\Compare
- */
- private $helper;
- /**
- * @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject
- */
- private $itemMock;
- /**
- * @var \Magento\Quote\Model\Quote\Item|\PHPUnit_Framework_MockObject_MockObject
- */
- private $comparedMock;
- /**
- * @var \Magento\Quote\Model\Quote\Item\Option|\PHPUnit_Framework_MockObject_MockObject
- */
- private $optionMock;
- /**
- * @var \Magento\Framework\Serialize\JsonValidator|\PHPUnit_Framework_MockObject_MockObject
- */
- private $jsonValidatorMock;
- /**
- * test setUp
- */
- protected function setUp()
- {
- $this->itemMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['__wakeup', 'getProductId', 'getOptions']
- );
- $this->comparedMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['__wakeup', 'getProductId', 'getOptions']
- );
- $this->optionMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item\Option::class,
- ['__wakeup', 'getCode', 'getValue']
- );
- $serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
- ->setMethods(['unserialize'])
- ->disableOriginalConstructor()
- ->getMockForAbstractClass();
- $serializer->expects($this->any())
- ->method('unserialize')
- ->willReturnCallback(
- function ($value) {
- return json_decode($value, true);
- }
- );
- $this->jsonValidatorMock = $this->getMockBuilder(\Magento\Framework\Serialize\JsonValidator::class)
- ->disableOriginalConstructor()
- ->getMock();
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->helper = $objectManagerHelper->getObject(
- \Magento\Quote\Model\Quote\Item\Compare::class,
- [
- 'serializer' => $serializer,
- 'jsonValidator' => $this->jsonValidatorMock
- ]
- );
- }
- /**
- * @param string $code
- * @param mixed $value
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getOptionMock($code, $value)
- {
- $optionMock = clone $this->optionMock;
- $optionMock->expects($this->once())
- ->method('getCode')
- ->will($this->returnValue($code));
- $optionMock->expects($this->once())
- ->method('getValue')
- ->will($this->returnValue($value));
- return $optionMock;
- }
- /**
- * test compare two different products
- */
- public function testCompareDifferentProduct()
- {
- $this->itemMock->expects($this->once())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->itemMock->expects($this->once())
- ->method('getProductId')
- ->will($this->returnValue(2));
- $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
- }
- /**
- * test compare two items with different options
- */
- public function testCompareProductWithDifferentOptions()
- {
- $this->itemMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->comparedMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->itemMock->expects($this->any())
- ->method('getOptions')
- ->will(
- $this->returnValue(
- [
- $this->getOptionMock('option-1', 1),
- $this->getOptionMock('option-2', 'option-value'),
- $this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2]))
- ]
- )
- );
- $this->comparedMock->expects($this->any())
- ->method('getOptions')
- ->will($this->returnValue(
- [
- $this->getOptionMock('option-4', 1),
- $this->getOptionMock('option-2', 'option-value'),
- $this->getOptionMock('option-3', json_encode([
- 'value' => 'value-1',
- 'qty' => 2,
- ])),
- ]
- ));
- $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
- }
- /**
- * test compare two items first with options and second without options
- */
- public function testCompareItemWithComparedWithoutOption()
- {
- $this->itemMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->comparedMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->itemMock->expects($this->any())
- ->method('getOptions')
- ->will(
- $this->returnValue(
- [
- $this->getOptionMock('option-1', 1),
- $this->getOptionMock('option-2', 'option-value'),
- $this->getOptionMock('option-3', json_encode(['value' => 'value-1', 'qty' => 2])),
- ]
- )
- );
- $this->comparedMock->expects($this->any())
- ->method('getOptions')
- ->will($this->returnValue([]));
- $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
- }
- /**
- * test compare two items first without options and second with options
- */
- public function testCompareItemWithoutOptionWithCompared()
- {
- $this->itemMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->comparedMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->comparedMock->expects($this->any())
- ->method('getOptions')
- ->will($this->returnValue(
- [
- $this->getOptionMock('option-1', 1),
- $this->getOptionMock('option-2', 'option-value'),
- $this->getOptionMock(
- 'option-3',
- json_encode(['value' => 'value-1', 'qty' => 2])
- ),
- ]
- ));
- $this->itemMock->expects($this->any())
- ->method('getOptions')
- ->will($this->returnValue([]));
- $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock));
- }
- /**
- * Verify that compare ignores empty options.
- */
- public function testCompareWithEmptyValues()
- {
- $itemOptionValue = '{"non-empty-option":"test","empty_option":""}';
- $comparedOptionValue = '{"non-empty-option":"test"}';
- $this->jsonValidatorMock->expects($this->any())
- ->method('isValid')
- ->willReturn(true);
- $this->itemMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->comparedMock->expects($this->any())
- ->method('getProductId')
- ->will($this->returnValue(1));
- $this->itemMock->expects($this->once())
- ->method('getOptions')
- ->willReturn(
- [
- $this->getOptionMock('option-1', $itemOptionValue)
- ]
- );
- $this->comparedMock->expects($this->once())
- ->method('getOptions')
- ->willReturn(
- [
- $this->getOptionMock('option-1', $comparedOptionValue)
- ]
- );
-
- $this->assertTrue($this->helper->compare($this->itemMock, $this->comparedMock));
- }
- }
|