CustomerTokenServiceTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Model;
  7. use Magento\Integration\Model\Integration;
  8. use Magento\Integration\Model\Oauth\Token;
  9. class CustomerTokenServiceTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** \Magento\Integration\Model\CustomerTokenService */
  12. protected $_tokenService;
  13. /** \Magento\Integration\Model\Oauth\TokenFactory|\PHPUnit_Framework_MockObject_MockObject */
  14. protected $_tokenFactoryMock;
  15. /** \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $_accountManagementMock;
  17. /** \Magento\Integration\Model\ResourceModel\Oauth\Token\Collection|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $_tokenModelCollectionMock;
  19. /** \PHPUnit_Framework_MockObject_MockObject */
  20. protected $_tokenModelCollectionFactoryMock;
  21. /** @var \Magento\Integration\Model\CredentialsValidator|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $validatorHelperMock;
  23. /** @var \Magento\Integration\Model\Oauth\Token|\PHPUnit_Framework_MockObject_MockObject */
  24. private $_tokenMock;
  25. /** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $manager;
  27. protected function setUp()
  28. {
  29. $this->_tokenFactoryMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\TokenFactory::class)
  30. ->setMethods(['create'])
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
  34. $this->_accountManagementMock = $this
  35. ->getMockBuilder(\Magento\Customer\Api\AccountManagementInterface::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->_tokenMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\Token::class)
  39. ->disableOriginalConstructor()
  40. ->setMethods(['getToken', 'loadByCustomerId', 'delete', '__wakeup'])->getMock();
  41. $this->_tokenModelCollectionMock = $this->getMockBuilder(
  42. \Magento\Integration\Model\ResourceModel\Oauth\Token\Collection::class
  43. )->disableOriginalConstructor()->setMethods(
  44. ['addFilterByCustomerId', 'getSize', '__wakeup', '_beforeLoad', '_afterLoad', 'getIterator', '_fetchAll']
  45. )->getMock();
  46. $this->_tokenModelCollectionFactoryMock = $this->getMockBuilder(
  47. \Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory::class
  48. )->setMethods(['create'])->disableOriginalConstructor()->getMock();
  49. $this->_tokenModelCollectionFactoryMock->expects($this->once())
  50. ->method('create')
  51. ->will($this->returnValue($this->_tokenModelCollectionMock));
  52. $this->validatorHelperMock = $this->getMockBuilder(
  53. \Magento\Integration\Model\CredentialsValidator::class
  54. )->disableOriginalConstructor()->getMock();
  55. $this->manager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  56. $this->_tokenService = new \Magento\Integration\Model\CustomerTokenService(
  57. $this->_tokenFactoryMock,
  58. $this->_accountManagementMock,
  59. $this->_tokenModelCollectionFactoryMock,
  60. $this->validatorHelperMock,
  61. $this->manager
  62. );
  63. }
  64. public function testRevokeCustomerAccessToken()
  65. {
  66. $customerId = 1;
  67. $this->_tokenModelCollectionMock->expects($this->once())
  68. ->method('addFilterByCustomerId')
  69. ->with($customerId)
  70. ->will($this->returnValue($this->_tokenModelCollectionMock));
  71. $this->_tokenModelCollectionMock->expects($this->any())
  72. ->method('getSize')
  73. ->will($this->returnValue(1));
  74. $this->_tokenModelCollectionMock->expects($this->once())
  75. ->method('getIterator')
  76. ->will($this->returnValue(new \ArrayIterator([$this->_tokenMock])));
  77. $this->_tokenModelCollectionMock->expects($this->any())
  78. ->method('_fetchAll')
  79. ->will($this->returnValue(1));
  80. $this->_tokenMock->expects($this->once())
  81. ->method('delete')
  82. ->will($this->returnValue($this->_tokenMock));
  83. $this->assertTrue($this->_tokenService->revokeCustomerAccessToken($customerId));
  84. }
  85. /**
  86. * @expectedException \Magento\Framework\Exception\LocalizedException
  87. * @expectedExceptionMessage This customer has no tokens.
  88. */
  89. public function testRevokeCustomerAccessTokenWithoutCustomerId()
  90. {
  91. $this->_tokenModelCollectionMock->expects($this->once())
  92. ->method('addFilterByCustomerId')
  93. ->with(null)
  94. ->will($this->returnValue($this->_tokenModelCollectionMock));
  95. $this->_tokenMock->expects($this->never())
  96. ->method('delete')
  97. ->will($this->returnValue($this->_tokenMock));
  98. $this->_tokenService->revokeCustomerAccessToken(null);
  99. }
  100. /**
  101. * @expectedException \Magento\Framework\Exception\LocalizedException
  102. * @expectedExceptionMessage The tokens couldn't be revoked.
  103. */
  104. public function testRevokeCustomerAccessTokenCannotRevoked()
  105. {
  106. $exception = new \Exception();
  107. $customerId = 1;
  108. $this->_tokenModelCollectionMock->expects($this->once())
  109. ->method('addFilterByCustomerId')
  110. ->with($customerId)
  111. ->will($this->returnValue($this->_tokenModelCollectionMock));
  112. $this->_tokenModelCollectionMock->expects($this->once())
  113. ->method('getSize')
  114. ->will($this->returnValue(1));
  115. $this->_tokenModelCollectionMock->expects($this->once())
  116. ->method('getIterator')
  117. ->will($this->returnValue(new \ArrayIterator([$this->_tokenMock])));
  118. $this->_tokenMock->expects($this->once())
  119. ->method('delete')
  120. ->will($this->throwException($exception));
  121. $this->_tokenService->revokeCustomerAccessToken($customerId);
  122. }
  123. }