You are here

protected function ContentHubReindexTest::setUp in Acquia Content Hub 8

Overrides UnitTestCase::setUp

File

tests/src/Unit/Controller/ContentHubReindexTest.php, line 97

Class

ContentHubReindexTest
@coversDefaultClass \Drupal\acquia_contenthub\Controller\ContentHubReindex

Namespace

Drupal\Tests\acquia_contenthub\Unit\Controller

Code

protected function setUp() : void {
  parent::setUp();
  $this->state = $this
    ->createMock('Drupal\\Core\\State\\StateInterface');
  $this->state
    ->method('get')
    ->willReturnCallback(function ($name, $value) {
    if ($name == ContentHubReindex::REINDEXING_STATE) {
      if (!empty($this->reindexState)) {
        return $this->reindexState;
      }
      return $value;
    }
    return NULL;
  });
  $this->state
    ->method('set')
    ->willReturnCallback(function ($name, $value) {
    if ($name === ContentHubReindex::REINDEXING_STATE) {
      $this->reindexState = $value;
    }
    else {
      $this->reindexState = ContentHubReindex::REINDEX_NONE;
    }
    return $this->reindexState;
  });
  $this->translationInterface = $this
    ->createMock('Drupal\\Core\\StringTranslation\\TranslationInterface');
  $this->entityManager = $this
    ->getMockBuilder('Drupal\\acquia_contenthub\\EntityManager')
    ->disableOriginalConstructor()
    ->getMock();

  // Setting up Entity Type Manager.
  $this->entityTypeManager = $this
    ->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
  $this->entityStorage = $this
    ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
  $this->entityTypeManager
    ->method('getStorage')
    ->willReturn($this->entityStorage);

  // Setting up the Container.
  $this->container = $this
    ->createMock('Drupal\\Core\\DependencyInjection\\Container');
  $this->container
    ->method('get')
    ->willReturnCallback(function ($name) {
    switch ($name) {
      case 'state':
        return $this->state;
      case 'acquia_contenthub.acquia_contenthub_reindex':
        return $this->contentHubReindex;
      case 'acquia_contenthub.entity_manager':
        return $this->entityManager;
      case 'entity_type.manager':
        return $this->entityTypeManager;
      case 'string_translation':
        return $this->translationInterface;
    }
    return NULL;
  });
  \Drupal::setContainer($this->container);
  $this->clientManager = $this
    ->createMock('\\Drupal\\acquia_contenthub\\Client\\ClientManagerInterface');
  $this->contentHubEntitiesTracking = $this
    ->getMockBuilder('Drupal\\acquia_contenthub\\ContentHubEntitiesTracking')
    ->disableOriginalConstructor()
    ->getMock();

  // Defining a certain number of entities to be reindexed.
  $this->numEntities = 5;
  $this->contentHubEntitiesTracking
    ->method('getCountEntitiesToReindex')
    ->willReturn($this->numEntities);

  // Creating the Test Object.
  $this->contentHubReindex = new ContentHubReindex($this->contentHubEntitiesTracking, $this->clientManager, $this->state);
}