You are here

protected function EntityConverterTest::setUpMocks in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityConverterTest::setUpMocks()
  2. 10 core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php \Drupal\Tests\Core\ParamConverter\EntityConverterTest::setUpMocks()

Sets up mock services and class instances.

Parameters

object[] $service_map: An associative array of service instances keyed by service name.

2 calls to EntityConverterTest::setUpMocks()
EntityConverterTest::testConvert in core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php
Tests the convert() method.
EntityConverterTest::testConvertWithInvalidEntityType in core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php
Tests the convert() method with an invalid entity type.

File

core/tests/Drupal/Tests/Core/ParamConverter/EntityConverterTest.php, line 69

Class

EntityConverterTest
@coversDefaultClass \Drupal\Core\ParamConverter\EntityConverter @group ParamConverter @group Entity

Namespace

Drupal\Tests\Core\ParamConverter

Code

protected function setUpMocks($service_map = []) {
  $entity = $this
    ->createMock(ContentEntityInterface::class);
  $entity
    ->expects($this
    ->any())
    ->method('getEntityTypeId')
    ->willReturn('entity_test');
  $entity
    ->expects($this
    ->any())
    ->method('id')
    ->willReturn('id');
  $entity
    ->expects($this
    ->any())
    ->method('isTranslatable')
    ->willReturn(FALSE);
  $entity
    ->expects($this
    ->any())
    ->method('getLoadedRevisionId')
    ->willReturn('revision_id');
  $storage = $this
    ->createMock(ContentEntityStorageInterface::class);
  $storage
    ->expects($this
    ->any())
    ->method('load')
    ->with('id')
    ->willReturn($entity);
  $storage
    ->expects($this
    ->any())
    ->method('getLatestRevisionId')
    ->with('id')
    ->willReturn('revision_id');
  $this->entityTypeManager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->with('entity_test')
    ->willReturn($storage);
  $entity_type = $this
    ->createMock(ContentEntityTypeInterface::class);
  $entity_type
    ->expects($this
    ->any())
    ->method('isRevisionable')
    ->willReturn(TRUE);
  $this->entityTypeManager
    ->expects($this
    ->any())
    ->method('getDefinition')
    ->with('entity_test')
    ->willReturn($entity_type);
  $context_repository = $this
    ->createMock(ContextRepositoryInterface::class);
  $context_repository
    ->expects($this
    ->any())
    ->method('getAvailableContexts')
    ->willReturn([]);
  $context_definition = $this
    ->createMock(DataDefinition::class);
  foreach ([
    'setLabel',
    'setDescription',
    'setRequired',
    'setConstraints',
  ] as $method) {
    $context_definition
      ->expects($this
      ->any())
      ->method($method)
      ->willReturn($context_definition);
  }
  $context_definition
    ->expects($this
    ->any())
    ->method('getConstraints')
    ->willReturn([]);
  $typed_data_manager = $this
    ->createMock(TypedDataManagerInterface::class);
  $typed_data_manager
    ->expects($this
    ->any())
    ->method('create')
    ->willReturn($this
    ->createMock(TypedDataInterface::class));
  $typed_data_manager
    ->expects($this
    ->any())
    ->method('createDataDefinition')
    ->willReturn($context_definition);
  $service_map += [
    'context.repository' => $context_repository,
    'typed_data_manager' => $typed_data_manager,
  ];

  /** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit\Framework\MockObject\MockObject $container */
  $container = $this
    ->createMock(ContainerInterface::class);
  $return_map = [];
  foreach ($service_map as $name => $service) {
    $return_map[] = [
      $name,
      1,
      $service,
    ];
  }
  $container
    ->expects($this
    ->any())
    ->method('get')
    ->willReturnMap($return_map);
  \Drupal::setContainer($container);
}