CustomerSessionUserContextTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\Authorization;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. /**
  9. * Tests Magento\Customer\Model\Authorization\CustomerSessionUserContext
  10. */
  11. class CustomerSessionUserContextTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  15. */
  16. protected $objectManager;
  17. /**
  18. * @var \Magento\Customer\Model\Authorization\CustomerSessionUserContext
  19. */
  20. protected $customerSessionUserContext;
  21. /**
  22. * @var \Magento\Customer\Model\Session
  23. */
  24. protected $customerSession;
  25. protected function setUp()
  26. {
  27. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->customerSession = $this->getMockBuilder(\Magento\Customer\Model\Session::class)
  29. ->disableOriginalConstructor()
  30. ->setMethods(['getId'])
  31. ->getMock();
  32. $this->customerSessionUserContext = $this->objectManager->getObject(
  33. \Magento\Customer\Model\Authorization\CustomerSessionUserContext::class,
  34. ['customerSession' => $this->customerSession]
  35. );
  36. }
  37. public function testGetUserIdExist()
  38. {
  39. $userId = 1;
  40. $this->setupUserId($userId);
  41. $this->assertEquals($userId, $this->customerSessionUserContext->getUserId());
  42. }
  43. public function testGetUserIdDoesNotExist()
  44. {
  45. $userId = null;
  46. $this->setupUserId($userId);
  47. $this->assertEquals($userId, $this->customerSessionUserContext->getUserId());
  48. }
  49. public function testGetUserType()
  50. {
  51. $this->assertEquals(UserContextInterface::USER_TYPE_CUSTOMER, $this->customerSessionUserContext->getUserType());
  52. }
  53. /**
  54. * @param int|null $userId
  55. * @return void
  56. */
  57. public function setupUserId($userId)
  58. {
  59. $this->customerSession->expects($this->once())
  60. ->method('getId')
  61. ->will($this->returnValue($userId));
  62. }
  63. }