From fe70b23143efa8974689505fad5f12fc7c3c057d Mon Sep 17 00:00:00 2001
From: git <git@787980.no-reply.drupal.org>
Date: Tue, 7 Feb 2017 17:08:58 -0500
Subject: [PATCH] Cleans up all salesforce_pull tests

---
 .../src/Entity/SalesforceMapping.php          |  7 +++
 .../src/Entity/SalesforceMappingInterface.php |  8 ++-
 .../src/Plugin/QueueWorker/PullBase.php       | 40 ++++++--------
 modules/salesforce_pull/src/QueueHandler.php  |  9 +--
 .../tests/src/Unit/PullBaseTest.php           | 55 +++++++------------
 .../tests/src/Unit/PullQueueItemTest.php      |  4 +-
 .../tests/src/Unit/QueueHandlerTest.php       |  4 +-
 7 files changed, 57 insertions(+), 70 deletions(-)

diff --git a/modules/salesforce_mapping/src/Entity/SalesforceMapping.php b/modules/salesforce_mapping/src/Entity/SalesforceMapping.php
index 6399ac59..bbd915c0 100644
--- a/modules/salesforce_mapping/src/Entity/SalesforceMapping.php
+++ b/modules/salesforce_mapping/src/Entity/SalesforceMapping.php
@@ -294,6 +294,13 @@ class SalesforceMapping extends ConfigEntityBase implements SalesforceMappingInt
     );
   }
 
