123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\SalesRule\Test\Unit\Model\Quote;
- /**
- * Class DiscountTest
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class DiscountTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\SalesRule\Model\Quote\Discount
- */
- protected $discount;
- /**
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
- */
- protected $objectManager;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeManagerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $validatorMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $eventManagerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $shippingAssignmentMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $addressMock;
- protected function setUp()
- {
- $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
- $this->validatorMock = $this->getMockBuilder(\Magento\SalesRule\Model\Validator::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'canApplyRules',
- 'reset',
- 'init',
- 'initTotals',
- 'sortItemsByPriority',
- 'setSkipActionsValidation',
- 'process',
- 'processShippingAmount',
- 'canApplyDiscount',
- '__wakeup',
- ]
- )
- ->getMock();
- $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\Manager::class);
- $priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
- $priceCurrencyMock->expects($this->any())
- ->method('round')
- ->will($this->returnCallback(
- function ($argument) {
- return round($argument, 2);
- }
- ));
- $this->addressMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Address::class,
- ['getQuote', 'getAllItems', 'getShippingAmount', '__wakeup', 'getCustomAttributesCodes']
- );
- $this->addressMock->expects($this->any())
- ->method('getCustomAttributesCodes')
- ->willReturn([]);
- $shipping = $this->createMock(\Magento\Quote\Api\Data\ShippingInterface::class);
- $shipping->expects($this->any())->method('getAddress')->willReturn($this->addressMock);
- $this->shippingAssignmentMock = $this->createMock(\Magento\Quote\Api\Data\ShippingAssignmentInterface::class);
- $this->shippingAssignmentMock->expects($this->any())->method('getShipping')->willReturn($shipping);
- /** @var \Magento\SalesRule\Model\Quote\Discount $discount */
- $this->discount = $this->objectManager->getObject(
- \Magento\SalesRule\Model\Quote\Discount::class,
- [
- 'storeManager' => $this->storeManagerMock,
- 'validator' => $this->validatorMock,
- 'eventManager' => $this->eventManagerMock,
- 'priceCurrency' => $priceCurrencyMock,
- ]
- );
- }
- public function testCollectItemNoDiscount()
- {
- $itemNoDiscount = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getNoDiscount', '__wakeup']
- );
- $itemNoDiscount->expects($this->once())->method('getNoDiscount')->willReturn(true);
- $this->validatorMock->expects($this->once())->method('sortItemsByPriority')
- ->with([$itemNoDiscount], $this->addressMock)
- ->willReturnArgument(0);
- $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStore', '__wakeup']);
- $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
- $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
- $this->addressMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
- $this->shippingAssignmentMock->expects($this->any())->method('getItems')->willReturn([$itemNoDiscount]);
- $this->addressMock->expects($this->any())->method('getShippingAmount')->willReturn(true);
- $totalMock = $this->createMock(\Magento\Quote\Model\Quote\Address\Total::class);
- $this->assertInstanceOf(
- \Magento\SalesRule\Model\Quote\Discount::class,
- $this->discount->collect($quoteMock, $this->shippingAssignmentMock, $totalMock)
- );
- }
- public function testCollectItemHasParent()
- {
- $itemWithParentId = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Item::class,
- ['getNoDiscount', 'getParentItem', '__wakeup']
- );
- $itemWithParentId->expects($this->once())->method('getNoDiscount')->willReturn(false);
- $itemWithParentId->expects($this->once())->method('getParentItem')->willReturn(true);
- $this->validatorMock->expects($this->any())->method('canApplyDiscount')->willReturn(true);
- $this->validatorMock->expects($this->any())->method('sortItemsByPriority')
- ->with([$itemWithParentId], $this->addressMock)
- ->willReturnArgument(0);
- $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getStore', '__wakeup']);
- $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
- $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
- $this->addressMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
- $this->addressMock->expects($this->any())->method('getShippingAmount')->willReturn(true);
- $this->shippingAssignmentMock->expects($this->any())->method('getItems')->willReturn([$itemWithParentId]);
- $totalMock = $this->createMock(\Magento\Quote\Model\Quote\Address\Total::class);
- $this->assertInstanceOf(
- \Magento\SalesRule\Model\Quote\Discount::class,
- $this->discount->collect($quoteMock, $this->shippingAssignmentMock, $totalMock)
- );
- }
- /**
- * @dataProvider collectItemHasChildrenDataProvider
- */
- public function testCollectItemHasChildren($childItemData, $parentData, $expectedChildData)
- {
- $childItems = [];
- foreach ($childItemData as $itemId => $itemData) {
- $item = $this->objectManager->getObject(\Magento\Quote\Model\Quote\Item::class)->setData($itemData);
- $childItems[$itemId] = $item;
- }
- $itemWithChildren = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getNoDiscount',
- 'getParentItem',
- 'getHasChildren',
- 'isChildrenCalculated',
- 'getChildren',
- '__wakeup',
- ]
- )
- ->getMock();
- $itemWithChildren->expects($this->once())->method('getNoDiscount')->willReturn(false);
- $itemWithChildren->expects($this->once())->method('getParentItem')->willReturn(false);
- $itemWithChildren->expects($this->once())->method('getHasChildren')->willReturn(true);
- $itemWithChildren->expects($this->once())->method('isChildrenCalculated')->willReturn(true);
- $itemWithChildren->expects($this->any())->method('getChildren')->willReturn($childItems);
- foreach ($parentData as $key => $value) {
- $itemWithChildren->setData($key, $value);
- }
- $this->validatorMock->expects($this->any())->method('canApplyDiscount')->willReturn(true);
- $this->validatorMock->expects($this->once())->method('sortItemsByPriority')
- ->with([$itemWithChildren], $this->addressMock)
- ->willReturnArgument(0);
- $this->validatorMock->expects($this->any())->method('canApplyRules')->willReturn(true);
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->setMethods(['getStore', '__wakeup'])
- ->getMock();
- $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->addressMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
- $this->addressMock->expects($this->any())->method('getShippingAmount')->willReturn(true);
- $this->shippingAssignmentMock->expects($this->any())->method('getItems')->willReturn([$itemWithChildren]);
- $totalMock = $this->createMock(\Magento\Quote\Model\Quote\Address\Total::class);
- $this->assertInstanceOf(
- \Magento\SalesRule\Model\Quote\Discount::class,
- $this->discount->collect($quoteMock, $this->shippingAssignmentMock, $totalMock)
- );
- foreach ($expectedChildData as $itemId => $expectedItemData) {
- $childItem = $childItems[$itemId];
- foreach ($expectedItemData as $key => $value) {
- $this->assertEquals($value, $childItem->getData($key), 'Incorrect value for ' . $key);
- }
- }
- }
- /**
- * @return array
- */
- public function collectItemHasChildrenDataProvider()
- {
- $data = [
- // 3 items, each $100, testing that discount are distributed to item correctly
- [
- 'child_item_data' => [
- 'item1' => [
- 'base_row_total' => 0,
- ]
- ],
- 'parent_item_data' => [
- 'discount_amount' => 20,
- 'base_discount_amount' => 10,
- 'original_discount_amount' => 40,
- 'base_original_discount_amount' => 20,
- 'base_row_total' => 0,
- ],
- 'expected_child_item_data' => [
- 'item1' => [
- 'discount_amount' => 0,
- 'base_discount_amount' => 0,
- 'original_discount_amount' => 0,
- 'base_original_discount_amount' => 0,
- ]
- ],
- ],
- [
- // 3 items, each $100, testing that discount are distributed to item correctly
- 'child_item_data' => [
- 'item1' => [
- 'base_row_total' => 100,
- ],
- 'item2' => [
- 'base_row_total' => 100,
- ],
- 'item3' => [
- 'base_row_total' => 100,
- ],
- ],
- 'parent_item_data' => [
- 'discount_amount' => 20,
- 'base_discount_amount' => 10,
- 'original_discount_amount' => 40,
- 'base_original_discount_amount' => 20,
- 'base_row_total' => 300,
- ],
- 'expected_child_item_data' => [
- 'item1' => [
- 'discount_amount' => 6.67,
- 'base_discount_amount' => 3.33,
- 'original_discount_amount' => 13.33,
- 'base_original_discount_amount' => 6.67,
- ],
- 'item2' => [
- 'discount_amount' => 6.66,
- 'base_discount_amount' => 3.34,
- 'original_discount_amount' => 13.34,
- 'base_original_discount_amount' => 6.66,
- ],
- 'item3' => [
- 'discount_amount' => 6.67,
- 'base_discount_amount' => 3.33,
- 'original_discount_amount' => 13.33,
- 'base_original_discount_amount' => 6.67,
- ],
- ],
- ],
- ];
- return $data;
- }
- public function testCollectItemHasNoChildren()
- {
- $itemWithChildren = $this->getMockBuilder(\Magento\Quote\Model\Quote\Item::class)
- ->disableOriginalConstructor()
- ->setMethods(
- [
- 'getNoDiscount',
- 'getParentItem',
- 'getHasChildren',
- 'isChildrenCalculated',
- 'getChildren',
- '__wakeup',
- ]
- )
- ->getMock();
- $itemWithChildren->expects($this->once())->method('getNoDiscount')->willReturn(false);
- $itemWithChildren->expects($this->once())->method('getParentItem')->willReturn(false);
- $itemWithChildren->expects($this->once())->method('getHasChildren')->willReturn(false);
- $this->validatorMock->expects($this->any())->method('canApplyDiscount')->willReturn(true);
- $this->validatorMock->expects($this->once())->method('sortItemsByPriority')
- ->with([$itemWithChildren], $this->addressMock)
- ->willReturnArgument(0);
- $storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
- ->disableOriginalConstructor()
- ->setMethods(['getStore', '__wakeup'])
- ->getMock();
- $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
- $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)->disableOriginalConstructor()->getMock();
- $this->addressMock->expects($this->any())->method('getQuote')->willReturn($quoteMock);
- $this->addressMock->expects($this->any())->method('getShippingAmount')->willReturn(true);
- $this->shippingAssignmentMock->expects($this->any())->method('getItems')->willReturn([$itemWithChildren]);
- $totalMock = $this->createMock(\Magento\Quote\Model\Quote\Address\Total::class);
- $this->assertInstanceOf(
- \Magento\SalesRule\Model\Quote\Discount::class,
- $this->discount->collect($quoteMock, $this->shippingAssignmentMock, $totalMock)
- );
- }
- public function testFetch()
- {
- $discountAmount = 100;
- $discountDescription = 100;
- $expectedResult = [
- 'code' => 'discount',
- 'value' => 100,
- 'title' => __('Discount (%1)', $discountDescription)
- ];
- $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
- $totalMock = $this->createPartialMock(
- \Magento\Quote\Model\Quote\Address\Total::class,
- ['getDiscountAmount', 'getDiscountDescription']
- );
- $totalMock->expects($this->once())->method('getDiscountAmount')->willReturn($discountAmount);
- $totalMock->expects($this->once())->method('getDiscountDescription')->willReturn($discountDescription);
- $this->assertEquals($expectedResult, $this->discount->fetch($quoteMock, $totalMock));
- }
- }
|