You are here

class SalesforceMappingStorageTest in Salesforce Suite 8.4

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

@coversDefaultClass \Drupal\salesforce_mapping\SalesforceMappingStorage @group salesforce_mapping

Hierarchy

Expanded class hierarchy of SalesforceMappingStorageTest

File

modules/salesforce_mapping/tests/src/Unit/SalesforceMappingStorageTest.php, line 19

Namespace

Drupal\Tests\salesforce_mapping\Unit
View source
class SalesforceMappingStorageTest extends UnitTestCase {

  /**
   * The type ID of the entity under test.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * The UUID service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $uuidService;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $languageManager;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $configFactory;

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $etm;

  /**
   * Entity type under test.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $entityType;

  /**
   * Mapping storage service mock.
   *
   * @var \PHPUnit_Framework_MockObject_MockObject
   */
  protected $salesforceMappingStorage;

  /**
   * {@inheritdoc}
   *
   * @covers ::__construct
   */
  protected function setUp() {
    parent::setUp();
    $this->entityTypeId = 'test_entity_type';
    $this->entityType = new ConfigEntityType([
      'id' => $this->entityTypeId,
      'class' => SalesforceMapping::class,
      'provider' => 'the_provider',
      'config_prefix' => 'the_config_prefix',
      'entity_keys' => [
        'id' => 'id',
        'uuid' => 'uuid',
        'langcode' => 'langcode',
      ],
      'list_cache_tags' => [
        $this->entityTypeId . '_list',
      ],
    ]);
    $this->uuidService = $this
      ->createMock(UuidInterface::class);
    $this->languageManager = $this
      ->createMock(LanguageManagerInterface::class);
    $this->configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $this->etm = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->etm
      ->expects($this
      ->any())
      ->method('getDefinition')
      ->with($this
      ->equalTo('test_entityType'))
      ->willReturn($this->entityType);
    $this->salesforceMappingStorage = $this
      ->getMockBuilder(SalesforceMappingStorage::class)
      ->setConstructorArgs([
      $this->entityTypeId,
      $this->configFactory,
      $this->uuidService,
      $this->languageManager,
      $this->etm,
    ])
      ->disableOriginalConstructor()
      ->setMethods([
      'loadByProperties',
    ])
      ->getMock();
  }

  /**
   * @covers ::loadByDrupal
   */
  public function testLoadByDrupal() {
    $config_object = $this
      ->prophesize(SalesforceMapping::class);
    $this->salesforceMappingStorage
      ->expects($this
      ->at(0))
      ->method('loadByProperties')
      ->with($this
      ->equalTo([
      'drupal_entity_type' => $this->entityTypeId,
    ]))
      ->willReturn([
      $config_object
        ->reveal(),
    ]);
    $this->salesforceMappingStorage
      ->expects($this
      ->at(1))
      ->method('loadByProperties')
      ->with($this
      ->equalTo([
      'drupal_entity_type' => 'dummy',
    ]))
      ->willReturn([]);

    // Good entity type id provided means config object should be returned.
    $entities = $this->salesforceMappingStorage
      ->loadByDrupal($this->entityTypeId);
    $this
      ->assertEquals([
      $config_object
        ->reveal(),
    ], $entities);

    // Bad entity type provided means config should not be returned.
    $entities = $this->salesforceMappingStorage
      ->loadByDrupal('dummy');
    $this
      ->assertEquals([], $entities);
  }

  /**
   * @covers ::loadPushMappings
   */
  public function testLoadPushMappings() {
    $foo_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $foo_config_object
      ->id()
      ->willReturn('foo');
    $foo_config_object
      ->doesPush()
      ->willReturn(TRUE);
    $bar_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $bar_config_object
      ->id()
      ->willReturn('bar');
    $bar_config_object
      ->doesPush()
      ->willReturn(TRUE);
    $zee_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $zee_config_object
      ->id()
      ->willReturn('zee');

    // Zee does NOT push; make sure it's excluded from our load helper.
    $zee_config_object
      ->doesPush()
      ->willReturn(FALSE);
    $this->salesforceMappingStorage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->willReturn([
      'foo' => $foo_config_object
        ->reveal(),
      'bar' => $bar_config_object
        ->reveal(),
      'zee' => $zee_config_object
        ->reveal(),
    ]);
    $entities = $this->salesforceMappingStorage
      ->loadPushMappings($this->entityTypeId);
    $expected = [
      'foo' => $foo_config_object
        ->reveal(),
      'bar' => $bar_config_object
        ->reveal(),
    ];
    $this
      ->assertContainsOnlyInstancesOf(EntityInterface::class, $entities);
    $this
      ->assertEquals($expected, $entities);
  }

