You are here

class PushQueueTest in Salesforce Suite 5.0.x

Same name in this branch
  1. 5.0.x modules/salesforce_push/tests/src/Functional/PushQueueTest.php \Drupal\Tests\salesforce_push\Functional\PushQueueTest
  2. 5.0.x modules/salesforce_push/tests/src/Unit/PushQueueTest.php \Drupal\Tests\salesforce_push\Unit\PushQueueTest
Same name and namespace in other branches
  1. 8.4 modules/salesforce_push/tests/src/Unit/PushQueueTest.php \Drupal\Tests\salesforce_push\Unit\PushQueueTest
  2. 8.3 modules/salesforce_push/tests/src/Unit/PushQueueTest.php \Drupal\Tests\salesforce_push\Unit\PushQueueTest

Test Object instantitation.

@coversDefaultClass \Drupal\salesforce_push\PushQueue

@group salesforce_push

Hierarchy

Expanded class hierarchy of PushQueueTest

File

modules/salesforce_push/tests/src/Unit/PushQueueTest.php, line 33

Namespace

Drupal\Tests\salesforce_push\Unit
View source
class PushQueueTest extends UnitTestCase {
  protected static $modules = [
    'salesforce_push',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    $this->schema = $this
      ->getMockBuilder(Schema::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->schema
      ->expects($this
      ->any())
      ->method('tableExists')
      ->willReturn(TRUE);
    $this->database = $this
      ->getMockBuilder(Connection::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->database
      ->expects($this
      ->any())
      ->method('schema')
      ->willReturn($this->schema);
    $this->state = $this
      ->getMockBuilder(StateInterface::class)
      ->getMock();
    $this->push_queue_processor_plugin_manager = $this
      ->getMockBuilder(PushQueueProcessorPluginManager::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->entityTypeManager = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->getMock();
    $this->eventDispatcher = $this
      ->getMockBuilder(EventDispatcherInterface::CLASS)
      ->getMock();
    $this->eventDispatcher
      ->expects($this
      ->any())
      ->method('dispatch')
      ->willReturn(NULL);
    $this->string_translation = $this
      ->getMockBuilder(TranslationInterface::class)
      ->getMock();
    $this->time = $this
      ->getMockBuilder(TimeInterface::class)
      ->getMock();
    $this->mappingStorage = $this
      ->getMockBuilder(SalesforceMappingStorage::CLASS)
      ->disableOriginalConstructor()
      ->getMock();
    $this->mappedObjectStorage = $this
      ->getMockBuilder(SqlEntityStorageInterface::CLASS)
      ->getMock();
    $this->entityStorage = $this
      ->getMockBuilder(SqlEntityStorageInterface::CLASS)
      ->getMock();
    $this->entityTypeManager
      ->expects($this
      ->at(0))
      ->method('getStorage')
      ->with($this
      ->equalTo('salesforce_mapping'))
      ->willReturn($this->mappingStorage);
    $this->entityTypeManager
      ->expects($this
      ->at(1))
      ->method('getStorage')
      ->with($this
      ->equalTo('salesforce_mapped_object'))
      ->willReturn($this->mappedObjectStorage);

    // Mock config.
    $prophecy = $this
      ->prophesize(Config::CLASS);
    $prophecy
      ->get('global_push_limit', Argument::any())
      ->willReturn(PushQueue::DEFAULT_GLOBAL_LIMIT);
    $config = $prophecy
      ->reveal();
    $prophecy = $this
      ->prophesize(ConfigFactoryInterface::CLASS);
    $prophecy
      ->get('salesforce.settings')
      ->willReturn($config);
    $this->configFactory = $prophecy
      ->reveal();
    $container = new ContainerBuilder();
    $container
      ->set('database', $this->database);
    $container
      ->set('state', $this->state);
    $container
      ->set('entity_type.manager', $this->entityTypeManager);
    $container
      ->set('event_dispatcher', $this->eventDispatcher);
    $container
      ->set('string_translation', $this->string_translation);
    $container
      ->set('plugin.manager.salesforce_push_queue_processor', $this->push_queue_processor_plugin_manager);
    $container
      ->set('datetime.time', $this->time);
    $container
      ->set('config.factory', $this->configFactory);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::claimItem
   */
  public function testClaimItem() {
    $this->queue = PushQueue::create(\Drupal::getContainer());
    $this
      ->expectException(\Exception::class);
    $this->queue
      ->claimItem();
  }

  /**
   * @covers ::claimItems
   */
  public function testClaimItems() {
    $this->queue = PushQueue::create(\Drupal::getContainer());

    // Test claiming items.
    $items = [
      1,
      2,
      3,
    ];
    $this->queryRange = $this
      ->getMockBuilder(StatementInterface::class)
      ->getMock();
    $this->queryRange
      ->expects($this
      ->once())
      ->method('fetchAllAssoc')
      ->willReturn($items);
    $this->database
      ->expects($this
      ->once())
      ->method('queryRange')
      ->willReturn($this->queryRange);
    $this->updateQuery = $this
      ->getMockBuilder(Update::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->updateQuery
      ->expects($this
      ->once())
      ->method('fields')
      ->willReturn($this->updateQuery);
    $this->updateQuery
      ->expects($this
      ->any())
      ->method('condition')
      ->willReturn($this->updateQuery);
    $this->updateQuery
      ->expects($this
      ->once())
      ->method('execute')
      ->willReturn(TRUE);
    $this->database
      ->expects($this
      ->once())
      ->method('update')
      ->willReturn($this->updateQuery);
    $this
      ->assertEquals($items, $this->queue
      ->claimItems(0));
  }

  /**
   * @covers ::processQueues
   */
  public function testProcessQueue() {
    $items = [
      1,
      2,
      3,
    ];
    $mapping1 = $this
      ->getMockBuilder(SalesforceMappingInterface::CLASS)
      ->getMock();
    $mapping1
      ->expects($this
      ->any())
      ->method('getNextPushTime')
      ->willReturn(0);
    $mapping1
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn(1);
    $mapping1->push_limit = 1;
    $mapping1->push_retries = 1;
    $this->worker = $this
      ->getMockBuilder(PushQueueProcessorInterface::class)
      ->getMock();
    $this->worker
      ->expects($this
      ->once())
      ->method('process')
      ->willReturn(NULL);
    $this->push_queue_processor_plugin_manager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->willReturn($this->worker);
    $this->queue = $this
      ->getMockBuilder(PushQueue::class)
      ->setMethods([
      'claimItems',
      'setName',
      'garbageCollection',
    ])
      ->setConstructorArgs([
      $this->database,
      $this->state,
      $this->push_queue_processor_plugin_manager,
      $this->entityTypeManager,
      $this->eventDispatcher,
      $this->time,
      $this->configFactory,
    ])
      ->getMock();
    $this->queue
      ->expects($this
      ->once())
      ->method('setName')
      ->willReturn(NULL);
    $this->queue
      ->expects($this
      ->any())
      ->method('garbageCollection')
      ->willReturn(NULL);

    // I don't know why at(2) works.
    $this->queue
      ->expects($this
      ->at(2))
      ->method('claimItems')
      ->willReturn($items);
    $this
      ->assertEquals(3, $this->queue
      ->processQueue($mapping1));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
PushQueueTest::$modules protected static property
PushQueueTest::setUp protected function Overrides UnitTestCase::setUp
PushQueueTest::testClaimItem public function @covers ::claimItem
PushQueueTest::testClaimItems public function @covers ::claimItems
PushQueueTest::testProcessQueue public function @covers ::processQueues
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
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.
UnitTestCase::setUpBeforeClass public static function