123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Analytics\Test\Unit\Model\Connector;
- use Magento\Analytics\Model\AnalyticsToken;
- use Magento\Analytics\Model\Connector\Http\ClientInterface;
- use Magento\Analytics\Model\Connector\Http\ResponseResolver;
- use Magento\Analytics\Model\Connector\OTPRequest;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Psr\Log\LoggerInterface;
- /**
- * A unit test for testing of the representation of a 'OTP' request.
- */
- class OTPRequestTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var OTPRequest
- */
- private $subject;
- /**
- * @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $loggerMock;
- /**
- * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $configMock;
- /**
- * @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $httpClientMock;
- /**
- * @var AnalyticsToken|\PHPUnit_Framework_MockObject_MockObject
- */
- private $analyticsTokenMock;
- /**
- * @var ResponseResolver|\PHPUnit_Framework_MockObject_MockObject
- */
- private $responseResolverMock;
- /**
- * @return void
- */
- public function setUp()
- {
- $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->responseResolverMock = $this->getMockBuilder(ResponseResolver::class)
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->subject = new OTPRequest(
- $this->analyticsTokenMock,
- $this->httpClientMock,
- $this->configMock,
- $this->responseResolverMock,
- $this->loggerMock
- );
- }
- /**
- * Returns test parameters for request.
- *
- * @return array
- */
- private function getTestData()
- {
- return [
- 'otp' => 'thisisotp',
- 'url' => 'http://www.mystore.com',
- 'access-token' => 'thisisaccesstoken',
- 'method' => \Magento\Framework\HTTP\ZendClient::POST,
- 'body'=> ['access-token' => 'thisisaccesstoken','url' => 'http://www.mystore.com'],
- ];
- }
- /**
- * @return void
- */
- public function testCallSuccess()
- {
- $data = $this->getTestData();
- $this->analyticsTokenMock->expects($this->once())
- ->method('isTokenExist')
- ->willReturn(true);
- $this->analyticsTokenMock->expects($this->once())
- ->method('getToken')
- ->willReturn($data['access-token']);
- $this->configMock->expects($this->any())
- ->method('getValue')
- ->willReturn($data['url']);
- $this->httpClientMock->expects($this->once())
- ->method('request')
- ->with(
- $data['method'],
- $data['url'],
- $data['body']
- )
- ->willReturn(new \Zend_Http_Response(201, []));
- $this->responseResolverMock->expects($this->once())
- ->method('getResult')
- ->willReturn($data['otp']);
- $this->assertEquals(
- $data['otp'],
- $this->subject->call()
- );
- }
- /**
- * @return void
- */
- public function testCallNoAccessToken()
- {
- $this->analyticsTokenMock->expects($this->once())
- ->method('isTokenExist')
- ->willReturn(false);
- $this->httpClientMock->expects($this->never())
- ->method('request');
- $this->assertFalse($this->subject->call());
- }
- /**
- * @return void
- */
- public function testCallNoOtp()
- {
- $data = $this->getTestData();
- $this->analyticsTokenMock->expects($this->once())
- ->method('isTokenExist')
- ->willReturn(true);
- $this->analyticsTokenMock->expects($this->once())
- ->method('getToken')
- ->willReturn($data['access-token']);
- $this->configMock->expects($this->any())
- ->method('getValue')
- ->willReturn($data['url']);
- $this->httpClientMock->expects($this->once())
- ->method('request')
- ->with(
- $data['method'],
- $data['url'],
- $data['body']
- )
- ->willReturn(new \Zend_Http_Response(0, []));
- $this->responseResolverMock->expects($this->once())
- ->method('getResult')
- ->willReturn(false);
- $this->loggerMock->expects($this->once())
- ->method('warning');
- $this->assertFalse($this->subject->call());
- }
- }
|