Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OTPRequestTest.php
Go to the documentation of this file.
1 <?php
7 
13 use Psr\Log\LoggerInterface;
14 
18 class OTPRequestTest extends \PHPUnit\Framework\TestCase
19 {
23  private $subject;
24 
28  private $loggerMock;
29 
33  private $configMock;
34 
38  private $httpClientMock;
39 
43  private $analyticsTokenMock;
44 
48  private $responseResolverMock;
49 
53  public function setUp()
54  {
55  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
56  ->disableOriginalConstructor()
57  ->getMock();
58 
59  $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62 
63  $this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
64  ->disableOriginalConstructor()
65  ->getMock();
66 
67  $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70 
71  $this->responseResolverMock = $this->getMockBuilder(ResponseResolver::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74 
75  $this->subject = new OTPRequest(
76  $this->analyticsTokenMock,
77  $this->httpClientMock,
78  $this->configMock,
79  $this->responseResolverMock,
80  $this->loggerMock
81  );
82  }
83 
89  private function getTestData()
90  {
91  return [
92  'otp' => 'thisisotp',
93  'url' => 'http://www.mystore.com',
94  'access-token' => 'thisisaccesstoken',
96  'body'=> ['access-token' => 'thisisaccesstoken','url' => 'http://www.mystore.com'],
97  ];
98  }
99 
103  public function testCallSuccess()
104  {
105  $data = $this->getTestData();
106 
107  $this->analyticsTokenMock->expects($this->once())
108  ->method('isTokenExist')
109  ->willReturn(true);
110  $this->analyticsTokenMock->expects($this->once())
111  ->method('getToken')
112  ->willReturn($data['access-token']);
113 
114  $this->configMock->expects($this->any())
115  ->method('getValue')
116  ->willReturn($data['url']);
117 
118  $this->httpClientMock->expects($this->once())
119  ->method('request')
120  ->with(
121  $data['method'],
122  $data['url'],
123  $data['body']
124  )
125  ->willReturn(new \Zend_Http_Response(201, []));
126  $this->responseResolverMock->expects($this->once())
127  ->method('getResult')
128  ->willReturn($data['otp']);
129 
130  $this->assertEquals(
131  $data['otp'],
132  $this->subject->call()
133  );
134  }
135 
139  public function testCallNoAccessToken()
140  {
141  $this->analyticsTokenMock->expects($this->once())
142  ->method('isTokenExist')
143  ->willReturn(false);
144 
145  $this->httpClientMock->expects($this->never())
146  ->method('request');
147 
148  $this->assertFalse($this->subject->call());
149  }
150 
154  public function testCallNoOtp()
155  {
156  $data = $this->getTestData();
157 
158  $this->analyticsTokenMock->expects($this->once())
159  ->method('isTokenExist')
160  ->willReturn(true);
161  $this->analyticsTokenMock->expects($this->once())
162  ->method('getToken')
163  ->willReturn($data['access-token']);
164 
165  $this->configMock->expects($this->any())
166  ->method('getValue')
167  ->willReturn($data['url']);
168 
169  $this->httpClientMock->expects($this->once())
170  ->method('request')
171  ->with(
172  $data['method'],
173  $data['url'],
174  $data['body']
175  )
176  ->willReturn(new \Zend_Http_Response(0, []));
177 
178  $this->responseResolverMock->expects($this->once())
179  ->method('getResult')
180  ->willReturn(false);
181 
182  $this->loggerMock->expects($this->once())
183  ->method('warning');
184 
185  $this->assertFalse($this->subject->call());
186  }
187 }