  /**
   * @covers ::loadPullMappings
   */
  public function testLoadPullMappings() {
    $foo_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $foo_config_object
      ->id()
      ->willReturn('foo');
    $foo_config_object
      ->doesPull()
      ->willReturn(TRUE);
    $bar_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $bar_config_object
      ->id()
      ->willReturn('bar');
    $bar_config_object
      ->doesPull()
      ->willReturn(TRUE);
    $zee_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $zee_config_object
      ->id()
      ->willReturn('zee');

    // Zee does NOT push; make sure it's excluded from our load helper.
    $zee_config_object
      ->doesPull()
      ->willReturn(FALSE);
    $this->salesforceMappingStorage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->willReturn([
      'foo' => $foo_config_object
        ->reveal(),
      'bar' => $bar_config_object
        ->reveal(),
      'zee' => $zee_config_object
        ->reveal(),
    ]);
    $entities = $this->salesforceMappingStorage
      ->loadPullMappings($this->entityTypeId);
    $expected = [
      'foo' => $foo_config_object
        ->reveal(),
      'bar' => $bar_config_object
        ->reveal(),
    ];
    $this
      ->assertContainsOnlyInstancesOf(EntityInterface::class, $entities);
    $this
      ->assertEquals($expected, $entities);
  }

  /**
   * @covers ::getMappedSobjectTypes
   */
  public function testGetMappedSobjectTypes() {
    $foo_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $foo_config_object
      ->id()
      ->willReturn('foo');
    $foo_config_object
      ->getSalesforceObjectType()
      ->willReturn('Account');
    $bar_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $bar_config_object
      ->id()
      ->willReturn('bar');
    $bar_config_object
      ->getSalesforceObjectType()
      ->willReturn('Account');
    $zee_config_object = $this
      ->prophesize(SalesforceMapping::class);
    $zee_config_object
      ->id()
      ->willReturn('zee');
    $zee_config_object
      ->getSalesforceObjectType()
      ->willReturn('Contact');
    $this->salesforceMappingStorage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->willReturn([
      $foo_config_object
        ->reveal(),
      $bar_config_object
        ->reveal(),
      $zee_config_object
        ->reveal(),
    ]);
    $object_types = $this->salesforceMappingStorage
      ->getMappedSobjectTypes();
    $expected = [
      'Account' => 'Account',
      'Contact' => 'Contact',
    ];
    $this
      ->assertEquals($expected, $object_types);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SalesforceMappingStorageTest::$configFactory protected property The config factory service.
SalesforceMappingStorageTest::$entityType protected property Entity type under test.
SalesforceMappingStorageTest::$entityTypeId protected property The type ID of the entity under test.
SalesforceMappingStorageTest::$etm protected property Entity type manager service.
SalesforceMappingStorageTest::$languageManager protected property The language manager.
SalesforceMappingStorageTest::$salesforceMappingStorage protected property Mapping storage service mock.
SalesforceMappingStorageTest::$uuidService protected property The UUID service.
SalesforceMappingStorageTest::setUp protected function @covers ::__construct Overrides UnitTestCase::setUp
SalesforceMappingStorageTest::testGetMappedSobjectTypes public function @covers ::getMappedSobjectTypes
SalesforceMappingStorageTest::testLoadByDrupal public function @covers ::loadByDrupal
SalesforceMappingStorageTest::testLoadPullMappings public function @covers ::loadPullMappings
SalesforceMappingStorageTest::testLoadPushMappings public function @covers ::loadPushMappings
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.