Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NonceTest.php
Go to the documentation of this file.
1 <?php
8 
12 class NonceTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $nonceModel;
18 
22  protected $contextMock;
23 
27  protected $registryMock;
28 
32  protected $oauthDataMock;
33 
37  protected $resourceMock;
38 
43 
44  protected function setUp()
45  {
46  $this->contextMock = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
47  $eventManagerMock = $this->getMockForAbstractClass(
48  \Magento\Framework\Event\ManagerInterface::class,
49  [],
50  '',
51  false,
52  true,
53  true,
54  ['dispatch']
55  );
56  $this->contextMock->expects($this->once())
57  ->method('getEventDispatcher')
58  ->will($this->returnValue($eventManagerMock));
59  $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
60  $this->oauthDataMock = $this->createMock(\Magento\Integration\Helper\Oauth\Data::class);
61  $this->resourceMock = $this->getMockForAbstractClass(
62  \Magento\Framework\Model\ResourceModel\AbstractResource::class,
63  [],
64  '',
65  false,
66  true,
67  true,
68  ['getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries']
69  );
70  $this->resourceCollectionMock = $this->createMock(\Magento\Framework\Data\Collection\AbstractDb::class);
71  $this->nonceModel = new \Magento\Integration\Model\Oauth\Nonce(
72  $this->contextMock,
73  $this->registryMock,
74  $this->oauthDataMock,
75  $this->resourceMock,
76  $this->resourceCollectionMock
77  );
78  }
79 
80  public function testAfterSave()
81  {
82  $this->oauthDataMock->expects($this->once())
83  ->method('isCleanupProbability')
84  ->will($this->returnValue(true));
85 
86  $this->oauthDataMock->expects($this->once())
87  ->method('getCleanupExpirationPeriod')
88  ->will($this->returnValue(30));
89 
90  $this->resourceMock->expects($this->once())
91  ->method('deleteOldEntries')
92  ->with(30)
93  ->will($this->returnValue(1));
94 
95  $this->assertEquals($this->nonceModel, $this->nonceModel->afterSave());
96  }
97 
99  {
100  $this->oauthDataMock->expects($this->once())
101  ->method('isCleanupProbability')
102  ->will($this->returnValue(false));
103 
104  $this->oauthDataMock->expects($this->never())
105  ->method('getCleanupExpirationPeriod');
106 
107  $this->resourceMock->expects($this->never())
108  ->method('deleteOldEntries');
109 
110  $this->assertEquals($this->nonceModel, $this->nonceModel->afterSave());
111  }
112 
113  public function testLoadByCompositeKey()
114  {
115  $expectedData = ['testData'];
116  $nonce = 'testNonce';
117  $consumerId = 1;
118 
119  $this->resourceMock->expects($this->once())
120  ->method('selectByCompositeKey')
121  ->with($nonce, $consumerId)
122  ->will($this->returnValue($expectedData));
123  $this->nonceModel->loadByCompositeKey($nonce, $consumerId);
124 
125  $this->assertEquals($expectedData, $this->nonceModel->getData());
126  }
127 }