RenewCookieObserverTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 RenewCookieObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\RenewCookieObserver
  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 $sessionFactoryMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $observerMock;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $eventManagerMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $sessionMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $requestMock;
  46. protected function setUp()
  47. {
  48. $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  49. $this->helperMock = $this->createMock(\Magento\Persistent\Helper\Data::class);
  50. $this->sessionHelperMock = $this->createMock(\Magento\Persistent\Helper\Session::class);
  51. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  52. $this->sessionFactoryMock =
  53. $this->createPartialMock(\Magento\Persistent\Model\SessionFactory::class, ['create']);
  54. $this->observerMock = $this->createMock(\Magento\Framework\Event\Observer::class);
  55. $eventMethods = ['getRequest', '__wakeUp'];
  56. $this->eventManagerMock = $this->createPartialMock(\Magento\Framework\Event::class, $eventMethods);
  57. $this->sessionMock = $this->createMock(\Magento\Persistent\Model\Session::class);
  58. $this->model = new \Magento\Persistent\Observer\RenewCookieObserver(
  59. $this->helperMock,
  60. $this->sessionHelperMock,
  61. $this->customerSessionMock,
  62. $this->sessionFactoryMock
  63. );
  64. }
  65. public function testRenewCookie()
  66. {
  67. $this->helperMock
  68. ->expects($this->once())
  69. ->method('canProcess')
  70. ->with($this->observerMock)
  71. ->will($this->returnValue(true));
  72. $this->helperMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true));
  73. $this->sessionHelperMock->expects($this->once())->method('isPersistent')->will($this->returnValue(true));
  74. $this->observerMock
  75. ->expects($this->once())
  76. ->method('getEvent')
  77. ->will($this->returnValue($this->eventManagerMock));
  78. $this->eventManagerMock
  79. ->expects($this->once())
  80. ->method('getRequest')
  81. ->will($this->returnValue($this->requestMock));
  82. $this->customerSessionMock->expects($this->once())->method('isLoggedIn')->will($this->returnValue(false));
  83. $this->requestMock
  84. ->expects($this->once())
  85. ->method('getFullActionName')
  86. ->will($this->returnValue('customer_account_logout'));
  87. $this->helperMock->expects($this->once())->method('getLifeTime')->will($this->returnValue(60));
  88. $this->customerSessionMock
  89. ->expects($this->once())->method('getCookiePath')->will($this->returnValue('path/cookie'));
  90. $this->sessionFactoryMock
  91. ->expects($this->once())
  92. ->method('create')
  93. ->will($this->returnValue($this->sessionMock));
  94. $this->sessionMock->expects($this->once())->method('renewPersistentCookie')->with(60, 'path/cookie');
  95. $this->model->execute($this->observerMock);
  96. }
  97. public function testRenewCookieWhenCannotProcessPersistentData()
  98. {
  99. $this->helperMock
  100. ->expects($this->once())
  101. ->method('canProcess')
  102. ->with($this->observerMock)
  103. ->will($this->returnValue(false));
  104. $this->helperMock->expects($this->never())->method('isEnabled');
  105. $this->observerMock
  106. ->expects($this->never())
  107. ->method('getEvent');
  108. $this->model->execute($this->observerMock);
  109. }
  110. }