SetQuotePersistentDataObserverTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Test\Unit\Observer;
  8. class SetQuotePersistentDataObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\SetQuotePersistentDataObserver
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $helperMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $sessionHelperMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $customerSessionMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $observerMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $quoteManagerMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $eventManagerMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $quoteMock;
  42. protected function setUp()
  43. {
  44. $quoteMethods = ['setIsActive', 'setIsPersistent', '__wakeUp'];
  45. $eventMethods = ['getQuote', '__wakeUp'];
  46. $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, $quoteMethods);
  47. $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  48. $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  49. $this->eventManagerMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
  50. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  51. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  52. $this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
  53. $this->model = new \Magento\Persistent\Observer\SetQuotePersistentDataObserver(
  54. $this->sessionHelperMock,
  55. $this->helperMock,
  56. $this->quoteManagerMock,
  57. $this->customerSessionMock
  58. );
  59. }
  60. public function testExecuteWhenSessionIsNotPersistent()
  61. {
  62. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
  63. $this->observerMock->expects($this->never())->method('getEvent');
  64. $this->model->execute($this->observerMock);
  65. }
  66. public function testExecuteWhenQuoteNotExist()
  67. {
  68. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  69. $this->observerMock
  70. ->expects($this->once())
  71. ->method('getEvent')
  72. ->will($this->returnValue($this->eventManagerMock));
  73. $this->eventManagerMock->expects($this->once())->method('getQuote');
  74. $this->customerSessionMock->expects($this->never())->method('isLoggedIn');
  75. $this->model->execute($this->observerMock);
  76. }
  77. public function testExecuteWhenSessionIsPersistent()
  78. {
  79. $this->sessionHelperMock->expects($this->exactly(2))->method('isPersistent')->will($this->returnValue(true));
  80. $this->observerMock
  81. ->expects($this->once())
  82. ->method('getEvent')
  83. ->will($this->returnValue($this->eventManagerMock));
  84. $this->eventManagerMock
  85. ->expects($this->once())
  86. ->method('getQuote')
  87. ->will($this->returnValue($this->quoteMock));
  88. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  89. $this->helperMock->expects($this->once())->method('isShoppingCartPersist')->will($this->returnValue(false));
  90. $this->quoteManagerMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  91. $this->quoteMock->expects($this->once())->method('setIsPersistent')->with(true);
  92. $this->model->execute($this->observerMock);
  93. }
  94. }