Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CompositeUserContextTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Authorization\Model\CompositeUserContext;
10 
13 
14 class CompositeUserContextTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $userContext;
20 
25 
29  protected $objectManager;
30 
31  protected function setUp()
32  {
33  $this->objectManager = new ObjectManager($this);
34  $this->compositeHelperMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\Helper\Composite::class)
35  ->disableOriginalConstructor()
36  ->setMethods(['filterAndSortDeclaredComponents'])
37  ->getMock();
38  $this->compositeHelperMock
39  ->expects($this->any())
40  ->method('filterAndSortDeclaredComponents')
41  ->will($this->returnArgument(0));
42  $this->userContext = $this->objectManager->getObject(
43  \Magento\Authorization\Model\CompositeUserContext::class,
44  ['compositeHelper' => $this->compositeHelperMock]
45  );
46  }
47 
48  public function testConstructor()
49  {
50  $userContextMock = $this->createUserContextMock();
51  $contexts = [
52  [
53  'sortOrder' => 10,
54  'type' => $userContextMock,
55  ],
56  ];
57  $model = $this->objectManager->getObject(
58  \Magento\Authorization\Model\CompositeUserContext::class,
59  ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
60  );
61  $this->verifyUserContextIsAdded($model, $userContextMock);
62  }
63 
64  public function testGetUserId()
65  {
66  $expectedUserId = 1;
67  $expectedUserType = 'Customer';
68  $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
69  ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
70  $userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
71  $userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
72  $contexts = [
73  [
74  'sortOrder' => 10,
75  'type' => $userContextMock,
76  ],
77  ];
78  $this->userContext = $this->objectManager->getObject(
79  \Magento\Authorization\Model\CompositeUserContext::class,
80  ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
81  );
82  $actualUserId = $this->userContext->getUserId();
83  $this->assertEquals($expectedUserId, $actualUserId, 'User ID is defined incorrectly.');
84  }
85 
86  public function testGetUserType()
87  {
88  $expectedUserId = 1;
89  $expectedUserType = 'Customer';
90  $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
91  ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
92  $userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
93  $userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
94  $contexts = [
95  [
96  'sortOrder' => 10,
97  'type' => $userContextMock,
98  ],
99  ];
100  $this->userContext = $this->objectManager->getObject(
101  \Magento\Authorization\Model\CompositeUserContext::class,
102  ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
103  );
104  $actualUserType = $this->userContext->getUserType();
105  $this->assertEquals($expectedUserType, $actualUserType, 'User Type is defined incorrectly.');
106  }
107 
108  public function testUserContextCaching()
109  {
110  $expectedUserId = 1;
111  $expectedUserType = 'Customer';
112  $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
113  ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
114  $userContextMock->expects($this->exactly(3))->method('getUserType')
115  ->will($this->returnValue($expectedUserType));
116  $userContextMock->expects($this->exactly(3))->method('getUserId')
117  ->will($this->returnValue($expectedUserId));
118  $contexts = [
119  [
120  'sortOrder' => 10,
121  'type' => $userContextMock,
122  ],
123  ];
124  $this->userContext = $this->objectManager->getObject(
125  \Magento\Authorization\Model\CompositeUserContext::class,
126  ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
127  );
128  $this->userContext->getUserId();
129  $this->userContext->getUserId();
130  $this->userContext->getUserType();
131  $this->userContext->getUserType();
132  }
133 
134  public function testEmptyUserContext()
135  {
136  $expectedUserId = null;
137  $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
138  ->disableOriginalConstructor()->setMethods(['getUserId'])->getMock();
139  $userContextMock->expects($this->any())->method('getUserId')
140  ->will($this->returnValue($expectedUserId));
141  $contexts = [
142  [
143  'sortOrder' => 10,
144  'type' => $userContextMock,
145  ],
146  ];
147  $this->userContext = $this->objectManager->getObject(
148  \Magento\Authorization\Model\CompositeUserContext::class,
149  ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
150  );
151  $actualUserId = $this->userContext->getUserId();
152  $this->assertEquals($expectedUserId, $actualUserId, 'User ID is defined incorrectly.');
153  }
154 
160  protected function createUserContextMock($userId = null, $userType = null)
161  {
162  $useContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
163  ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
164  if ($userId !== null && $userType !== null) {
165  $useContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
166  $useContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
167  }
168  return $useContextMock;
169  }
170 
175  protected function verifyUserContextIsAdded($model, $userContextMock)
176  {
177  $userContext = new \ReflectionProperty(
178  \Magento\Authorization\Model\CompositeUserContext::class,
179  'userContexts'
180  );
181  $userContext->setAccessible(true);
182  $values = $userContext->getValue($model);
183  $this->assertCount(1, $values, 'User context is not registered.');
184  $this->assertEquals($userContextMock, $values[0], 'User context is registered incorrectly.');
185  }
186 }
$values
Definition: options.phtml:88