LogoutTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Account;
  7. use Magento\Customer\Controller\Account\Logout;
  8. use Magento\Framework\App\Response\RedirectInterface;
  9. use Magento\Framework\Controller\Result\Redirect;
  10. use Magento\Framework\Controller\Result\RedirectFactory;
  11. use Magento\Framework\Stdlib\Cookie\CookieMetadata;
  12. use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
  13. use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
  14. class LogoutTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /** @var Logout */
  17. protected $controller;
  18. /** @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $contextMock;
  20. /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $sessionMock;
  22. /** @var CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $cookieMetadataFactory;
  24. /** @var PhpCookieManager|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $cookieManager;
  26. /** @var CookieMetadata|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $cookieMetadata;
  28. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $resultRedirect;
  30. /** @var RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $redirectFactory;
  32. /** @var RedirectInterface|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $redirect;
  34. protected function setUp()
  35. {
  36. $this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $this->sessionMock = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  40. ->disableOriginalConstructor()
  41. ->setMethods(['getId', 'logout', 'setBeforeAuthUrl', 'setLastCustomerId'])
  42. ->getMock();
  43. $this->cookieMetadataFactory = $this->getMockBuilder(CookieMetadataFactory::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->cookieManager = $this->getMockBuilder(PhpCookieManager::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->cookieMetadata = $this->getMockBuilder(CookieMetadata::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->redirectFactory = $this->getMockBuilder(RedirectFactory::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->resultRedirect = $this->getMockBuilder(Redirect::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $this->contextMock->expects($this->once())
  59. ->method('getResultRedirectFactory')
  60. ->willReturn($this->redirectFactory);
  61. $this->redirect = $this->getMockBuilder(RedirectInterface::class)
  62. ->getMockForAbstractClass();
  63. $this->contextMock->expects($this->once())
  64. ->method('getRedirect')
  65. ->willReturn($this->redirect);
  66. $this->controller = new Logout($this->contextMock, $this->sessionMock);
  67. $refClass = new \ReflectionClass(Logout::class);
  68. $cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
  69. $cookieMetadataManagerProperty->setAccessible(true);
  70. $cookieMetadataManagerProperty->setValue($this->controller, $this->cookieManager);
  71. $cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
  72. $cookieMetadataFactoryProperty->setAccessible(true);
  73. $cookieMetadataFactoryProperty->setValue($this->controller, $this->cookieMetadataFactory);
  74. }
  75. public function testExecute()
  76. {
  77. $customerId = 1;
  78. $refererUrl = 'http://referer.url';
  79. $this->sessionMock->expects($this->once())
  80. ->method('getId')
  81. ->willReturn($customerId);
  82. $this->sessionMock->expects($this->once())
  83. ->method('logout')
  84. ->willReturnSelf();
  85. $this->redirect->expects($this->once())
  86. ->method('getRefererUrl')
  87. ->willReturn($refererUrl);
  88. $this->sessionMock->expects($this->once())
  89. ->method('setBeforeAuthUrl')
  90. ->with($refererUrl)
  91. ->willReturnSelf();
  92. $this->sessionMock->expects($this->once())
  93. ->method('setLastCustomerId')
  94. ->with($customerId);
  95. $this->cookieManager->expects($this->once())
  96. ->method('getCookie')
  97. ->with('mage-cache-sessid')
  98. ->willReturn(true);
  99. $this->cookieMetadataFactory->expects($this->once())
  100. ->method('createCookieMetadata')
  101. ->willReturn($this->cookieMetadata);
  102. $this->cookieMetadata->expects($this->once())
  103. ->method('setPath')
  104. ->with('/');
  105. $this->cookieManager->expects($this->once())
  106. ->method('deleteCookie')
  107. ->with('mage-cache-sessid', $this->cookieMetadata);
  108. $this->redirectFactory->expects($this->once())
  109. ->method('create')
  110. ->willReturn($this->resultRedirect);
  111. $this->resultRedirect->expects($this->once())
  112. ->method('setPath')
  113. ->with('*/*/logoutSuccess');
  114. $this->assertSame($this->resultRedirect, $this->controller->execute());
  115. }
  116. }