RemovePersistentCookieOnRegisterObserverTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. use \Magento\Persistent\Observer\RemovePersistentCookieOnRegisterObserver;
  9. class RemovePersistentCookieOnRegisterObserverTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var RemovePersistentCookieOnRegisterObserver
  13. */
  14. protected $model;
  15. /**
  16. * @var \Magento\Persistent\Helper\Session|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $persistentSessionMock;
  19. /**
  20. * @var \Magento\Persistent\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $persistentDataMock;
  23. /**
  24. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $customerSessionMock;
  27. /**
  28. * @var \Magento\Persistent\Model\QuoteManager|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $quoteManagerMock;
  31. /**
  32. * @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $observerMock;
  35. /**
  36. * @var \Magento\Persistent\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $sessionModelMock;
  39. protected function setUp()
  40. {
  41. $this->persistentSessionMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  42. $this->sessionModelMock = $this->createMock(\Magento\Persistent\Model\Session::class);
  43. $this->persistentDataMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  44. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  45. $this->quoteManagerMock = $this->createMock(\Magento\Persistent\Model\QuoteManager::class);
  46. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  47. $this->model = new RemovePersistentCookieOnRegisterObserver(
  48. $this->persistentSessionMock,
  49. $this->persistentDataMock,
  50. $this->customerSessionMock,
  51. $this->quoteManagerMock
  52. );
  53. }
  54. public function testExecuteWithPersistentDataThatCanNotBeProcess()
  55. {
  56. $this->persistentDataMock->expects($this->once())
  57. ->method('canProcess')->with($this->observerMock)->will($this->returnValue(false));
  58. $this->persistentSessionMock->expects($this->never())->method('getSession');
  59. $this->model->execute($this->observerMock);
  60. }
  61. public function testExecuteWhenSessionIsNotPersistent()
  62. {
  63. $this->persistentDataMock->expects($this->once())
  64. ->method('canProcess')->with($this->observerMock)->will($this->returnValue(true));
  65. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(false));
  66. $this->persistentSessionMock->expects($this->never())->method('getSession');
  67. $this->model->execute($this->observerMock);
  68. }
  69. public function testExecuteWithNotLoggedInCustomer()
  70. {
  71. $this->persistentDataMock->expects($this->once())
  72. ->method('canProcess')->with($this->observerMock)->will($this->returnValue(true));
  73. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  74. $this->persistentSessionMock->expects($this->once())
  75. ->method('getSession')->will($this->returnValue($this->sessionModelMock));
  76. $this->sessionModelMock->expects($this->once())->method('removePersistentCookie')->will($this->returnSelf());
  77. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  78. $this->customerSessionMock->expects($this->once())
  79. ->method('setCustomerId')->with(null)->will($this->returnSelf());
  80. $this->customerSessionMock->expects($this->once())
  81. ->method('setCustomerGroupId')->with(null)->will($this->returnSelf());
  82. $this->quoteManagerMock->expects($this->once())->method('setGuest');
  83. $this->model->execute($this->observerMock);
  84. }
  85. public function testExecute()
  86. {
  87. $this->persistentDataMock->expects($this->once())
  88. ->method('canProcess')->with($this->observerMock)->will($this->returnValue(true));
  89. $this->persistentSessionMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  90. $this->persistentSessionMock->expects($this->once())
  91. ->method('getSession')->will($this->returnValue($this->sessionModelMock));
  92. $this->sessionModelMock->expects($this->once())->method('removePersistentCookie')->will($this->returnSelf());
  93. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(true));
  94. $this->customerSessionMock->expects($this->never())->method('setCustomerId');
  95. $this->customerSessionMock->expects($this->never())->method('setCustomerGroupId');
  96. $this->quoteManagerMock->expects($this->once())->method('setGuest');
  97. $this->model->execute($this->observerMock);
  98. }
  99. }