You are here

protected function EntityShareCronServiceTest::setUp in Entity Share Cron 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/EntityShareCronServiceTest.php \Drupal\Tests\entity_share_cron\Unit\EntityShareCronServiceTest::setUp()

Overrides UnitTestCase::setUp

File

tests/src/Unit/EntityShareCronServiceTest.php, line 264

Class

EntityShareCronServiceTest
@coversDefaultClass \Drupal\entity_share_cron\EntityShareCronService @group entity_share_cron

Namespace

Drupal\Tests\entity_share_cron\Unit

Code

protected function setUp() {
  $this->responsePages = count($this->responseData);
  $this->requestCount = 0;

  // Mocks a configuration factory for the module settings.
  $settings = $this
    ->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
    ->disableOriginalConstructor()
    ->getMock();
  $settings
    ->expects($this
    ->any())
    ->method('get')
    ->with('remotes')
    ->will($this
    ->returnCallback([
    $this,
    'getRemotesConfig',
  ]));
  $config_factory = $this
    ->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
  $config_factory
    ->expects($this
    ->any())
    ->method('get')
    ->with('entity_share_cron.settings')
    ->will($this
    ->returnValue($settings));

  // Mocks storages to load remotes and nodes.
  $remote = new Remote([
    'id' => self::REMOTE_ID,
  ], 'remote');
  $remote_storage = $this
    ->getMock('Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
  $remote_storage
    ->expects($this
    ->any())
    ->method('load')
    ->with($this
    ->equalTo(self::REMOTE_ID))
    ->will($this
    ->returnValue($remote));
  $node_storage = $this
    ->getMock('Drupal\\Core\\Entity\\ContentEntityStorageInterface');
  $node_storage
    ->expects($this
    ->any())
    ->method('loadByProperties')
    ->will($this
    ->returnCallback([
    $this,
    'loadNodeByProperties',
  ]));

  // Mocks node entity type definition.
  $node_definition = $this
    ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
  $node_definition
    ->expects($this
    ->any())
    ->method('getKey')
    ->with($this
    ->equalTo('uuid'))
    ->will($this
    ->returnValue('uuid'));

  // Mocks entity type manager and repository and add them to the container.
  $entity_type_manager = $this
    ->getMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
  $entity_type_manager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->will($this
    ->returnValueMap([
    [
      'node',
      $node_storage,
    ],
    [
      'remote',
      $remote_storage,
    ],
  ]));
  $entity_type_manager
    ->expects($this
    ->any())
    ->method('getDefinition')
    ->with($this
    ->equalTo('node'))
    ->will($this
    ->returnValue($node_definition));
  $entity_type_repository = $this
    ->getMock('Drupal\\Core\\Entity\\EntityTypeRepositoryInterface');
  $entity_type_repository
    ->expects($this
    ->any())
    ->method('getEntityTypeFromClass')
    ->will($this
    ->returnValue('remote'));
  $container = new ContainerBuilder();
  $container
    ->set('entity_type.manager', $entity_type_manager);
  $container
    ->set('entity_type.repository', $entity_type_repository);
  \Drupal::setContainer($container);

  // Mocks a remote manager.
  $response = $this
    ->getMock('Psr\\Http\\Message\\ResponseInterface');
  $response
    ->expects($this
    ->any())
    ->method('getBody')
    ->will($this
    ->returnCallback([
    $this,
    'getResponseBody',
  ]));
  $http_client = $this
    ->getMock('GuzzleHttp\\Client');
  $http_client
    ->expects($this
    ->atLeastOnce())
    ->method('__call')
    ->with($this
    ->equalTo('get'))
    ->will($this
    ->returnValue($response));
  $remote_manager = $this
    ->getMock('Drupal\\entity_share_client\\Service\\RemoteManagerInterface');
  $remote_manager
    ->expects($this
    ->any())
    ->method('prepareJsonApiClient')
    ->will($this
    ->returnValue($http_client));

  // Mocks a JSON API Helper.
  $jsonapi_helper = $this
    ->getMock('Drupal\\entity_share_client\\Service\\JsonapiHelperInterface');
  $jsonapi_helper
    ->expects($this
    ->any())
    ->method('prepareData')
    ->will($this
    ->returnArgument(0));
  $jsonapi_helper
    ->expects($this
    ->once())
    ->method('importEntityListData')
    ->will($this
    ->returnArgument(0));

  // Mocks a queue factory.
  $this->queue = $this
    ->getMock('Drupal\\Core\\Queue\\QueueInterface');
  $queue_factory = $this
    ->getMockBuilder('Drupal\\Core\\Queue\\QueueFactory')
    ->disableOriginalConstructor()
    ->setMethods([
    'get',
  ])
    ->getMock();
  $queue_factory
    ->expects($this
    ->any())
    ->method('get')
    ->with($this
    ->equalTo(EntityShareCronServiceInterface::PENDING_QUEUE_NAME))
    ->will($this
    ->returnValue($this->queue));

  // Instantiates a service to test.
  $this->service = new EntityShareCronService($config_factory, $remote_manager, $jsonapi_helper, $queue_factory, $entity_type_manager);
}