|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +namespace Magento\OfflineShipping\Test\Unit\Model\Quote\Address; |
| 8 | + |
| 9 | +class FreeShippingTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var \Magento\OfflineShipping\Model\Quote\Address\FreeShipping |
| 13 | + */ |
| 14 | + private $model; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\StoreManagerInterface |
| 18 | + */ |
| 19 | + private $storeManagerMock; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\OfflineShipping\Model\SalesRule\Calculator |
| 23 | + */ |
| 24 | + private $calculatorMock; |
| 25 | + |
| 26 | + protected function setUp() |
| 27 | + { |
| 28 | + $this->storeManagerMock = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class); |
| 29 | + $this->calculatorMock = $this->getMock( |
| 30 | + \Magento\OfflineShipping\Model\SalesRule\Calculator::class, |
| 31 | + [], |
| 32 | + [], |
| 33 | + '', |
| 34 | + false |
| 35 | + ); |
| 36 | + |
| 37 | + $this->model = new \Magento\OfflineShipping\Model\Quote\Address\FreeShipping( |
| 38 | + $this->storeManagerMock, |
| 39 | + $this->calculatorMock |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + public function testIsFreeShippingIfNoItems() |
| 44 | + { |
| 45 | + $quoteMock = $this->getMock(\Magento\Quote\Model\Quote::class, [], [], '', false); |
| 46 | + $this->assertFalse($this->model->isFreeShipping($quoteMock, [])); |
| 47 | + } |
| 48 | + |
| 49 | + public function testIsFreeShipping() |
| 50 | + { |
| 51 | + $storeId = 100; |
| 52 | + $websiteId = 200; |
| 53 | + $customerGroupId = 300; |
| 54 | + $quoteMock = $this->getMock( |
| 55 | + \Magento\Quote\Model\Quote::class, |
| 56 | + ['getShippingAddress', 'getStoreId', 'getCustomerGroupId', 'getCouponCode'], |
| 57 | + [], |
| 58 | + '', |
| 59 | + false |
| 60 | + ); |
| 61 | + $itemMock = $this->getMock( |
| 62 | + \Magento\Quote\Model\Quote\Item::class, |
| 63 | + [ |
| 64 | + 'getNoDiscount', |
| 65 | + 'getParentItemId', |
| 66 | + 'getFreeShipping', |
| 67 | + 'getAddress', |
| 68 | + 'isChildrenCalculated', |
| 69 | + 'getHasChildren', |
| 70 | + 'getChildren' |
| 71 | + ], |
| 72 | + [], |
| 73 | + '', |
| 74 | + false |
| 75 | + ); |
| 76 | + |
| 77 | + $quoteMock->expects($this->once())->method('getStoreId')->willReturn($storeId); |
| 78 | + $storeMock = $this->getMock(\Magento\Store\Api\Data\StoreInterface::class); |
| 79 | + $storeMock->expects($this->once())->method('getWebsiteId')->willReturn($websiteId); |
| 80 | + $this->storeManagerMock->expects($this->once())->method('getStore')->with($storeId)->willReturn($storeMock); |
| 81 | + |
| 82 | + $quoteMock->expects($this->once())->method('getCustomerGroupId')->willReturn($customerGroupId); |
| 83 | + $quoteMock->expects($this->once())->method('getCouponCode')->willReturn(null); |
| 84 | + |
| 85 | + $this->calculatorMock->expects($this->once()) |
| 86 | + ->method('init') |
| 87 | + ->with($websiteId, $customerGroupId, null) |
| 88 | + ->willReturnSelf(); |
| 89 | + |
| 90 | + $itemMock->expects($this->once())->method('getNoDiscount')->willReturn(false); |
| 91 | + $itemMock->expects($this->once())->method('getParentItemId')->willReturn(false); |
| 92 | + $this->calculatorMock->expects($this->exactly(2))->method('processFreeShipping')->willReturnSelf(); |
| 93 | + $itemMock->expects($this->once())->method('getFreeShipping')->willReturn(true); |
| 94 | + |
| 95 | + $addressMock = $this->getMock( |
| 96 | + \Magento\Quote\Model\Quote\Address::class, |
| 97 | + ['getFreeShipping', 'setFreeShipping'], |
| 98 | + [], |
| 99 | + '', |
| 100 | + false |
| 101 | + ); |
| 102 | + $itemAddressMock = $this->getMock( |
| 103 | + \Magento\Quote\Model\Quote\Address::class, |
| 104 | + ['getFreeShipping', 'setFreeShipping'], |
| 105 | + [], |
| 106 | + '', |
| 107 | + false |
| 108 | + ); |
| 109 | + $itemMock->expects($this->exactly(2))->method('getAddress')->willReturn($itemAddressMock); |
| 110 | + $itemAddressMock->expects($this->once())->method('getFreeShipping')->willReturn(false); |
| 111 | + $addressMock->expects($this->once())->method('getFreeShipping')->willReturn(true); |
| 112 | + $addressMock->expects($this->once())->method('setFreeShipping')->with(0)->willReturnSelf(); |
| 113 | + $itemAddressMock->expects($this->once())->method('setFreeShipping')->with(true)->willReturnSelf(); |
| 114 | + $quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock); |
| 115 | + |
| 116 | + $itemMock->expects($this->once())->method('getHasChildren')->willReturn(true); |
| 117 | + $itemMock->expects($this->once())->method('isChildrenCalculated')->willReturn(true); |
| 118 | + |
| 119 | + $childMock = $this->getMock(\Magento\Quote\Model\Quote\Item::class, ['setFreeShipping'], [], '', false); |
| 120 | + $childMock->expects($this->once())->method('setFreeShipping')->with(true)->willReturnSelf(); |
| 121 | + $itemMock->expects($this->once())->method('getChildren')->willReturn([$childMock]); |
| 122 | + |
| 123 | + $this->assertTrue($this->model->isFreeShipping($quoteMock, [$itemMock])); |
| 124 | + } |
| 125 | +} |
0 commit comments