123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Authorization\Test\Unit\Model;
- use \Magento\Authorization\Model\CompositeUserContext;
- use Magento\Framework\ObjectManager\Helper\Composite as CompositeHelper;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- class CompositeUserContextTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var CompositeUserContext
- */
- protected $userContext;
- /**
- * @var CompositeHelper
- */
- protected $compositeHelperMock;
- /**
- * @var ObjectManager
- */
- protected $objectManager;
- protected function setUp()
- {
- $this->objectManager = new ObjectManager($this);
- $this->compositeHelperMock = $this->getMockBuilder(\Magento\Framework\ObjectManager\Helper\Composite::class)
- ->disableOriginalConstructor()
- ->setMethods(['filterAndSortDeclaredComponents'])
- ->getMock();
- $this->compositeHelperMock
- ->expects($this->any())
- ->method('filterAndSortDeclaredComponents')
- ->will($this->returnArgument(0));
- $this->userContext = $this->objectManager->getObject(
- \Magento\Authorization\Model\CompositeUserContext::class,
- ['compositeHelper' => $this->compositeHelperMock]
- );
- }
- public function testConstructor()
- {
- $userContextMock = $this->createUserContextMock();
- $contexts = [
- [
- 'sortOrder' => 10,
- 'type' => $userContextMock,
- ],
- ];
- $model = $this->objectManager->getObject(
- \Magento\Authorization\Model\CompositeUserContext::class,
- ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
- );
- $this->verifyUserContextIsAdded($model, $userContextMock);
- }
- public function testGetUserId()
- {
- $expectedUserId = 1;
- $expectedUserType = 'Customer';
- $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
- ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
- $userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
- $userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
- $contexts = [
- [
- 'sortOrder' => 10,
- 'type' => $userContextMock,
- ],
- ];
- $this->userContext = $this->objectManager->getObject(
- \Magento\Authorization\Model\CompositeUserContext::class,
- ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
- );
- $actualUserId = $this->userContext->getUserId();
- $this->assertEquals($expectedUserId, $actualUserId, 'User ID is defined incorrectly.');
- }
- public function testGetUserType()
- {
- $expectedUserId = 1;
- $expectedUserType = 'Customer';
- $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
- ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
- $userContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($expectedUserId));
- $userContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($expectedUserType));
- $contexts = [
- [
- 'sortOrder' => 10,
- 'type' => $userContextMock,
- ],
- ];
- $this->userContext = $this->objectManager->getObject(
- \Magento\Authorization\Model\CompositeUserContext::class,
- ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
- );
- $actualUserType = $this->userContext->getUserType();
- $this->assertEquals($expectedUserType, $actualUserType, 'User Type is defined incorrectly.');
- }
- public function testUserContextCaching()
- {
- $expectedUserId = 1;
- $expectedUserType = 'Customer';
- $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
- ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
- $userContextMock->expects($this->exactly(3))->method('getUserType')
- ->will($this->returnValue($expectedUserType));
- $userContextMock->expects($this->exactly(3))->method('getUserId')
- ->will($this->returnValue($expectedUserId));
- $contexts = [
- [
- 'sortOrder' => 10,
- 'type' => $userContextMock,
- ],
- ];
- $this->userContext = $this->objectManager->getObject(
- \Magento\Authorization\Model\CompositeUserContext::class,
- ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
- );
- $this->userContext->getUserId();
- $this->userContext->getUserId();
- $this->userContext->getUserType();
- $this->userContext->getUserType();
- }
- public function testEmptyUserContext()
- {
- $expectedUserId = null;
- $userContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
- ->disableOriginalConstructor()->setMethods(['getUserId'])->getMock();
- $userContextMock->expects($this->any())->method('getUserId')
- ->will($this->returnValue($expectedUserId));
- $contexts = [
- [
- 'sortOrder' => 10,
- 'type' => $userContextMock,
- ],
- ];
- $this->userContext = $this->objectManager->getObject(
- \Magento\Authorization\Model\CompositeUserContext::class,
- ['compositeHelper' => $this->compositeHelperMock, 'userContexts' => $contexts]
- );
- $actualUserId = $this->userContext->getUserId();
- $this->assertEquals($expectedUserId, $actualUserId, 'User ID is defined incorrectly.');
- }
- /**
- * @param int|null $userId
- * @param string|null $userType
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function createUserContextMock($userId = null, $userType = null)
- {
- $useContextMock = $this->getMockBuilder(\Magento\Authorization\Model\CompositeUserContext::class)
- ->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock();
- if ($userId !== null && $userType !== null) {
- $useContextMock->expects($this->any())->method('getUserId')->will($this->returnValue($userId));
- $useContextMock->expects($this->any())->method('getUserType')->will($this->returnValue($userType));
- }
- return $useContextMock;
- }
- /**
- * @param CompositeUserContext $model
- * @param CompositeUserContext $userContextMock
- */
- protected function verifyUserContextIsAdded($model, $userContextMock)
- {
- $userContext = new \ReflectionProperty(
- \Magento\Authorization\Model\CompositeUserContext::class,
- 'userContexts'
- );
- $userContext->setAccessible(true);
- $values = $userContext->getValue($model);
- $this->assertCount(1, $values, 'User context is not registered.');
- $this->assertEquals($userContextMock, $values[0], 'User context is registered incorrectly.');
- }
- }
|