Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SessionCheckerTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class SessionCheckerTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $plugin;
16 
20  protected $cookieManager;
21 
25  protected $metadataFactory;
26 
30  protected $metadata;
31 
35  protected $sessionManager;
36 
37  public function setUp()
38  {
39  $this->cookieManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42  $this->metadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $this->metadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
46  ->disableOriginalConstructor()
47  ->getMock();
48  $this->sessionManager = $this->getMockBuilder(\Magento\Framework\Session\SessionManager::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->plugin = new SessionChecker($this->cookieManager, $this->metadataFactory);
53  }
54 
61  public function testBeforeStart($result, $callCount)
62  {
63  $phpSessionCookieName = 'PHPSESSID';
64  $frontendSessionCookieName = 'mage-cache-sessid';
65  $this->sessionManager->expects($this->once())
66  ->method('getName')
67  ->willReturn($phpSessionCookieName);
68  $this->cookieManager->expects($this->exactly(2))
69  ->method('getCookie')
70  ->withConsecutive(
71  [$phpSessionCookieName],
72  [$frontendSessionCookieName]
73  )
74  ->willReturnOnConsecutiveCalls(false, $result);
75 
76  $this->metadataFactory->expects($this->{$callCount}())
77  ->method('createCookieMetadata')
78  ->willReturn($this->metadata);
79  $this->metadata->expects($this->{$callCount}())
80  ->method('setPath')
81  ->with('/');
82  $this->cookieManager->expects($this->{$callCount}())
83  ->method('deleteCookie')
84  ->with('mage-cache-sessid', $this->metadata);
85 
86  $this->plugin->beforeStart($this->sessionManager);
87  }
88 
92  public function beforeStartDataProvider()
93  {
94  return [
95  [true, 'once'],
96  [false, 'never']
97  ];
98  }
99 }