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

/tests/sync_app/Controller/QueueProcessControllerTest.php

https://gitlab.com/vietcoop/sync.app
PHP | 100 lines | 67 code | 17 blank | 16 comment | 0 complexity | 5a473dd98bd0041d01620a98a199fdb0 MD5 | raw file
  1. <?php
  2. namespace GoCatalyze\SyncApp\Testing\Controller;
  3. use GoCatalyze\SyncApp\Controller\QueueProcessController;
  4. use GoCatalyze\SyncApp\Entity\SyncMappingEntity;
  5. use GoCatalyze\SyncApp\Testing\BaseTestCase;
  6. use GoCatalyze\SyncCenter\Entity\Mapping\EntityConvertor;
  7. use GoCatalyze\SyncCenter\Extensions\Drupal\DrupalEntity;
  8. use GoCatalyze\SyncCenter\Extensions\Drupal\DrupalService;
  9. use GoCatalyze\SyncCenter\Extensions\Salesforce\SalesforceEntity;
  10. use GoCatalyze\SyncCenter\Extensions\Salesforce\SalesforceService;
  11. /**
  12. * @group QueueProcessController
  13. */
  14. class QueueProcessControllerTest extends BaseTestCase
  15. {
  16. protected $need_db = true;
  17. public function testProcess()
  18. {
  19. // Process queue item, reload item, make sure the status is changed.
  20. $controller = new QueueProcessController($this->app);
  21. // Dummy
  22. /* @var $sync_mapping SyncMappingEntity */
  23. $job = $this->dummyQueueItem('drupal', 'salesforce', 'create', $this->dummyDrupalInput());
  24. $sync_mapping = $job->getArgs()['sync_mapping'];
  25. $si = $sync_mapping->getSourceServiceInstance();
  26. $di = $sync_mapping->getDestinationServiceInstance();
  27. $drupal_service = new DrupalService();
  28. $drupal_service->setConfiguration($si->getOptions());
  29. $drupal_entity = new DrupalEntity();
  30. $drupal_entity->setAttributeValues($job->getArgs()['attributes']);
  31. $sf_service = new SalesforceService();
  32. $sf_service->setConfiguration($di->getOptions());
  33. $sf_entity = new SalesforceEntity();
  34. $sf_entity['drupal_id__c'] = $job->getArgs()['attributes']['uid'];
  35. $sf_entity['Mail'] = $job->getArgs()['attributes']['mail'];
  36. // controller.doProcessQueueItem > call DrupalService::loadFull()
  37. // > entityFullLoader > should return source entity ifself
  38. $drupal_full_loader = $this->getMockBuilder('GoCatalyze\SyncCenter\Extensions\Drupal\Helper\EntityFullLoader')
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $drupal_full_loader->expects($this->once())
  42. ->method('load')
  43. ->willReturn($drupal_entity);
  44. $drupal_service->setEntityFullLoader($drupal_full_loader);
  45. // Mock › Salesforce client
  46. $sf_client = $this->getMock('GoCatalyze\SyncCenter\Extensions\Salesforce\SalesforceClient');
  47. $sf_client->expects($this->once())
  48. ->method('doCreate')
  49. ->with($sf_entity, 'Contact')
  50. ->willReturn($sf_entity->getAttributes())
  51. ;
  52. $sf_service->setClient($sf_client);
  53. // Mock › manager
  54. $manager = $this->getMock('GoCatalyze\SyncCenter\BaseManager');
  55. // controller.doProcessQueueItem > getService 2 times
  56. $manager->expects($this->any())
  57. ->method('getService')
  58. ->withConsecutive(
  59. ['drupal', $this->anything()], ['salesforce', $this->anything()]
  60. )
  61. ->willReturnOnConsecutiveCalls($drupal_service, $sf_service);
  62. // controller.doProcessQueueItem > create Drupal entity from array
  63. $manager->expects($this->once())
  64. ->method('entityFromArray')
  65. ->with($job->getArgs()['attributes'], 'drupal.entity', 'user')
  66. ->willReturn($drupal_entity);
  67. // controller.doProcessQueueItem > get entity convertor
  68. $manager->expects($this->once())
  69. ->method('getEntityConvertor')
  70. ->willReturn(new EntityConvertor($manager));
  71. // controller.doProcessQueueItem > create empty salesforce entity
  72. $manager->expects($this->once())
  73. ->method('getEntityType')
  74. ->with('salesforce.entity')
  75. ->willReturn(new SalesforceEntity());
  76. $controller->setManager($manager);
  77. // Action
  78. $response = $controller->doGet($job);
  79. // Check result
  80. $this->assertEquals($job->getId(), $response['id']);
  81. $this->assertEquals($sf_entity->getAttributes(), $response['response']);
  82. }
  83. }