+  /**
+   * @return string
+   */
+  public function getPullTriggerDate() {
+    return $this->pull_trigger_date;
+  }
+
   public function doesPush() {
     return $this->checkTriggers([
       MappingConstants::SALESFORCE_MAPPING_SYNC_DRUPAL_CREATE,
diff --git a/modules/salesforce_mapping/src/Entity/SalesforceMappingInterface.php b/modules/salesforce_mapping/src/Entity/SalesforceMappingInterface.php
index 0a2ec485..d5cd9fb7 100644
--- a/modules/salesforce_mapping/src/Entity/SalesforceMappingInterface.php
+++ b/modules/salesforce_mapping/src/Entity/SalesforceMappingInterface.php
@@ -25,7 +25,11 @@ interface SalesforceMappingInterface {
 
   public function getDrupalBundle();
 
-  public function id();
+  public function getPullFieldsArray();
+
+  public function getPullTriggerDate();
+
+  //public function id();
 
   public function checkTriggers(array $triggers);
 
@@ -49,5 +53,5 @@ interface SalesforceMappingInterface {
    * @return string
    */
   public function getKeyValue(EntityInterface $entity);
-  
+
 }
diff --git a/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php b/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php
index 83eda416..5a9af261 100644
--- a/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php
+++ b/modules/salesforce_pull/src/Plugin/QueueWorker/PullBase.php
@@ -28,6 +28,7 @@ use Drupal\salesforce_mapping\SalesforceMappingStorage;
 use Drupal\salesforce_mapping\SalesforcePullEvent;
 use Psr\Log\LogLevel;
 use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 /**
  * Provides base functionality for the Salesforce Pull Queue Workers.
@@ -84,7 +85,7 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $etm
    *   The entity type manager.
    */
-  public function __construct(EntityTypeManagerInterface $entity_type_manager, RestClient $client, ModuleHandlerInterface $module_handler, LoggerChannelFactoryInterface $logger_factory, ContainerAwareEventDispatcher $event_dispatcher) {
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, RestClient $client, ModuleHandlerInterface $module_handler, LoggerChannelFactoryInterface $logger_factory, EventDispatcherInterface $event_dispatcher) {
     $this->etm = $entity_type_manager;
     $this->client = $client;
     $this->mh = $module_handler;
@@ -146,17 +147,12 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
    */
   protected function updateEntity(SalesforceMappingInterface $mapping, MappedObjectInterface $mapped_object, SObject $sf_object) {
     if (!$mapping->checkTriggers([MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE])) {
-      echo __LINE__.PHP_EOL;
-      return;
+            return;
     }
 
     try {
-      echo __LINE__.PHP_EOL;
-      //print_r($mapped_object->entity_type_id);
       $entity = $this->etm->getStorage($mapped_object->entity_type_id->value)
         ->load($mapped_object->entity_id->value);
-      //print_r($entity);
-      echo __LINE__.PHP_EOL;
       if (!$entity) {
         $this->logger->log(
           LogLevel::ERROR,
@@ -166,14 +162,12 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
             '%msg' => $e->getMessage(),
           ]
         );
-        echo __LINE__.PHP_EOL;
-        return;
+                return;
       }
 
       // Flag this entity as having been processed. This does not persist,
       // but is used by salesforce_push to avoid duplicate processing.
       $entity->salesforce_pull = TRUE;
-      echo __LINE__.PHP_EOL;
 
       print_r(empty($entity->changed->value));
       $entity_updated = !empty($entity->changed->value)
@@ -181,9 +175,8 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
         : $mapped_object->get('entity_updated');
 
       $pull_trigger_date =
-        $sf_object->field($mapping->get('pull_trigger_date'));
+        $sf_object->field($mapping->getPullTriggerDate());
       $sf_record_updated = strtotime($pull_trigger_date);
-      echo __LINE__.PHP_EOL;
 
       $mapped_object
         ->setDrupalEntity($entity)
@@ -191,18 +184,16 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
 
       $this->event_dispatcher->dispatch(
         SalesforceEvents::PULL_PREPULL,
-        new SalesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE)
+        //new SalesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE)
+        $this->salesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE)
       );
-      echo __LINE__.PHP_EOL;
 
       // @TODO allow some means for contrib to force pull regardless
       // of updated dates
       if ($sf_record_updated > $entity_updated) {
         // Set fields values on the Drupal entity.
-        echo __LINE__.PHP_EOL;
-        $mapped_object->pull();
-        echo __LINE__.PHP_EOL;
-        $this->logger->log(
+                $mapped_object->pull();
+                $this->logger->log(
           LogLevel::NOTICE,
           'Updated entity %label associated with Salesforce Object ID: %sfid',
           [
@@ -210,14 +201,12 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
             '%sfid' => (string)$sf_object->id(),
           ]
         );
-        echo __LINE__.PHP_EOL;
-        return MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE;
+                return MappingConstants::SALESFORCE_MAPPING_SYNC_SF_UPDATE;
       }
     }
     catch (\Exception $e) {
       var_dump(Error::decodeException($e));
-      echo __LINE__.PHP_EOL;
-      $this->logger->log(
+            $this->logger->log(
         LogLevel::ERROR,
         'Failed to update entity %label from Salesforce object %sfobjectid. Error: %msg',
         [
@@ -246,7 +235,6 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
     if (!$mapping->checkTriggers([MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE])) {
       return;
     }
-    echo __LINE__.PHP_EOL;
 
     try {
       // Define values to pass to entity_create().
@@ -279,7 +267,7 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
 
       $this->event_dispatcher->dispatch(
         SalesforceEvents::PULL_PREPULL,
-        new SalesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE)
+        $this->salesforcePullEvent($mapped_object, MappingConstants::SALESFORCE_MAPPING_SYNC_SF_CREATE)
       );
 
       $mapped_object->pull();
@@ -320,4 +308,8 @@ abstract class PullBase extends QueueWorkerBase implements ContainerFactoryPlugi
       );
     }
   }
+
+  public function salesforcePullEvent($mapped_object, $mapping_constant) {
+    return new SalesforcePullEvent($mapped_object, $mapping_constant);
+  }
 }
diff --git a/modules/salesforce_pull/src/QueueHandler.php b/modules/salesforce_pull/src/QueueHandler.php
index ad76bf21..9d7c8a1e 100644
--- a/modules/salesforce_pull/src/QueueHandler.php
+++ b/modules/salesforce_pull/src/QueueHandler.php
@@ -17,6 +17,7 @@ use Drupal\salesforce\SelectQueryResult;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LogLevel;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 /**
  * Handles pull cron queue set up.
@@ -35,7 +36,7 @@ class QueueHandler {
   protected $logger;
   protected $request;
 
-  public function __construct(RestClient $sfapi, array $mappings, QueueInterface $queue, StateInterface $state, LoggerInterface $logger, ContainerAwareEventDispatcher $event_dispatcher, Request $request) {
+  public function __construct(RestClient $sfapi, array $mappings, QueueInterface $queue, StateInterface $state, LoggerInterface $logger, EventDispatcherInterface $event_dispatcher, Request $request) {
     $this->sfapi = $sfapi;
     $this->queue = $queue;
     $this->state = $state;
@@ -57,7 +58,7 @@ class QueueHandler {
    *
    * @return QueueHandler
    */
-  public static function create(RestClient $sfapi, array $mappings, QueueInterface $queue, StateInterface $state, LoggerInterface $logger, ContainerAwareEventDispatcher $event_dispatcher, Request $request) {
+  public static function create(RestClient $sfapi, array $mappings, QueueInterface $queue, StateInterface $state, LoggerInterface $logger, EventDispatcherInterface $event_dispatcher, Request $request) {
     return new QueueHandler($sfapi, $mappings, $queue, $state, $logger, $event_dispatcher, $request);
   }
 
@@ -124,7 +125,7 @@ class QueueHandler {
     $soql = new SelectQuery($mapping->getSalesforceObjectType());
 
     // Convert field mappings to SOQL.
-    $soql->fields = ['Id', $mapping->get('pull_trigger_date')];
+    $soql->fields = ['Id', $mapping->getPullTriggerDate()];
     $mapped_fields = $this->pull_fields[$mapping->getSalesforceObjectType()];
     foreach ($mapped_fields as $field) {
       $soql->fields[] = $field;
@@ -134,7 +135,7 @@ class QueueHandler {
     $sf_last_sync = $this->state->get('salesforce_pull_last_sync_' . $mapping->getSalesforceObjectType(), NULL);
     if ($sf_last_sync) {
       $last_sync = gmdate('Y-m-d\TH:i:s\Z', $sf_last_sync);
-      $soql->addCondition($mapping->get('pull_trigger_date'), $last_sync, '>');
+      $soql->addCondition($mapping->getPullTriggerDate(), $last_sync, '>');
     }
 
     $soql->fields = array_unique($soql->fields);
diff --git a/modules/salesforce_pull/tests/src/Unit/PullBaseTest.php b/modules/salesforce_pull/tests/src/Unit/PullBaseTest.php
index f06bd2e3..7be9a813 100644
--- a/modules/salesforce_pull/tests/src/Unit/PullBaseTest.php
+++ b/modules/salesforce_pull/tests/src/Unit/PullBaseTest.php
@@ -1,32 +1,31 @@
 <?php
 namespace Drupal\Tests\salesforce_pull\Unit;
 
-use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
+//use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
 use Drupal\Core\Config\Entity\ConfigEntityStorage;
 use Drupal\Core\Entity\ContentEntityBase;
 use Drupal\Core\Entity\EntityStorageBase;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
+use Drupal\Core\Language\LanguageInterface;
 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
 use Drupal\Core\StringTranslation\Translator\TranslationInterface;
-use Drupal\Tests\UnitTestCase;
-use Drupal\salesforce\Rest\RestClient;
-use Drupal\salesforce\SObject;
-use Drupal\salesforce\SFID;
-use Drupal\salesforce\SelectQueryResult;
 use Drupal\salesforce_mapping\Entity\MappedObject;
 use Drupal\salesforce_mapping\Entity\MappedObjectInterface;
-use Drupal\salesforce_mapping\Entity\SalesforceMapping;
-//use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
+use Drupal\salesforce_mapping\Entity\SalesforceMappingInterface;
 use Drupal\salesforce_mapping\MappingConstants;
+use Drupal\salesforce_mapping\SalesforcePullEvent;
 use Drupal\salesforce_pull\Plugin\QueueWorker\PullBase;
 use Drupal\salesforce_pull\PullQueueItem;
+use Drupal\salesforce\Rest\RestClient;
+use Drupal\salesforce\SelectQueryResult;
+use Drupal\salesforce\SFID;
+use Drupal\salesforce\SObject;
+use Drupal\Tests\UnitTestCase;
 use Prophecy\Argument;
 use Psr\Log\LoggerInterface;
-use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
-//use Drupal\Core\TypedData\DataDefinitionInterface;
-use Drupal\Core\Language\LanguageInterface;
 
 /**
  * Test Object instantitation
@@ -58,7 +57,7 @@ class PullBaseTest extends UnitTestCase {
       ->disableOriginalConstructor()
       //->setConstructorArgs([$ddi,null,null])
       ->getMock();
-    $changed_value->expects($this->once())
+    $changed_value->expects($this->any())
       ->method('__get')
       ->with($this->equalTo('value'))
       ->willReturn('999999');
@@ -68,28 +67,20 @@ class PullBaseTest extends UnitTestCase {
       ->setMethods(['__construct', '__get', '__set', 'label', 'id', '__isset'])
       ->disableOriginalConstructor()
       ->getMock();
-    //$this->entity->method('label')
-    //  ->willReturn('test');
-    //$this->entity->method('id')
-    //  ->willReturn(1);
     $this->entity->expects($this->any())
       ->method('__get')
       ->with($this->equalTo('changed'))
       ->willReturn($changed_value);
-    $this->entity->expects($this->once())
+    $this->entity->expects($this->any())
       ->method('__set')
       ->with($this->equalTo('salesforce_pull'));
-    // to handle !empty($entity->changed->value) call
     $this->entity->expects($this->any())
       ->method('__isset')
       ->with($this->equalTo('changed'))
       ->willReturn(true);
 
     // mock mapping object
-    $this->mapping = $this->getMockBuilder(SalesforceMapping::CLASS)
-      ->setMethods(['__construct', '__get', 'get', 'checkTriggers', 'getDrupalEntityType', 'getDrupalBundle', 'getFieldMappings', 'getSalesforceObjectType'])
-      ->disableOriginalConstructor()
-      ->getMock();
+    $this->mapping = $this->getMock(SalesforceMappingInterface::CLASS);
     $this->mapping->expects($this->any())
       ->method('__get')
       ->with($this->equalTo('id'))
@@ -97,11 +88,8 @@ class PullBaseTest extends UnitTestCase {
     $this->mapping->expects($this->any())
       ->method('checkTriggers')
       ->willReturn(true);
-    $this->mapping->expects($this->any())
-      ->method('get')
-      ->will($this->returnValueMap([
-        ['pull_trigger_date', 'pull_trigger_date']
-      ]));
+    $this->mapping->method('getPullTriggerDate')
+      ->willReturn('pull_trigger_date');
     $this->mapping->method('getDrupalEntityType')
       ->willReturn('test');
     $this->mapping->method('getDrupalBundle')
@@ -116,9 +104,8 @@ class PullBaseTest extends UnitTestCase {
     $entity_type_id_value = $this->getMockBuilder(StringItem::CLASS)
       ->setMethods(['__get'])
       ->disableOriginalConstructor()
-      //->setConstructorArgs([$ddi,null,null])
       ->getMock();
-    $entity_type_id_value->expects($this->once())
+    $entity_type_id_value->expects($this->any())
       ->method('__get')
       ->with($this->equalTo('value'))
       ->willReturn('test');
@@ -127,9 +114,8 @@ class PullBaseTest extends UnitTestCase {
     $entity_id_value = $this->getMockBuilder(StringItem::CLASS)
       ->setMethods(['__get'])
       ->disableOriginalConstructor()
-      //->setConstructorArgs([$ddi,null,null])
       ->getMock();
-    $entity_id_value->expects($this->once())
+    $entity_id_value->expects($this->any())
       ->method('__get')
       ->with($this->equalTo('value'))
       ->willReturn('1');
@@ -244,9 +230,7 @@ class PullBaseTest extends UnitTestCase {
     $this->lf = $prophecy->reveal();
 
     // mock event dispatcher
-    $prophecy = $this->prophesize(ContainerAwareEventDispatcher::CLASS);
-    $prophecy->dispatch(Argument::any(),Argument::any())->willReturn(NULL);
-    $this->ed = $prophecy->reveal();
+    $this->ed = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface');
 
     $this->pullWorker = $this->getMockBuilder(PullBase::CLASS)
       ->setConstructorArgs([
@@ -256,7 +240,10 @@ class PullBaseTest extends UnitTestCase {
         $this->lf,
         $this->ed
       ])
+      ->setMethods(['salesforcePullEvent'])
       ->getMockForAbstractClass();
+    $this->pullWorker->method('salesforcePullEvent')
+      ->willReturn(null);
   }
 
   /**
diff --git a/modules/salesforce_pull/tests/src/Unit/PullQueueItemTest.php b/modules/salesforce_pull/tests/src/Unit/PullQueueItemTest.php
index 1fbee3a9..f8c0d554 100644
--- a/modules/salesforce_pull/tests/src/Unit/PullQueueItemTest.php
+++ b/modules/salesforce_pull/tests/src/Unit/PullQueueItemTest.php
@@ -28,9 +28,7 @@ class PullQueueItemTest extends UnitTestCase {
   public function testObject() {
     $sobject = new SObject(['id' => '1234567890abcde', 'attributes' => ['type' => 'dummy',]]);
     // OF COURSE Prophesy doesn't do magic methods well.
-    $mapping = $this->getMockBuilder(SalesforceMappingInterface::CLASS)
-      ->setMethods(['__construct', '__get'])
-      ->getMock();
+    $mapping = $this->getMock(SalesforceMappingInterface::CLASS);
     $mapping->expects($this->any())
       ->method('__get')
       ->with($this->equalTo('id'))
diff --git a/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php b/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php
index ddb9376c..14e2ef3c 100644
--- a/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php
+++ b/modules/salesforce_pull/tests/src/Unit/QueueHandlerTest.php
@@ -47,9 +47,7 @@ class QueueHandlerTest extends UnitTestCase {
       ->willReturn($this->sqr);
     $this->sfapi = $prophecy->reveal();
 
-    $this->mapping = $this->getMockBuilder(SalesforceMappingInterface::CLASS)
-      ->setMethods(['__construct', '__get', 'get', 'getSalesforceObjectType', 'getPullFieldsArray'])
-      ->getMock();
+    $this->mapping = $this->getMock(SalesforceMappingInterface::CLASS);
     $this->mapping->expects($this->any())
       ->method('__get')
       ->with($this->equalTo('id'))
-- 
GitLab