TokenTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\ResourceModel\Oauth;
  7. /**
  8. * Unit test for \Magento\Integration\Model\ResourceModel\Oauth\Token
  9. */
  10. class TokenTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $connectionMock;
  16. /**
  17. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $resourceMock;
  20. /**
  21. * @var \Magento\Integration\Model\ResourceModel\Oauth\Token
  22. */
  23. protected $tokenResource;
  24. protected function setUp()
  25. {
  26. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  27. $this->connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
  28. $this->resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  29. $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock);
  30. $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
  31. $contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
  32. $this->tokenResource = $objectManager->getObject(
  33. \Magento\Integration\Model\ResourceModel\Oauth\Token::class,
  34. ['context' => $contextMock]
  35. );
  36. }
  37. public function testCleanOldAuthorizedTokensExcept()
  38. {
  39. $tokenMock = $this->createPartialMock(
  40. \Magento\Integration\Model\Oauth\Token::class,
  41. ['getId', 'getAuthorized', 'getConsumerId', 'getCustomerId', 'getAdminId']
  42. );
  43. $tokenMock->expects($this->any())->method('getId')->willReturn(1);
  44. $tokenMock->expects($this->once())->method('getAuthorized')->willReturn(true);
  45. $tokenMock->expects($this->any())->method('getCustomerId')->willReturn(1);
  46. $this->connectionMock->expects($this->any())->method('quoteInto');
  47. $this->connectionMock->expects($this->once())->method('delete');
  48. $this->tokenResource->cleanOldAuthorizedTokensExcept($tokenMock);
  49. }
  50. public function testDeleteOldEntries()
  51. {
  52. $this->connectionMock->expects($this->once())->method('delete');
  53. $this->connectionMock->expects($this->once())->method('quoteInto');
  54. $this->tokenResource->deleteOldEntries(5);
  55. }
  56. public function testSelectTokenByType()
  57. {
  58. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  59. $selectMock->expects($this->once())->method('from')->will($this->returnValue($selectMock));
  60. $selectMock->expects($this->exactly(2))->method('where')->will($this->returnValue($selectMock));
  61. $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock);
  62. $this->connectionMock->expects($this->once())->method('fetchRow');
  63. $this->tokenResource->selectTokenByType(5, 'nonce');
  64. }
  65. public function testSelectTokenByConsumerIdAndUserType()
  66. {
  67. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  68. $selectMock->expects($this->once())->method('from')->will($this->returnValue($selectMock));
  69. $selectMock->expects($this->exactly(2))->method('where')->will($this->returnValue($selectMock));
  70. $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock);
  71. $this->connectionMock->expects($this->once())->method('fetchRow');
  72. $this->tokenResource->selectTokenByConsumerIdAndUserType(5, 'nonce');
  73. }
  74. public function testSelectTokenByAdminId()
  75. {
  76. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  77. $selectMock->expects($this->once())->method('from')->will($this->returnValue($selectMock));
  78. $selectMock->expects($this->exactly(2))->method('where')->will($this->returnValue($selectMock));
  79. $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock);
  80. $this->connectionMock->expects($this->once())->method('fetchRow');
  81. $this->tokenResource->selectTokenByAdminId(5);
  82. }
  83. public function testSelectTokenByCustomerId()
  84. {
  85. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  86. $selectMock->expects($this->once())->method('from')->will($this->returnValue($selectMock));
  87. $selectMock->expects($this->exactly(2))->method('where')->will($this->returnValue($selectMock));
  88. $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock);
  89. $this->connectionMock->expects($this->once())->method('fetchRow');
  90. $this->tokenResource->selectTokenByCustomerId(5);
  91. }
  92. }