123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?php
- /**
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Quote\Test\Unit\Model;
- use Magento\Quote\Model\CouponManagement;
- class CouponManagementTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var CouponManagement
- */
- protected $couponManagement;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteRepositoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $storeMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $quoteAddressMock;
- protected function setUp()
- {
- $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
- $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
- 'getItemsCount',
- 'setCouponCode',
- 'collectTotals',
- 'save',
- 'getShippingAddress',
- 'getCouponCode',
- 'getStoreId',
- '__wakeup'
- ]);
- $this->quoteAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
- 'setCollectShippingRates',
- '__wakeup'
- ]);
- $this->couponManagement = new CouponManagement(
- $this->quoteRepositoryMock
- );
- }
- public function testGetCoupon()
- {
- $cartId = 11;
- $couponCode = 'test_coupon_code';
- $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getCouponCode', '__wakeup']);
- $quoteMock->expects($this->any())->method('getCouponCode')->will($this->returnValue($couponCode));
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')
- ->with($cartId)
- ->will($this->returnValue($quoteMock));
- $this->assertEquals($couponCode, $this->couponManagement->get($cartId));
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage The "33" Cart doesn't contain products.
- */
- public function testSetWhenCartDoesNotContainsProducts()
- {
- $cartId = 33;
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(0));
- $this->couponManagement->set($cartId, 'coupon_code');
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotSaveException
- * @expectedExceptionMessage The coupon code couldn't be applied. Verify the coupon code and try again.
- */
- public function testSetWhenCouldNotApplyCoupon()
- {
- $cartId = 33;
- $couponCode = '153a-ABC';
- $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
- $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
- $this->quoteMock->expects($this->once())
- ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
- $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
- $exceptionMessage = "The coupon code couldn't be applied. Verify the coupon code and try again.";
- $exception = new \Magento\Framework\Exception\CouldNotDeleteException(__($exceptionMessage));
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteRepositoryMock->expects($this->once())
- ->method('save')
- ->with($this->quoteMock)
- ->willThrowException($exception);
- $this->couponManagement->set($cartId, $couponCode);
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again.
- */
- public function testSetWhenCouponCodeIsInvalid()
- {
- $cartId = 33;
- $couponCode = '153a-ABC';
- $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
- $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
- $this->quoteMock->expects($this->once())
- ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
- $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
- $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue('invalidCoupon'));
- $this->couponManagement->set($cartId, $couponCode);
- }
- public function testSet()
- {
- $cartId = 33;
- $couponCode = '153a-ABC';
- $this->storeMock->expects($this->any())->method('getId')->will($this->returnValue(1));
- $this->quoteMock->expects($this->once())->method('getStoreId')->willReturn($this->returnValue(1));
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
- $this->quoteMock->expects($this->once())
- ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
- $this->quoteMock->expects($this->once())->method('setCouponCode')->with($couponCode);
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
- $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue($couponCode));
- $this->assertTrue($this->couponManagement->set($cartId, $couponCode));
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage The "65" Cart doesn't contain products.
- */
- public function testDeleteWhenCartDoesNotContainsProducts()
- {
- $cartId = 65;
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(0));
- $this->quoteMock->expects($this->never())->method('getShippingAddress');
- $this->couponManagement->remove($cartId);
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
- * @expectedExceptionMessage The coupon code couldn't be deleted. Verify the coupon code and try again.
- */
- public function testDeleteWhenCouldNotDeleteCoupon()
- {
- $cartId = 65;
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
- $this->quoteMock->expects($this->once())
- ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
- $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $exceptionMessage = "The coupon code couldn't be deleted. Verify the coupon code and try again.";
- $exception = new \Magento\Framework\Exception\CouldNotSaveException(__($exceptionMessage));
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteRepositoryMock->expects($this->once())
- ->method('save')
- ->with($this->quoteMock)
- ->willThrowException($exception);
- $this->couponManagement->remove($cartId);
- }
- /**
- * @expectedException \Magento\Framework\Exception\CouldNotDeleteException
- * @expectedExceptionMessage The coupon code couldn't be deleted. Verify the coupon code and try again.
- */
- public function testDeleteWhenCouponIsNotEmpty()
- {
- $cartId = 65;
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
- $this->quoteMock->expects($this->once())
- ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
- $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
- $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue('123_ABC'));
- $this->couponManagement->remove($cartId);
- }
- public function testDelete()
- {
- $cartId = 65;
- $this->quoteRepositoryMock->expects($this->once())
- ->method('getActive')->with($cartId)->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(12));
- $this->quoteMock->expects($this->once())
- ->method('getShippingAddress')->will($this->returnValue($this->quoteAddressMock));
- $this->quoteAddressMock->expects($this->once())->method('setCollectShippingRates')->with(true);
- $this->quoteMock->expects($this->once())->method('setCouponCode')->with('');
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteMock->expects($this->once())->method('collectTotals')->will($this->returnValue($this->quoteMock));
- $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock);
- $this->quoteMock->expects($this->once())->method('getCouponCode')->will($this->returnValue(''));
- $this->assertTrue($this->couponManagement->remove($cartId));
- }
- }
|