SetCanApplyMsrpObserverTest.php 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Msrp\Test\Unit\Observer\Frontend\Quote;
  7. use Magento\Quote\Model\Quote\Address;
  8. class SetCanApplyMsrpObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Msrp\Observer\Frontend\Quote\SetCanApplyMsrpObserver
  12. */
  13. protected $observer;
  14. /**
  15. * @var \Magento\Msrp\Model\Config|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $configMock;
  18. /** @var \PHPUnit_Framework_MockObject_MockObject */
  19. protected $canApplyMsrpMock;
  20. /** @var \PHPUnit_Framework_MockObject_MockObject */
  21. protected $msrpMock;
  22. protected function setUp()
  23. {
  24. $this->configMock = $this->createMock(\Magento\Msrp\Model\Config::class);
  25. $this->canApplyMsrpMock = $this->createMock(\Magento\Msrp\Model\Quote\Address\CanApplyMsrp::class);
  26. $this->msrpMock = $this->createMock(\Magento\Msrp\Model\Quote\Msrp::class);
  27. $this->observer = new \Magento\Msrp\Observer\Frontend\Quote\SetCanApplyMsrpObserver(
  28. $this->configMock,
  29. $this->canApplyMsrpMock,
  30. $this->msrpMock
  31. );
  32. }
  33. public function testSetQuoteCanApplyMsrpIfMsrpCanApply()
  34. {
  35. $quoteId = 100;
  36. $eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getQuote']);
  37. $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getAllAddresses', 'getId']);
  38. $observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  39. $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
  40. $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
  41. $this->configMock->expects($this->once())->method('isEnabled')->willReturn(true);
  42. $this->msrpMock->expects($this->once())->method('setCanApplyMsrp')->with($quoteId, true);
  43. $addressMock = $this->createPartialMock(\Magento\Customer\Model\Address\AbstractAddress::class, ['__wakeup']);
  44. $this->canApplyMsrpMock->expects($this->once())->method('isCanApplyMsrp')->willReturn(true);
  45. $quoteMock->expects($this->once())->method('getAllAddresses')->willReturn([$addressMock]);
  46. $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
  47. $this->observer->execute($observerMock);
  48. }
  49. public function setQuoteCanApplyMsrpDataProvider()
  50. {
  51. $quoteId = 100;
  52. $eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getQuote']);
  53. $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getAllAddresses', 'getId']);
  54. $observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  55. $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
  56. $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
  57. $this->configMock->expects($this->once())->method('isEnabled')->willReturn(true);
  58. $this->msrpMock->expects($this->once())->method('setCanApplyMsrp')->with($quoteId, false);
  59. $addressMock = $this->createPartialMock(\Magento\Customer\Model\Address\AbstractAddress::class, ['__wakeup']);
  60. $this->canApplyMsrpMock->expects($this->once())->method('isCanApplyMsrp')->willReturn(false);
  61. $quoteMock->expects($this->once())->method('getAllAddresses')->willReturn([$addressMock]);
  62. $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
  63. $this->observer->execute($observerMock);
  64. }
  65. public function testSetQuoteCanApplyMsrpIfMsrpDisabled()
  66. {
  67. $quoteId = 100;
  68. $eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getQuote']);
  69. $quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, ['getAllAddresses', 'getId']);
  70. $observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  71. $observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
  72. $eventMock->expects($this->once())->method('getQuote')->willReturn($quoteMock);
  73. $this->configMock->expects($this->once())->method('isEnabled')->willReturn(false);
  74. $this->msrpMock->expects($this->once())->method('setCanApplyMsrp')->with($quoteId, false);
  75. $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
  76. $this->observer->execute($observerMock);
  77. }
  78. }