PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/sync_app/DrupalToSalesforceTest.php

https://gitlab.com/vietcoop/sync.app
PHP | 228 lines | 159 code | 37 blank | 32 comment | 1 complexity | aac611df8412da428a470f80a8e09aec MD5 | raw file
  1. <?php
  2. namespace GoCatalyze\SyncApp\Testing;
  3. use GoCatalyze\SyncApp\Entity\ServiceInstanceEntity;
  4. use GoCatalyze\SyncApp\Entity\SyncMappingEntity;
  5. use GoCatalyze\SyncCenter\Entity\Mapping\EntityMapping;
  6. use GoCatalyze\SyncCenter\Extensions\Drupal\DrupalEntity;
  7. use GoCatalyze\SyncCenter\Extensions\Salesforce\SalesforceEntity;
  8. use PHPQueue\Job;
  9. use DateTime;
  10. use GoCatalyze\SyncCenter\Entity\EntityInterface;
  11. class DrupalToSalesforceTest extends BaseTestCase
  12. {
  13. /**
  14. * @return ServiceInstanceEntity
  15. */
  16. private function makeDrupalInstance()
  17. {
  18. $instance = ServiceInstanceEntity::fromArray([
  19. 'service_name' => 'drupal',
  20. 'description' => 'Test Drupal instance',
  21. 'options' => [
  22. 'host' => 'http://127.0.0.1/',
  23. 'name' => 'admin',
  24. 'pass' => 'password',
  25. 'endpoints' => [
  26. 'user' => ['GET' => ['/views_user']]
  27. ]
  28. ]
  29. ]);
  30. $em = $this->app->getEntitiyManager();
  31. $em->persist($instance);
  32. $em->flush();
  33. return $instance;
  34. }
  35. private function makeSalesforceInstance()
  36. {
  37. $instance = ServiceInstanceEntity::fromArray([
  38. 'service_name' => 'salesforce',
  39. 'description' => 'Test Salesforce instance',
  40. 'options' => [
  41. 'client_id' => '***',
  42. 'client_secret' => '***',
  43. 'instance_url' => 'https://test.salesforce.com',
  44. 'refresh_token' => '***',
  45. ]
  46. ]);
  47. $em = $this->app->getEntitiyManager();
  48. $em->persist($instance);
  49. $em->flush();
  50. return $instance;
  51. }
  52. /**
  53. * @return SyncMappingEntity
  54. */
  55. private function makeSyncMapping()
  56. {
  57. $sync_mapping = SyncMappingEntity::fromArray([
  58. 'status' => true,
  59. 'description' => 'Drupal user to Salesforce',
  60. 'source' => [
  61. 'service_instance' => $this->makeDrupalInstance(),
  62. 'entity_type' => 'drupal.entity',
  63. 'remote_entity_type' => 'user',
  64. ],
  65. 'destination' => [
  66. 'service_instance' => $this->makeSalesforceInstance(),
  67. 'entity_type' => 'salesforce.entity',
  68. 'remote_entity_type' => 'Contact',
  69. 'unique_fields' => ['SYN_Id__c']
  70. ],
  71. 'mapping' => [
  72. ['source' => 'uid', 'destination' => 'SYN_Id__c'],
  73. ['source' => 'first_name', 'destination' => 'FirstName'],
  74. ['source' => 'last_name', 'destination' => 'LastName'],
  75. ['source' => 'mail', 'destination' => 'Mail'],
  76. ]
  77. ]);
  78. $sync_mapping->setUpdatedAt(new DateTime());
  79. $em = $this->app->getEntitiyManager();
  80. $em->persist($sync_mapping);
  81. $em->flush();
  82. return $sync_mapping;
  83. }
  84. /**
  85. * No config-aware, hardcode a sync mapping object.
  86. *
  87. * @return Job
  88. */
  89. private function makeQueueCommand()
  90. {
  91. $queue_item = new Job();
  92. $queue_item->data = [
  93. 'action' => 'create',
  94. 'sync_mapping' => $this->makeSyncMapping()->getId(),
  95. 'attributes' => [
  96. 'uid' => 1,
  97. 'first_name' => 'Andy',
  98. 'last_name' => 'Truong',
  99. 'mail' => 'heyandy@x.v3k.net'
  100. ]
  101. ];
  102. return $queue_item;
  103. }
  104. /**
  105. * Simple, make sure there is no issue on creating queue item.
  106. */
  107. private function checkQueueCommand()
  108. {
  109. $queue_item = $this->makeQueueCommand();
  110. // Add more assertions here
  111. // …
  112. return $queue_item;
  113. }
  114. private function checkSyncMappingSaving()
  115. {
  116. $queue_item = $this->checkQueueCommand();
  117. // load sync-mapping by ID
  118. /* @var $sync_mapping SyncMappingEntity */
  119. $sync_mapping = $this->getEntityManager()->getRepository('GoCatalyze\SyncApp\Entity\SyncMappingEntity')->find($queue_item->data['sync_mapping']);
  120. // check sync-mapping arguments, all should be saved correctly
  121. $this->assertInstanceOf('GoCatalyze\SyncApp\Entity\SyncMappingEntity', $sync_mapping);
  122. $this->assertEquals('drupal', $sync_mapping->getSourceServiceInstance()->getServiceName());
  123. $this->assertEquals('user', $sync_mapping->getSourceRemoteEntityType());
  124. $this->assertEquals('salesforce', $sync_mapping->getDestinationServiceInstance()->getServiceName());
  125. $this->assertEquals('Contact', $sync_mapping->getDestinationRemoteEntityType());
  126. $this->assertEquals(['SYN_Id__c'], $sync_mapping->getDestinationUniqueFields());
  127. $this->assertEquals(['source' => 'mail', 'destination' => 'Mail'], $sync_mapping->getMappingInfo()[3]);
  128. return $queue_item;
  129. }
  130. private function checkEntityCasting()
  131. {
  132. /* @var $queue_item Job */
  133. /* @var $mapping SyncMappingEntity */
  134. $queue_item = $this->checkSyncMappingSaving();
  135. $input = $queue_item->data['attributes'];
  136. $mapping = $this->getEntityManager()->getRepository('GoCatalyze\SyncApp\Entity\SyncMappingEntity')->find($queue_item->data['sync_mapping']);
  137. $convertor = $this->app->getSyncManager()->getEntityConvertor();
  138. $field_mapping = EntityMapping::fromArray($mapping->getMappingInfo());
  139. $drupal_entity = new DrupalEntity();
  140. $drupal_entity->setAttributeValues($input);
  141. // Convert source entity to destination entity
  142. $sf_entity = $convertor->convert($drupal_entity, 'salesforce.entity', $field_mapping);
  143. $this->assertInstanceOf('GoCatalyze\SyncCenter\Extensions\Salesforce\SalesforceEntity', $sf_entity);
  144. $this->assertEquals([
  145. 'SYN_Id__c' => $input['uid'],
  146. 'FirstName' => $input['first_name'],
  147. 'LastName' => $input['last_name'],
  148. 'Mail' => $input['mail']
  149. ], $sf_entity->getAttributes()
  150. );
  151. return [$queue_item, $mapping, $sf_entity];
  152. }
  153. public function checkDestinationService()
  154. {
  155. /* @var $queue_item Job */
  156. /* @var $sync_mapping SyncMappingEntity */
  157. /* @var $sf_entity SalesforceEntity */
  158. list($queue_item, $sync_mapping, $sf_entity) = $this->checkEntityCasting();
  159. $dest_service = $this->app->getSyncManager()->getService(
  160. $sync_mapping->getDestinationServiceInstance()->getServiceName(), $sync_mapping->getDestinationServiceInstance()->getOptions()
  161. );
  162. $this->assertInstanceOf('GoCatalyze\SyncCenter\Extensions\Salesforce\SalesforceService', $dest_service);
  163. return [$queue_item, $sync_mapping, $sf_entity, $dest_service];
  164. }
  165. /**
  166. * @group heyandy
  167. */
  168. public function testRemotePushing()
  169. {
  170. /* @var $queue_item Job */
  171. /* @var $sync_mapping SyncMappingEntity */
  172. /* @var $sf_entity SalesforceEntityyncCenter\Extensions\Salesforce\SalesforceService */
  173. list($queue_item, $sync_mapping, $sf_entity, $sf_service) = $this->checkDestinationService();
  174. $action = $queue_item->data['action'];
  175. $type = $sync_mapping->getDestinationRemoteEntityType();
  176. $uniques = $sync_mapping->getDestinationUniqueFields();
  177. $client = $this->getMock('GoCatalyze\SyncCenter\BaseClient');
  178. if ($action === 'create') {
  179. /* @var $full_entity EntityInterface */
  180. $full_entity = clone ($sf_entity);
  181. $full_entity->setAttributeValue('Id', 112233);
  182. $client->expects($this->once())
  183. ->method('doCreate')
  184. ->with($sf_entity, $type)
  185. ->willReturn($full_entity);
  186. }
  187. $sf_service->setClient($client);
  188. $response = $sf_service->{$action}($sf_entity, $type, $uniques);
  189. $this->assertEquals(112233, $response['Id']);
  190. }
  191. }