Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IntegrationManagerTest.php
Go to the documentation of this file.
1 <?php
8 
15 
16 class IntegrationManagerTest extends \PHPUnit\Framework\TestCase
17 {
21  private $integrationServiceMock;
22 
26  private $oauthServiceMock;
27 
31  private $configMock;
32 
36  private $integrationMock;
37 
41  private $integrationManager;
42 
43  public function setUp()
44  {
45  $objectManagerHelper = new ObjectManagerHelper($this);
46  $this->integrationServiceMock = $this->getMockBuilder(IntegrationServiceInterface::class)
47  ->getMock();
48  $this->configMock = $this->getMockBuilder(Config::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51  $this->oauthServiceMock = $this->getMockBuilder(OauthServiceInterface::class)
52  ->getMock();
53  $this->integrationMock = $this->getMockBuilder(Integration::class)
54  ->disableOriginalConstructor()
55  ->setMethods([
56  'getId',
57  'getConsumerId'
58  ])
59  ->getMock();
60  $this->integrationManager = $objectManagerHelper->getObject(
61  IntegrationManager::class,
62  [
63  'integrationService' => $this->integrationServiceMock,
64  'oauthService' => $this->oauthServiceMock,
65  'config' => $this->configMock
66  ]
67  );
68  }
69 
75  private function getIntegrationUserData($status)
76  {
77  return [
78  'name' => 'ma-integration-user',
79  'status' => $status,
80  'all_resources' => false,
81  'resource' => [
82  'Magento_Analytics::analytics',
83  'Magento_Analytics::analytics_api'
84  ],
85  ];
86  }
87 
92  {
93  $this->integrationServiceMock->expects($this->once())
94  ->method('findByName')
95  ->with('ma-integration-user')
96  ->willReturn($this->integrationMock);
97  $this->integrationMock->expects($this->exactly(2))
98  ->method('getId')
99  ->willReturn(100500);
100  $integrationData = $this->getIntegrationUserData(Integration::STATUS_ACTIVE);
101  $integrationData['integration_id'] = 100500;
102  $this->configMock->expects($this->exactly(2))
103  ->method('getConfigDataValue')
104  ->with('analytics/integration_name', null, null)
105  ->willReturn('ma-integration-user');
106  $this->integrationServiceMock->expects($this->once())
107  ->method('update')
108  ->with($integrationData);
109  $this->assertTrue($this->integrationManager->activateIntegration());
110  }
111 
116  {
117  $this->integrationServiceMock->expects($this->once())
118  ->method('findByName')
119  ->with('ma-integration-user')
120  ->willReturn($this->integrationMock);
121  $this->integrationMock->expects($this->once())
122  ->method('getId')
123  ->willReturn(null);
124  $this->configMock->expects($this->once())
125  ->method('getConfigDataValue')
126  ->with('analytics/integration_name', null, null)
127  ->willReturn('ma-integration-user');
128  $this->integrationServiceMock->expects($this->never())
129  ->method('update');
130  $this->integrationManager->activateIntegration();
131  }
132 
139  public function testGetTokenNewIntegration($integrationId)
140  {
141  $this->configMock->expects($this->atLeastOnce())
142  ->method('getConfigDataValue')
143  ->with('analytics/integration_name', null, null)
144  ->willReturn('ma-integration-user');
145  $this->integrationServiceMock->expects($this->once())
146  ->method('findByName')
147  ->with('ma-integration-user')
148  ->willReturn($this->integrationMock);
149  $this->integrationMock->expects($this->once())
150  ->method('getConsumerId')
151  ->willReturn(100500);
152  $this->integrationMock->expects($this->once())
153  ->method('getId')
154  ->willReturn($integrationId);
155  if (!$integrationId) {
156  $this->integrationServiceMock
157  ->expects($this->once())
158  ->method('create')
159  ->with($this->getIntegrationUserData(Integration::STATUS_INACTIVE))
160  ->willReturn($this->integrationMock);
161  }
162  $this->oauthServiceMock->expects($this->at(0))
163  ->method('getAccessToken')
164  ->with(100500)
165  ->willReturn(false);
166  $this->oauthServiceMock->expects($this->at(2))
167  ->method('getAccessToken')
168  ->with(100500)
169  ->willReturn('IntegrationToken');
170  $this->oauthServiceMock->expects($this->once())
171  ->method('createAccessToken')
172  ->with(100500, true)
173  ->willReturn(true);
174  $this->assertEquals('IntegrationToken', $this->integrationManager->generateToken());
175  }
176 
183  public function testGetTokenExistingIntegration($integrationId)
184  {
185  $this->configMock->expects($this->atLeastOnce())
186  ->method('getConfigDataValue')
187  ->with('analytics/integration_name', null, null)
188  ->willReturn('ma-integration-user');
189  $this->integrationServiceMock->expects($this->once())
190  ->method('findByName')
191  ->with('ma-integration-user')
192  ->willReturn($this->integrationMock);
193  $this->integrationMock->expects($this->once())
194  ->method('getConsumerId')
195  ->willReturn(100500);
196  $this->integrationMock->expects($this->once())
197  ->method('getId')
198  ->willReturn($integrationId);
199  if (!$integrationId) {
200  $this->integrationServiceMock
201  ->expects($this->once())
202  ->method('create')
203  ->with($this->getIntegrationUserData(Integration::STATUS_INACTIVE))
204  ->willReturn($this->integrationMock);
205  }
206  $this->oauthServiceMock->expects($this->once())
207  ->method('getAccessToken')
208  ->with(100500)
209  ->willReturn('IntegrationToken');
210  $this->oauthServiceMock->expects($this->never())
211  ->method('createAccessToken');
212  $this->assertEquals('IntegrationToken', $this->integrationManager->generateToken());
213  }
214 
218  public function integrationIdDataProvider()
219  {
220  return [
221  [1],
222  [null],
223  ];
224  }
225 }
$status
Definition: order_status.php:8