SessionCheckerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\CustomerData\Plugin;
  7. use Magento\Customer\CustomerData\Plugin\SessionChecker;
  8. class SessionCheckerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var SessionChecker
  12. */
  13. protected $plugin;
  14. /**
  15. * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $cookieManager;
  18. /**
  19. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $metadataFactory;
  22. /**
  23. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadata|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $metadata;
  26. /**
  27. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $sessionManager;
  30. public function setUp()
  31. {
  32. $this->cookieManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $this->metadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->metadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->sessionManager = $this->getMockBuilder(\Magento\Framework\Session\SessionManager::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->plugin = new SessionChecker($this->cookieManager, $this->metadataFactory);
  45. }
  46. /**
  47. * @param bool $result
  48. * @param string $callCount
  49. * @return void
  50. * @dataProvider beforeStartDataProvider
  51. */
  52. public function testBeforeStart($result, $callCount)
  53. {
  54. $phpSessionCookieName = 'PHPSESSID';
  55. $frontendSessionCookieName = 'mage-cache-sessid';
  56. $this->sessionManager->expects($this->once())
  57. ->method('getName')
  58. ->willReturn($phpSessionCookieName);
  59. $this->cookieManager->expects($this->exactly(2))
  60. ->method('getCookie')
  61. ->withConsecutive(
  62. [$phpSessionCookieName],
  63. [$frontendSessionCookieName]
  64. )
  65. ->willReturnOnConsecutiveCalls(false, $result);
  66. $this->metadataFactory->expects($this->{$callCount}())
  67. ->method('createCookieMetadata')
  68. ->willReturn($this->metadata);
  69. $this->metadata->expects($this->{$callCount}())
  70. ->method('setPath')
  71. ->with('/');
  72. $this->cookieManager->expects($this->{$callCount}())
  73. ->method('deleteCookie')
  74. ->with('mage-cache-sessid', $this->metadata);
  75. $this->plugin->beforeStart($this->sessionManager);
  76. }
  77. /**
  78. * @return array
  79. */
  80. public function beforeStartDataProvider()
  81. {
  82. return [
  83. [true, 'once'],
  84. [false, 'never']
  85. ];
  86. }
  87. }