|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2023 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * ADOBE CONFIDENTIAL |
| 7 | + * |
| 8 | + * NOTICE: All information contained herein is, and remains |
| 9 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 10 | + * and technical concepts contained herein are proprietary to Adobe |
| 11 | + * and its suppliers and are protected by all applicable intellectual |
| 12 | + * property laws, including trade secret and copyright laws. |
| 13 | + * Dissemination of this information or reproduction of this material |
| 14 | + * is strictly forbidden unless prior written permission is obtained |
| 15 | + * from Adobe. |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\Quote\Test\Unit\Model\Quote; |
| 20 | + |
| 21 | +use Magento\Framework\Exception\CouldNotSaveException; |
| 22 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 23 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 24 | +use Magento\Quote\Model\CouponManagement; |
| 25 | +use Magento\Quote\Model\Quote; |
| 26 | +use Magento\Quote\Model\Quote\Address; |
| 27 | +use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use PHPUnit\Framework\TestCase; |
| 29 | + |
| 30 | +class CouponManagementTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var CartRepositoryInterface|MockObject |
| 34 | + */ |
| 35 | + private CartRepositoryInterface|MockObject $quoteRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var CouponManagement |
| 39 | + */ |
| 40 | + private CouponManagement $couponManagement; |
| 41 | + |
| 42 | + protected function setUp(): void |
| 43 | + { |
| 44 | + $this->quoteRepository = $this->getMockForAbstractClass(CartRepositoryInterface::class); |
| 45 | + $this->couponManagement = new CouponManagement($this->quoteRepository); |
| 46 | + |
| 47 | + parent::setUp(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return void |
| 52 | + * @throws CouldNotSaveException |
| 53 | + * @throws NoSuchEntityException |
| 54 | + */ |
| 55 | + public function testSetCouponSuccess(): void |
| 56 | + { |
| 57 | + $cartId = 1; |
| 58 | + $couponCode = ' code '; |
| 59 | + |
| 60 | + $shippingAddress = $this->getShippingAddressMock(); |
| 61 | + $shippingAddress->expects($this->once())->method('setCollectShippingRates')->with(true); |
| 62 | + $quote = $this->getMockBuilder(Quote::class) |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->addMethods(['setCouponCode', 'getCouponCode']) |
| 65 | + ->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals']) |
| 66 | + ->getMock(); |
| 67 | + $quote->expects($this->once())->method('getItemsCount')->willReturn(2); |
| 68 | + $quote->expects($this->once())->method('getStoreId')->willReturn(1); |
| 69 | + $quote->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddress); |
| 70 | + $quote->expects($this->once())->method('setCouponCode')->with(trim($couponCode)); |
| 71 | + $quote->expects($this->once())->method('collectTotals')->willReturnSelf(); |
| 72 | + $quote->expects($this->once())->method('getCouponCode')->willReturn(trim($couponCode)); |
| 73 | + $this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote); |
| 74 | + $this->quoteRepository->expects($this->once())->method('save'); |
| 75 | + |
| 76 | + $this->assertTrue($this->couponManagement->set($cartId, $couponCode)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return void |
| 81 | + * @throws CouldNotSaveException |
| 82 | + * @throws NoSuchEntityException |
| 83 | + */ |
| 84 | + public function testSetCouponNoEntityExceptionProducts(): void |
| 85 | + { |
| 86 | + $cartId = 1; |
| 87 | + $couponCode = ' code '; |
| 88 | + |
| 89 | + $this->expectException(NoSuchEntityException::class); |
| 90 | + $this->expectExceptionMessage('The "' . $cartId . '" Cart doesn\'t contain products.'); |
| 91 | + |
| 92 | + $quote = $this->getMockBuilder(Quote::class) |
| 93 | + ->disableOriginalConstructor() |
| 94 | + ->addMethods(['setCouponCode', 'getCouponCode']) |
| 95 | + ->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals']) |
| 96 | + ->getMock(); |
| 97 | + $quote->expects($this->once())->method('getItemsCount')->willReturn(0); |
| 98 | + $this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote); |
| 99 | + |
| 100 | + $this->couponManagement->set($cartId, $couponCode); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return void |
| 105 | + * @throws CouldNotSaveException |
| 106 | + * @throws NoSuchEntityException |
| 107 | + */ |
| 108 | + public function testSetCouponNoEntityExceptionStore(): void |
| 109 | + { |
| 110 | + $cartId = 1; |
| 111 | + $couponCode = ' code '; |
| 112 | + |
| 113 | + $this->expectException(NoSuchEntityException::class); |
| 114 | + $this->expectExceptionMessage('Cart isn\'t assigned to correct store'); |
| 115 | + |
| 116 | + $quote = $this->getMockBuilder(Quote::class) |
| 117 | + ->disableOriginalConstructor() |
| 118 | + ->addMethods(['setCouponCode', 'getCouponCode']) |
| 119 | + ->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals']) |
| 120 | + ->getMock(); |
| 121 | + $quote->expects($this->once())->method('getItemsCount')->willReturn(1); |
| 122 | + $quote->expects($this->once())->method('getStoreId')->willReturn(0); |
| 123 | + $this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote); |
| 124 | + |
| 125 | + $this->couponManagement->set($cartId, $couponCode); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return void |
| 130 | + * @throws CouldNotSaveException |
| 131 | + * @throws NoSuchEntityException |
| 132 | + */ |
| 133 | + public function testSetCouponExceptionSave(): void |
| 134 | + { |
| 135 | + $cartId = 1; |
| 136 | + $couponCode = ' code '; |
| 137 | + |
| 138 | + $this->expectException(CouldNotSaveException::class); |
| 139 | + $this->expectExceptionMessage("The coupon code couldn't be applied. Verify the coupon code and try again."); |
| 140 | + |
| 141 | + $shippingAddress = $this->getShippingAddressMock(); |
| 142 | + $shippingAddress->expects($this->once())->method('setCollectShippingRates')->with(true); |
| 143 | + $quote = $this->getMockBuilder(Quote::class) |
| 144 | + ->disableOriginalConstructor() |
| 145 | + ->addMethods(['setCouponCode', 'getCouponCode']) |
| 146 | + ->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals']) |
| 147 | + ->getMock(); |
| 148 | + $quote->expects($this->once())->method('getItemsCount')->willReturn(2); |
| 149 | + $quote->expects($this->once())->method('getStoreId')->willReturn(1); |
| 150 | + $quote->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddress); |
| 151 | + $quote->expects($this->once())->method('setCouponCode')->with(trim($couponCode)); |
| 152 | + $quote->expects($this->once())->method('collectTotals')->willReturnSelf(); |
| 153 | + $this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote); |
| 154 | + $this->quoteRepository->expects($this->once())->method('save')->willThrowException(new \Exception()); |
| 155 | + |
| 156 | + $this->couponManagement->set($cartId, $couponCode); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @return void |
| 161 | + * @throws CouldNotSaveException |
| 162 | + * @throws NoSuchEntityException |
| 163 | + */ |
| 164 | + public function testSetCouponNoEntityExceptionCoupon(): void |
| 165 | + { |
| 166 | + $cartId = 1; |
| 167 | + $couponCode = ' code '; |
| 168 | + |
| 169 | + $this->expectException(NoSuchEntityException::class); |
| 170 | + $this->expectExceptionMessage("The coupon code isn't valid. Verify the code and try again."); |
| 171 | + |
| 172 | + $shippingAddress = $this->getShippingAddressMock(); |
| 173 | + $shippingAddress->expects($this->once())->method('setCollectShippingRates')->with(true); |
| 174 | + $quote = $this->getMockBuilder(Quote::class) |
| 175 | + ->disableOriginalConstructor() |
| 176 | + ->addMethods(['setCouponCode', 'getCouponCode']) |
| 177 | + ->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals']) |
| 178 | + ->getMock(); |
| 179 | + $quote->expects($this->once())->method('getItemsCount')->willReturn(2); |
| 180 | + $quote->expects($this->once())->method('getStoreId')->willReturn(1); |
| 181 | + $quote->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddress); |
| 182 | + $quote->expects($this->once())->method('setCouponCode')->with(trim($couponCode)); |
| 183 | + $quote->expects($this->once())->method('collectTotals')->willReturnSelf(); |
| 184 | + $quote->expects($this->once())->method('getCouponCode')->willReturn(null); |
| 185 | + $this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote); |
| 186 | + $this->quoteRepository->expects($this->once())->method('save'); |
| 187 | + |
| 188 | + $this->couponManagement->set($cartId, $couponCode); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @return MockObject |
| 193 | + */ |
| 194 | + private function getShippingAddressMock(): MockObject |
| 195 | + { |
| 196 | + return $this->getMockBuilder(Address::class) |
| 197 | + ->disableOriginalConstructor() |
| 198 | + ->addMethods(['setCollectShippingRates']) |
| 199 | + ->getMock(); |
| 200 | + } |
| 201 | +} |
0 commit comments