You are here

class DeleteHandlerTest in Salesforce Suite 8.4

Same name and namespace in other branches
  1. 8.3 modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php \Drupal\Tests\salesforce_pull\Unit\DeleteHandlerTest
  2. 5.0.x modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php \Drupal\Tests\salesforce_pull\Unit\DeleteHandlerTest

Test Object instantitation.

@group salesforce_pull

Hierarchy

Expanded class hierarchy of DeleteHandlerTest

File

modules/salesforce_pull/tests/src/Unit/DeleteHandlerTest.php, line 25

Namespace

Drupal\Tests\salesforce_pull\Unit
View source
class DeleteHandlerTest extends UnitTestCase {

  /**
   * Required modules.
   *
   * @var array
   */
  protected static $modules = [
    'salesforce_pull',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $result = [
      'totalSize' => 1,
      'done' => TRUE,
      'deletedRecords' => [
        [
          'id' => '1234567890abcde',
          'attributes' => [
            'type' => 'dummy',
          ],
          'name' => 'Example',
        ],
      ],
    ];
    $prophecy = $this
      ->prophesize(RestClientInterface::CLASS);
    $prophecy
      ->getDeleted(Argument::any(), Argument::any(), Argument::any())
      ->willReturn($result);
    $this->sfapi = $prophecy
      ->reveal();

    // Mock an atribtary Drupal entity.
    $this->entity = $this
      ->getMockBuilder(User::CLASS)
      ->disableOriginalConstructor()
      ->getMock();
    $this->entity
      ->expects($this
      ->any())
      ->method('delete')
      ->willReturn(TRUE);
    $this->entity
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn(1);
    $this->entity
      ->expects($this
      ->any())
      ->method('label')
      ->willReturn('foo');
    $this->mapping = $this
      ->getMockBuilder(SalesforceMappingInterface::CLASS)
      ->getMock();
    $this->mapping
      ->expects($this
      ->any())
      ->method('__get')
      ->with($this
      ->equalTo('id'))
      ->willReturn(1);
    $this->mapping
      ->expects($this
      ->any())
      ->method('__get')
      ->with($this
      ->equalTo('entity'))
      ->willReturn($this->entity);
    $this->mapping
      ->expects($this
      ->any())
      ->method('getSalesforceObjectType')
      ->willReturn('default');
    $this->mapping
      ->expects($this
      ->any())
      ->method('getPullFieldsArray')
      ->willReturn([
      'Name' => 'Name',
      'Account Number' => 'Account Number',
    ]);
    $this->mapping
      ->expects($this
      ->any())
      ->method('checkTriggers')
      ->with([
      MappingConstants::SALESFORCE_MAPPING_SYNC_SF_DELETE,
    ])
      ->willReturn(TRUE);

    // Mock mapped object.
    $this->entityTypeId = new \stdClass();
    $this->entityId = new \stdClass();
    $this->entityRef = new \stdClass();
    $this->entityTypeId->value = 'test';
    $this->entityId->value = '1';
    $this->entityRef->entity = $this->mapping;
    $this->mappedObject = $this
      ->getMockBuilder(MappedObjectInterface::CLASS)
      ->getMock();
    $this->mappedObject
      ->expects($this
      ->any())
      ->method('delete')
      ->willReturn(TRUE);
    $this->mappedObject
      ->expects($this
      ->any())
      ->method('getMapping')
      ->willReturn($this->mapping);
    $this->mappedObject
      ->expects($this
      ->any())
      ->method('getFieldDefinitions')
      ->willReturn([
      'drupal_entity',
      'salesforce_mapping',
    ]);
    $this->mappedObject
      ->expects($this
      ->any())
      ->method('getMappedEntity')
      ->willReturn($this->entity);

    // Mock mapping ConfigEntityStorage object.
    $prophecy = $this
      ->prophesize(SalesforceMappingStorage::CLASS);
    $prophecy
      ->loadByProperties(Argument::any())
      ->willReturn([
      $this->mapping,
    ]);
    $prophecy
      ->load(Argument::any())
      ->willReturn($this->mapping);
    $prophecy
      ->loadMultiple()
      ->willReturn([
      $this->mapping,
    ]);
    $this->configStorage = $prophecy
      ->reveal();

    // Mock mapped object EntityStorage object.
    $this->entityStorage = $this
      ->getMockBuilder(MappedObjectStorage::CLASS)
      ->disableOriginalConstructor()
      ->getMock();
    $this->entityStorage
      ->expects($this
      ->any())
      ->method('loadBySfid')
      ->willReturn([
      $this->mappedObject,
    ]);

    // Mock Drupal entity EntityStorage object.
    $prophecy = $this
      ->prophesize(EntityStorageBase::CLASS);
    $prophecy
      ->load(Argument::any())
      ->willReturn($this->entity);
    $this->drupalEntityStorage = $prophecy
      ->reveal();

    // Mock EntityTypeManagerInterface.
    $prophecy = $this
      ->prophesize(EntityTypeManagerInterface::CLASS);
    $prophecy
      ->getStorage('salesforce_mapping')
      ->willReturn($this->configStorage);
    $prophecy
      ->getStorage('salesforce_mapped_object')
      ->willReturn($this->entityStorage);
    $prophecy
      ->getStorage('test')
      ->willReturn($this->drupalEntityStorage);
    $this->etm = $prophecy
      ->reveal();

    // Mock state.
    $prophecy = $this
      ->prophesize(StateInterface::CLASS);
    $prophecy
      ->get('salesforce.mapping_pull_info', Argument::any())
      ->willReturn([
      1 => [
        'last_delete_timestamp' => '1485787434',
      ],
    ]);
    $prophecy
      ->set('salesforce.mapping_pull_info', Argument::any())
      ->willReturn(NULL);
    $this->state = $prophecy
      ->reveal();

    // Mock event dispatcher.
    $prophecy = $this
      ->prophesize(ContainerAwareEventDispatcher::CLASS);
    $prophecy
      ->dispatch(Argument::any(), Argument::any())
      ->willReturn();
    $this->ed = $prophecy
      ->reveal();
    $this->dh = new DeleteHandler($this->sfapi, $this->etm, $this->state, $this->ed);
  }

  /**
   * Test object creation.
   */
  public function testObject() {
    $this
      ->assertTrue($this->dh instanceof DeleteHandler);
  }

  /**
   * Test processDeletedRecords.
   */
  public function testGetUpdatedRecords() {
    $result = $this->dh
      ->processDeletedRecords();
    $this
      ->assertTrue($result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeleteHandlerTest::$modules protected static property Required modules.
DeleteHandlerTest::setUp protected function Overrides UnitTestCase::setUp
DeleteHandlerTest::testGetUpdatedRecords public function Test processDeletedRecords.
DeleteHandlerTest::testObject public function Test object creation.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.