Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerSessionUserContextTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class CustomerSessionUserContextTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $objectManager;
20 
25 
29  protected $customerSession;
30 
31  protected function setUp()
32  {
33  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
34 
35  $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
36  ->disableOriginalConstructor()
37  ->setMethods(['getId'])
38  ->getMock();
39 
40  $this->customerSessionUserContext = $this->objectManager->getObject(
41  \Magento\Customer\Model\Authorization\CustomerSessionUserContext::class,
42  ['customerSession' => $this->customerSession]
43  );
44  }
45 
46  public function testGetUserIdExist()
47  {
48  $userId = 1;
49  $this->setupUserId($userId);
50  $this->assertEquals($userId, $this->customerSessionUserContext->getUserId());
51  }
52 
53  public function testGetUserIdDoesNotExist()
54  {
55  $userId = null;
56  $this->setupUserId($userId);
57  $this->assertEquals($userId, $this->customerSessionUserContext->getUserId());
58  }
59 
60  public function testGetUserType()
61  {
62  $this->assertEquals(UserContextInterface::USER_TYPE_CUSTOMER, $this->customerSessionUserContext->getUserType());
63  }
64 
69  public function setupUserId($userId)
70  {
71  $this->customerSession->expects($this->once())
72  ->method('getId')
73  ->will($this->returnValue($userId));
74  }
75 }