Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SignUpCommandTest.php
Go to the documentation of this file.
1 <?php
7 
16 use Psr\Log\LoggerInterface;
17 
18 class SignUpCommandTest extends \PHPUnit\Framework\TestCase
19 {
23  private $signUpCommand;
24 
28  private $analyticsTokenMock;
29 
33  private $integrationManagerMock;
34 
38  private $integrationToken;
39 
43  private $configMock;
44 
48  private $httpClientMock;
49 
53  private $loggerMock;
54 
58  private $responseResolverMock;
59 
63  protected function setUp()
64  {
65  $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68  $this->integrationManagerMock = $this->getMockBuilder(IntegrationManager::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->integrationToken = $this->getMockBuilder(IntegrationToken::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77  $this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
78  ->disableOriginalConstructor()
79  ->getMock();
80  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $this->responseResolverMock = $this->getMockBuilder(ResponseResolver::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86 
87  $this->signUpCommand = new SignUpCommand(
88  $this->analyticsTokenMock,
89  $this->integrationManagerMock,
90  $this->configMock,
91  $this->httpClientMock,
92  $this->loggerMock,
93  $this->responseResolverMock
94  );
95  }
96 
101  public function testExecuteSuccess()
102  {
103  $this->integrationManagerMock->expects($this->once())
104  ->method('generateToken')
105  ->willReturn($this->integrationToken);
106  $this->integrationManagerMock->expects($this->once())
107  ->method('activateIntegration')
108  ->willReturn(true);
109  $data = $this->getTestData();
110 
111  $this->configMock->expects($this->any())
112  ->method('getValue')
113  ->willReturn($data['url']);
114  $this->integrationToken->expects($this->any())
115  ->method('getData')
116  ->with('token')
117  ->willReturn($data['integration-token']);
118  $httpResponse = new \Zend_Http_Response(201, [], '{"access-token": "' . $data['access-token'] . '"}');
119  $this->httpClientMock->expects($this->once())
120  ->method('request')
121  ->with(
122  $data['method'],
123  $data['url'],
124  $data['body']
125  )
126  ->willReturn($httpResponse);
127  $this->responseResolverMock->expects($this->any())
128  ->method('getResult')
129  ->with($httpResponse)
130  ->willReturn(true);
131  $this->assertTrue($this->signUpCommand->execute());
132  }
133 
138  {
139  $this->integrationManagerMock->expects($this->once())
140  ->method('generateToken')
141  ->willReturn(false);
142  $this->integrationManagerMock->expects($this->never())
143  ->method('activateIntegration');
144  $this->assertFalse($this->signUpCommand->execute());
145  }
146 
152  {
153  $this->integrationManagerMock->expects($this->once())
154  ->method('generateToken')
155  ->willReturn($this->integrationToken);
156  $this->integrationManagerMock->expects($this->once())
157  ->method('activateIntegration')
158  ->willReturn(true);
159  $httpResponse = new \Zend_Http_Response(0, []);
160  $this->httpClientMock->expects($this->once())
161  ->method('request')
162  ->willReturn($httpResponse);
163  $this->responseResolverMock->expects($this->any())
164  ->method('getResult')
165  ->willReturn(false);
166  $this->assertFalse($this->signUpCommand->execute());
167  }
168 
174  private function getTestData()
175  {
176  return [
177  'url' => 'http://www.mystore.com',
178  'access-token' => 'thisisaccesstoken',
179  'integration-token' => 'thisisintegrationtoken',
181  'body'=> ['token' => 'thisisintegrationtoken','url' => 'http://www.mystore.com'],
182  ];
183  }
184 }