Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ManagerTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Integration\Model\Integration;
9 
13 class ManagerTest extends \PHPUnit\Framework\TestCase
14 {
21 
25  protected $aclRetriever;
26 
30  protected $configMock;
31 
38 
39  protected function setUp()
40  {
41  $this->integrationServiceMock = $this->getMockBuilder(
42  \Magento\Integration\Api\IntegrationServiceInterface::class
43  )->disableOriginalConstructor()->setMethods(
44  [
45  'findByName',
46  'update',
47  'create',
48  'get',
49  'findByConsumerId',
50  'findActiveIntegrationByConsumerId',
51  'delete',
52  'getSelectedResources'
53  ]
54  )->getMock();
55 
56  $this->aclRetriever = $this->getMockBuilder(\Magento\Authorization\Model\Acl\AclRetriever::class)
57  ->disableOriginalConstructor()
58  ->setMethods([])
59  ->getMock();
60 
61  $this->configMock = $this->getMockBuilder(\Magento\Integration\Model\Config::class)
62  ->disableOriginalConstructor()
63  ->setMethods([])
64  ->getMock();
65  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
66 
67  $this->integrationManager = $objectManagerHelper->getObject(
68  \Magento\Integration\Model\ConfigBasedIntegrationManager::class,
69  [
70  'integrationService' => $this->integrationServiceMock,
71  'aclRetriever' => $this->aclRetriever,
72  'integrationConfig' => $this->configMock
73  ]
74  );
75  }
76 
77  public function tearDown()
78  {
79  unset($this->integrationServiceMock);
80  unset($this->integrationManager);
81  }
82 
84  {
85  $this->configMock->expects($this->never())->method('getIntegrations');
86  $this->integrationManager->processIntegrationConfig([]);
87  }
88 
90  {
91  $this->configMock->expects(
92  $this->once()
93  )->method(
94  'getIntegrations'
95  )->will(
96  $this->returnValue(
97  [
98  'TestIntegration1' => [
99  'email' => '[email protected]',
100  'endpoint_url' => 'http://endpoint.com',
101  'identity_link_url' => 'http://www.example.com/identity',
102  ],
103  'TestIntegration2' => ['email' => '[email protected]'],
104  ]
105  )
106  );
107  $intLookupData1 = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
108  ->disableOriginalConstructor()
109  ->setMethods([])
110  ->getMock();
111  $intLookupData1->expects($this->any())->method('getId')->willReturn(1);
112  $intLookupData2 = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
113  ->disableOriginalConstructor()
114  ->setMethods([])
115  ->getMock();
116  $intLookupData1->expects($this->any())->method('getId')->willReturn(false);
117 
118  $intUpdateData1 = [
119  Integration::ID => 1,
120  Integration::NAME => 'TestIntegration1',
122  Integration::ENDPOINT => 'http://endpoint.com',
123  Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
125  ];
126  $integrationsData2 = [
127  Integration::NAME => 'TestIntegration2',
130  ];
131  $this->integrationServiceMock->expects(
132  $this->at(0)
133  )->method(
134  'findByName'
135  )->with(
136  'TestIntegration1'
137  )->will(
138  $this->returnValue($intLookupData1)
139  );
140  $this->integrationServiceMock->expects($this->once())->method('create')->with($integrationsData2);
141  $this->integrationServiceMock->expects(
142  $this->at(2)
143  )->method(
144  'findByName'
145  )->with(
146  'TestIntegration2'
147  )->will(
148  $this->returnValue($intLookupData2)
149  );
150  $this->integrationServiceMock->expects($this->at(1))->method('update')->with($intUpdateData1);
151  $this->integrationManager->processIntegrationConfig(['TestIntegration1', 'TestIntegration2']);
152  }
153 
155  {
156  $originalData = [
157  Integration::ID => 1,
158  Integration::NAME => 'TestIntegration1',
160  Integration::ENDPOINT => 'http://endpoint.com',
161  Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
163  ];
164  $integrations = [
165  'TestIntegration1' => [
167  Integration::ENDPOINT => 'http://endpoint.com',
168  Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
169  'resources' => [
170  'Magento_Customer::manage',
171  'Magento_Customer::customer'
172  ]
173  ]
174  ];
175  $originalResources = [
176  'Magento_Customer::manage'
177  ];
178  $newResources = [
179  'Magento_Customer::manage',
180  'Magento_Customer::customer'
181  ];
182 
183  $integrationObject = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
184  ->disableOriginalConstructor()
185  ->setMethods([])
186  ->getMock();
187 
188  // Integration already exists, so update with new data and recreate
189  $this->integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will(
190  $this->returnValue($integrationObject)
191  );
192  $this->aclRetriever->expects($this->once())->method('getAllowedResourcesByUser')
193  ->willReturn($originalResources);
194  $integrationObject->expects($this->any())->method('getId')->willReturn($originalData[Integration::ID]);
195  $this->integrationServiceMock->expects($this->once())->method('update')->willReturn($integrationObject);
196 
197  $integrationObject->expects($this->once())->method('getOrigData')->willReturn($originalData);
198  $integrationObject->expects($this->once())->method('getData')->willReturn($newResources);
199 
200  $this->integrationServiceMock->expects($this->once())->method('create');
201 
202  $this->integrationManager->processConfigBasedIntegrations($integrations);
203  }
204 
206  {
207  $integrations = [
208  'TestIntegration1' => [
210  Integration::ENDPOINT => 'http://endpoint.com',
211  Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
212  'resources' => [
213  'Magento_Customer::manage',
214  'Magento_Customer::customer'
215  ]
216  ],
217  'TestIntegration2' => [
219  Integration::ENDPOINT => 'http://endpoint.com',
220  Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity',
221  ]
222  ];
223 
224  $integrationObject = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
225  ->disableOriginalConstructor()
226  ->setMethods([])
227  ->getMock();
228 
229  // Integration1 does not exist, so create it
230  $this->integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will(
231  $this->returnValue($integrationObject)
232  );
233  $integrationObject->expects($this->any())->method('getId')->willReturn(false);
234  $this->integrationServiceMock->expects($this->any())->method('create');
235 
236  // Integration2 does not exist, so create it
237  $this->integrationServiceMock->expects($this->at(2))->method('findByName')->with('TestIntegration2')->will(
238  $this->returnValue($integrationObject)
239  );
240 
241  $this->integrationManager->processConfigBasedIntegrations($integrations);
242  }
243 }