Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdminSessionUserContextTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class AdminSessionUserContextTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $objectManager;
20 
25 
29  protected $adminSession;
30 
31  protected function setUp()
32  {
33  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
34 
35  $this->adminSession = $this->getMockBuilder(\Magento\Backend\Model\Auth\Session::class)
36  ->disableOriginalConstructor()
37  ->setMethods(['hasUser', 'getUser', 'getId'])
38  ->getMock();
39 
40  $this->adminSessionUserContext = $this->objectManager->getObject(
41  \Magento\User\Model\Authorization\AdminSessionUserContext::class,
42  ['adminSession' => $this->adminSession]
43  );
44  }
45 
46  public function testGetUserIdExist()
47  {
48  $userId = 1;
49 
50  $this->setupUserId($userId);
51 
52  $this->assertEquals($userId, $this->adminSessionUserContext->getUserId());
53  }
54 
55  public function testGetUserIdDoesNotExist()
56  {
57  $userId = null;
58 
59  $this->setupUserId($userId);
60 
61  $this->assertEquals($userId, $this->adminSessionUserContext->getUserId());
62  }
63 
64  public function testGetUserType()
65  {
66  $this->assertEquals(UserContextInterface::USER_TYPE_ADMIN, $this->adminSessionUserContext->getUserType());
67  }
68 
73  public function setupUserId($userId)
74  {
75  $this->adminSession->expects($this->once())
76  ->method('hasUser')
77  ->will($this->returnValue($userId));
78 
79  if ($userId) {
80  $this->adminSession->expects($this->once())
81  ->method('getUser')
82  ->will($this->returnSelf());
83 
84  $this->adminSession->expects($this->once())
85  ->method('getId')
86  ->will($this->returnValue($userId));
87  }
88  }
89 }