/tests/sync_app/BaseTestCase.php

https://gitlab.com/vietcoop/sync.app · PHP · 182 lines · 127 code · 25 blank · 30 comment · 2 complexity · 3766beedd171821ce259c2b9b58b11e8 MD5 · raw file

  1. <?php
  2. namespace GoCatalyze\SyncApp\Testing;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\Tools\SchemaTool;
  5. use GoCatalyze\SyncApp\Application;
  6. use GoCatalyze\SyncApp\Entity\QueueJobEntity;
  7. use GoCatalyze\SyncApp\Entity\ServiceInstanceEntity;
  8. use GoCatalyze\SyncApp\Entity\SyncMappingEntity;
  9. use PHPQueue\Job;
  10. use PHPUnit_Framework_TestCase;
  11. use SyncCenterTestManager;
  12. abstract class BaseTestCase extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Application
  16. *
  17. * @var Application
  18. */
  19. protected $app;
  20. /**
  21. * Override this to true if you need entity CRUD functionality in use cases.
  22. *
  23. * @var boolean
  24. */
  25. protected $need_db = false;
  26. public function setUp()
  27. {
  28. parent::setUp();
  29. $this->app = SyncCenterTestManager::getApplication();
  30. }
  31. /**
  32. * Get entity manager.
  33. *
  34. * @staticvar boolean $ran
  35. * @return EntityManagerInterface
  36. */
  37. public function getEntityManager()
  38. {
  39. static $ran = false;
  40. if (false === $ran) {
  41. $ran = true;
  42. if (true === $this->need_db) {
  43. $this->setupDB();
  44. }
  45. }
  46. return $this->app->getEntitiyManager();
  47. }
  48. private function setupDB()
  49. {
  50. // getting objects
  51. $em = $this->app->getEntitiyManager();
  52. $metadatas = $em->getMetadataFactory()->getAllMetadata();
  53. $schema_tool = new SchemaTool($em);
  54. // drop all schemas
  55. $schema_tool->dropSchema($metadatas);
  56. // recreate schemas
  57. $schema_tool->createSchema($metadatas);
  58. }
  59. /**
  60. * @return ServiceInstanceEntity
  61. */
  62. protected function dummyDrupalServiceInstance()
  63. {
  64. return ServiceInstanceEntity::fromArray([
  65. 'service_name' => 'drupal',
  66. 'description' => 'A demo Drupal Instance',
  67. 'options' => [
  68. 'host' => 'http://127.0.0.1/drupal/entity', 'token' => 'SAMPLE TOKEN',
  69. 'entity_info' => [
  70. 'user' => ['entity keys' => ['id' => 'uid']]
  71. ]
  72. ]
  73. ]);
  74. }
  75. /**
  76. * @return ServiceInstanceEntity
  77. */
  78. protected function dummySalesforceServiceInstance()
  79. {
  80. return ServiceInstanceEntity::fromArray([
  81. 'service_name' => 'salesforce',
  82. 'description' => 'A demo Salesforce Instance',
  83. 'options' => [
  84. 'instance_url' => 'http://unreal_' . __LINE__ . '.salesforce.com/',
  85. 'token' => 'MySaleforceToken',
  86. ],
  87. ]);
  88. }
  89. protected function dummySyncMapping($source, $destination)
  90. {
  91. $_source = [
  92. 'service_instance' => $source === 'drupal' ? $this->dummyDrupalServiceInstance() : $this->dummySalesforceServiceInstance(),
  93. 'entity_type' => $source === 'drupal' ? 'drupal.entity' : 'salesforce.entity',
  94. 'remote_entity_type' => $source === 'drupal' ? 'user' : 'Contact',
  95. ];
  96. $_dest = [
  97. 'service_instance' => $destination === 'drupal' ? $this->dummyDrupalServiceInstance() : $this->dummySalesforceServiceInstance(),
  98. 'entity_type' => $destination === 'drupal' ? 'drupal.entity' : 'salesforce.entity',
  99. 'remote_entity_type' => $destination === 'drupal' ? 'user' : 'Contact',
  100. ];
  101. $mapping = [];
  102. $mapping['drupal'][] = ['source' => 'uid', 'destination' => 'drupal_id__c'];
  103. $mapping['drupal'][] = ['source' => 'mail', 'destination' => 'Mail'];
  104. $mapping['salesforces'][] = ['source' => 'Id', 'destination' => 'field_salesforce_id'];
  105. $mapping['salesforces'][] = ['source' => 'Id', 'Mail' => 'mail'];
  106. return SyncMappingEntity::fromArray([
  107. 'id' => __LINE__,
  108. 'status' => true,
  109. 'description' => 'Drupal to Saleforce mapping',
  110. 'source' => $_source,
  111. 'destination' => $_dest,
  112. 'mapping' => $mapping[$source]
  113. ]);
  114. }
  115. /**
  116. * @param string $action
  117. * @param array $attributes
  118. * @return QueueJobEntity
  119. */
  120. protected function dummyQueueItem($source, $destination, $action, array $attributes)
  121. {
  122. $data = [
  123. 'action' => $action,
  124. 'sync_mapping' => $this->dummySyncMapping($source, $destination),
  125. 'attributes' => $attributes
  126. ];
  127. $job = new QueueJobEntity('syncEntity', $data);
  128. $job->setId(__LINE__ + rand(100, 900));
  129. return $job;
  130. }
  131. protected function dummyDrupalInput()
  132. {
  133. $name = 'john' . (__LINE__ + rand(100, 900));
  134. return [
  135. 'uid' => __LINE__ + rand(100, 900),
  136. 'name' => $name,
  137. 'mail' => $name . '@english.com',
  138. ];
  139. }
  140. protected function dummySalesforceInput()
  141. {
  142. $number = __LINE__ + rand(1000, 9000);
  143. return [
  144. 'Id' => $number,
  145. 'Name' => 'John English Number ' + $number,
  146. 'Mail' => 'john' . $number . '@english.com',
  147. ];
  148. }
  149. protected function dummyEntityQueue($action, $service)
  150. {
  151. return [
  152. 'action' => $action,
  153. 'sync_mapping' => $this->dummySyncMapping($service, $service === 'drupal' ? 'salesforce' : 'drupal'),
  154. 'attributes' => $service === 'drupal' ? $this->dummyDrupalInput() : $this->dummySalesforceInput(),
  155. ];
  156. }
  157. }