|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Copyright © 2015 Magento. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Magento\Quote\Test\Unit\Model\GuestCart; |
| 9 | + |
| 10 | +class GuestCouponManagementTest extends \PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var \Magento\Quote\Model\GuestCart\GuestCouponManagement |
| 14 | + */ |
| 15 | + protected $model; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 19 | + */ |
| 20 | + protected $quoteRepositoryMock; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 24 | + */ |
| 25 | + protected $quoteMock; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 29 | + */ |
| 30 | + protected $quoteAddressMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 34 | + */ |
| 35 | + protected $quoteIdMaskFactoryMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var \PHPUnit_Framework_MockObject_MockObject |
| 39 | + */ |
| 40 | + protected $quoteIdMaskMock; |
| 41 | + |
| 42 | + protected function setUp() |
| 43 | + { |
| 44 | + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); |
| 45 | + |
| 46 | + $this->quoteRepositoryMock = $this->getMock('Magento\Quote\Model\QuoteRepository', [], [], '', false); |
| 47 | + $this->quoteMock = $this->getMock( |
| 48 | + 'Magento\Quote\Model\Quote', |
| 49 | + [ |
| 50 | + 'getItemsCount', |
| 51 | + 'setCouponCode', |
| 52 | + 'collectTotals', |
| 53 | + 'save', |
| 54 | + 'getShippingAddress', |
| 55 | + 'getCouponCode' |
| 56 | + ], |
| 57 | + [], |
| 58 | + '', |
| 59 | + false |
| 60 | + ); |
| 61 | + $this->quoteAddressMock = $this->getMock( |
| 62 | + 'Magento\Quote\Model\Quote\Address', |
| 63 | + [ |
| 64 | + 'setCollectShippingRates' |
| 65 | + ], |
| 66 | + [], |
| 67 | + '', |
| 68 | + false); |
| 69 | + |
| 70 | + $this->quoteIdMaskFactoryMock = $this->getMock('Magento\Quote\Model\QuoteIdMaskFactory', [], [], '', false); |
| 71 | + $this->quoteIdMaskMock = $this->getMock('Magento\Quote\Model\QuoteIdMask', [], [], '', false); |
| 72 | + |
| 73 | + $this->model = $objectManager->getObject( |
| 74 | + 'Magento\Quote\Model\GuestCart\GuestCouponManagement', |
| 75 | + [ |
| 76 | + 'quoteRepository' => $this->quoteRepositoryMock, |
| 77 | + 'quoteIdMaskFactory' => $this->quoteIdMaskFactoryMock |
| 78 | + ] |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public function testGetCoupon() |
| 83 | + { |
| 84 | + $maskedCartId = 'f216207248d65c789b17be8545e0aa73'; |
| 85 | + $cartId = 11; |
| 86 | + $couponCode = 'test_coupon_code'; |
| 87 | + |
| 88 | + $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock); |
| 89 | + $this->quoteIdMaskMock->expects($this->once()) |
| 90 | + ->method('load') |
| 91 | + ->with($maskedCartId, 'masked_id') |
| 92 | + ->willReturn($this->quoteIdMaskMock); |
| 93 | + $this->quoteIdMaskMock->expects($this->once()) |
| 94 | + ->method('getId') |
| 95 | + ->willReturn($cartId); |
| 96 | + |
| 97 | + $quoteMock = $this->getMock('Magento\Quote\Model\Quote', ['getCouponCode', '__wakeup'], [], '', false); |
| 98 | + $quoteMock->expects($this->any())->method('getCouponCode')->will($this->returnValue($couponCode)); |
| 99 | + |
| 100 | + $this->quoteRepositoryMock->expects($this->once()) |
| 101 | + ->method('getActive') |
| 102 | + ->with($cartId) |
| 103 | + ->will($this->returnValue($quoteMock)); |
| 104 | + |
| 105 | + $this->assertEquals($couponCode, $this->model->get($maskedCartId)); |
| 106 | + } |
| 107 | + |
| 108 | + public function testSet() |
| 109 | + { |
| 110 | + $maskedCartId = 'f216207248d65c789b17be8545e0aa73'; |
| 111 | + $cartId = 33; |
| 112 | + $couponCode = '153a-ABC'; |
| 113 | + |
| 114 | + $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock); |
| 115 | + $this->quoteIdMaskMock->expects($this->once()) |
| 116 | + ->method('load') |
| 117 | + ->with($maskedCartId, 'masked_id') |
| 118 | + ->willReturn($this->quoteIdMaskMock); |
| 119 | + $this->quoteIdMaskMock->expects($this->once()) |
| 120 | + ->method('getId') |
| 121 | + ->willReturn($cartId); |
| 122 | + |
| 123 | + $this->quoteRepositoryMock->expects($this->once()) |
| 124 | + ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); |
| 125 | + $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12)); |
| 126 | + $this->quoteMock->expects($this->once()) |
| 127 | + ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock)); |
| 128 | + $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true); |
| 129 | + $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode); |
| 130 | + $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); |
| 131 | + $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); |
| 132 | + $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue($couponCode)); |
| 133 | + |
| 134 | + $this->assertTrue($this->model->set($maskedCartId, $couponCode)); |
| 135 | + } |
| 136 | + |
| 137 | + public function testDelete() |
| 138 | + { |
| 139 | + $maskedCartId = 'f216207248d65c789b17be8545e0aa73'; |
| 140 | + $cartId = 65; |
| 141 | + |
| 142 | + $this->quoteIdMaskFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteIdMaskMock); |
| 143 | + $this->quoteIdMaskMock->expects($this->once()) |
| 144 | + ->method('load') |
| 145 | + ->with($maskedCartId, 'masked_id') |
| 146 | + ->willReturn($this->quoteIdMaskMock); |
| 147 | + $this->quoteIdMaskMock->expects($this->once()) |
| 148 | + ->method('getId') |
| 149 | + ->willReturn($cartId); |
| 150 | + |
| 151 | + $this->quoteRepositoryMock->expects($this->once()) |
| 152 | + ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock)); |
| 153 | + $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12)); |
| 154 | + $this->quoteMock->expects($this->once()) |
| 155 | + ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock)); |
| 156 | + $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true); |
| 157 | + $this->quoteMock->expects($this->once())->method('setCouponCode')->with(''); |
| 158 | + $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); |
| 159 | + $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock)); |
| 160 | + $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); |
| 161 | + $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue('')); |
| 162 | + |
| 163 | + $this->assertTrue($this->model->remove($maskedCartId)); |
| 164 | + } |
| 165 | +} |
0 commit comments