NonceTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Oauth;
  7. /**
  8. * Unit test for \Magento\Integration\Model\Oauth\Nonce
  9. */
  10. class NonceTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Integration\Model\Oauth\Nonce
  14. */
  15. protected $nonceModel;
  16. /**
  17. * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $contextMock;
  20. /**
  21. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $registryMock;
  24. /**
  25. * @var \Magento\Integration\Helper\Oauth\Data|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $oauthDataMock;
  28. /**
  29. * @var \Magento\Framework\Model\ResourceModel\AbstractResource|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $resourceMock;
  32. /**
  33. * @var \Magento\Framework\Data\Collection\AbstractDb|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $resourceCollectionMock;
  36. protected function setUp()
  37. {
  38. $this->contextMock = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
  39. $eventManagerMock = $this->getMockForAbstractClass(
  40. \Magento\Framework\Event\ManagerInterface::class,
  41. [],
  42. '',
  43. false,
  44. true,
  45. true,
  46. ['dispatch']
  47. );
  48. $this->contextMock->expects($this->once())
  49. ->method('getEventDispatcher')
  50. ->will($this->returnValue($eventManagerMock));
  51. $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
  52. $this->oauthDataMock = $this->createMock(\Magento\Integration\Helper\Oauth\Data::class);
  53. $this->resourceMock = $this->getMockForAbstractClass(
  54. \Magento\Framework\Model\ResourceModel\AbstractResource::class,
  55. [],
  56. '',
  57. false,
  58. true,
  59. true,
  60. ['getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries']
  61. );
  62. $this->resourceCollectionMock = $this->createMock(\Magento\Framework\Data\Collection\AbstractDb::class);
  63. $this->nonceModel = new \Magento\Integration\Model\Oauth\Nonce(
  64. $this->contextMock,
  65. $this->registryMock,
  66. $this->oauthDataMock,
  67. $this->resourceMock,
  68. $this->resourceCollectionMock
  69. );
  70. }
  71. public function testAfterSave()
  72. {
  73. $this->oauthDataMock->expects($this->once())
  74. ->method('isCleanupProbability')
  75. ->will($this->returnValue(true));
  76. $this->oauthDataMock->expects($this->once())
  77. ->method('getCleanupExpirationPeriod')
  78. ->will($this->returnValue(30));
  79. $this->resourceMock->expects($this->once())
  80. ->method('deleteOldEntries')
  81. ->with(30)
  82. ->will($this->returnValue(1));
  83. $this->assertEquals($this->nonceModel, $this->nonceModel->afterSave());
  84. }
  85. public function testAfterSaveNoCleanupProbability()
  86. {
  87. $this->oauthDataMock->expects($this->once())
  88. ->method('isCleanupProbability')
  89. ->will($this->returnValue(false));
  90. $this->oauthDataMock->expects($this->never())
  91. ->method('getCleanupExpirationPeriod');
  92. $this->resourceMock->expects($this->never())
  93. ->method('deleteOldEntries');
  94. $this->assertEquals($this->nonceModel, $this->nonceModel->afterSave());
  95. }
  96. public function testLoadByCompositeKey()
  97. {
  98. $expectedData = ['testData'];
  99. $nonce = 'testNonce';
  100. $consumerId = 1;
  101. $this->resourceMock->expects($this->once())
  102. ->method('selectByCompositeKey')
  103. ->with($nonce, $consumerId)
  104. ->will($this->returnValue($expectedData));
  105. $this->nonceModel->loadByCompositeKey($nonce, $consumerId);
  106. $this->assertEquals($expectedData, $this->nonceModel->getData());
  107. }
  108. }