OTPRequestTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Model\Connector;
  7. use Magento\Analytics\Model\AnalyticsToken;
  8. use Magento\Analytics\Model\Connector\Http\ClientInterface;
  9. use Magento\Analytics\Model\Connector\Http\ResponseResolver;
  10. use Magento\Analytics\Model\Connector\OTPRequest;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * A unit test for testing of the representation of a 'OTP' request.
  15. */
  16. class OTPRequestTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var OTPRequest
  20. */
  21. private $subject;
  22. /**
  23. * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $loggerMock;
  26. /**
  27. * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $configMock;
  30. /**
  31. * @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $httpClientMock;
  34. /**
  35. * @var AnalyticsToken|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $analyticsTokenMock;
  38. /**
  39. * @var ResponseResolver|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. private $responseResolverMock;
  42. /**
  43. * @return void
  44. */
  45. public function setUp()
  46. {
  47. $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->responseResolverMock = $this->getMockBuilder(ResponseResolver::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->subject = new OTPRequest(
  63. $this->analyticsTokenMock,
  64. $this->httpClientMock,
  65. $this->configMock,
  66. $this->responseResolverMock,
  67. $this->loggerMock
  68. );
  69. }
  70. /**
  71. * Returns test parameters for request.
  72. *
  73. * @return array
  74. */
  75. private function getTestData()
  76. {
  77. return [
  78. 'otp' => 'thisisotp',
  79. 'url' => 'http://www.mystore.com',
  80. 'access-token' => 'thisisaccesstoken',
  81. 'method' => \Magento\Framework\HTTP\ZendClient::POST,
  82. 'body'=> ['access-token' => 'thisisaccesstoken','url' => 'http://www.mystore.com'],
  83. ];
  84. }
  85. /**
  86. * @return void
  87. */
  88. public function testCallSuccess()
  89. {
  90. $data = $this->getTestData();
  91. $this->analyticsTokenMock->expects($this->once())
  92. ->method('isTokenExist')
  93. ->willReturn(true);
  94. $this->analyticsTokenMock->expects($this->once())
  95. ->method('getToken')
  96. ->willReturn($data['access-token']);
  97. $this->configMock->expects($this->any())
  98. ->method('getValue')
  99. ->willReturn($data['url']);
  100. $this->httpClientMock->expects($this->once())
  101. ->method('request')
  102. ->with(
  103. $data['method'],
  104. $data['url'],
  105. $data['body']
  106. )
  107. ->willReturn(new \Zend_Http_Response(201, []));
  108. $this->responseResolverMock->expects($this->once())
  109. ->method('getResult')
  110. ->willReturn($data['otp']);
  111. $this->assertEquals(
  112. $data['otp'],
  113. $this->subject->call()
  114. );
  115. }
  116. /**
  117. * @return void
  118. */
  119. public function testCallNoAccessToken()
  120. {
  121. $this->analyticsTokenMock->expects($this->once())
  122. ->method('isTokenExist')
  123. ->willReturn(false);
  124. $this->httpClientMock->expects($this->never())
  125. ->method('request');
  126. $this->assertFalse($this->subject->call());
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testCallNoOtp()
  132. {
  133. $data = $this->getTestData();
  134. $this->analyticsTokenMock->expects($this->once())
  135. ->method('isTokenExist')
  136. ->willReturn(true);
  137. $this->analyticsTokenMock->expects($this->once())
  138. ->method('getToken')
  139. ->willReturn($data['access-token']);
  140. $this->configMock->expects($this->any())
  141. ->method('getValue')
  142. ->willReturn($data['url']);
  143. $this->httpClientMock->expects($this->once())
  144. ->method('request')
  145. ->with(
  146. $data['method'],
  147. $data['url'],
  148. $data['body']
  149. )
  150. ->willReturn(new \Zend_Http_Response(0, []));
  151. $this->responseResolverMock->expects($this->once())
  152. ->method('getResult')
  153. ->willReturn(false);
  154. $this->loggerMock->expects($this->once())
  155. ->method('warning');
  156. $this->assertFalse($this->subject->call());
  157. }
  158. }