You are here

public function RoutesTest::testRoutes in JSON:API Search API 8

@covers ::routes @dataProvider routeDataProvider

Parameters

\Drupal\search_api\IndexInterface $mocked_index: The mocked index.

bool $expect_disabled_index: Boolean to check if the index is expected to be disabled.

string[] $expected_entity_types: The expected entity types from the index darasources.

\Drupal\jsonapi\ResourceType\ResourceType[] $mocked_resource_types: The mocked resource types.

File

tests/src/Unit/RoutesTest.php, line 39

Class

RoutesTest
Tests route generation.

Namespace

Drupal\Tests\jsonapi_search_api\Unit

Code

public function testRoutes(IndexInterface $mocked_index, bool $expect_disabled_index, array $expected_entity_types, array $mocked_resource_types) {
  $entity_type_manager = $this
    ->prophesize(EntityTypeManagerInterface::class);
  foreach ($expected_entity_types as $expected_entity_type) {
    $entity_type_manager
      ->hasDefinition($expected_entity_type)
      ->willReturn(TRUE);
  }
  $index_storage = $this
    ->prophesize(SearchApiConfigEntityStorage::class);
  $index_storage
    ->loadMultiple()
    ->willReturn([
    $mocked_index,
  ]);
  $entity_type_manager
    ->getStorage('search_api_index')
    ->willReturn($index_storage
    ->reveal());
  $resource_type_repository = $this
    ->prophesize(ResourceTypeRepositoryInterface::class);
  $index_resource_type = $this
    ->prophesize(ResourceType::class);
  $index_resource_type
    ->getTypeName()
    ->willReturn('search_api_index--search_api_index');
  $resource_type_repository
    ->get('search_api_index', 'search_api_index')
    ->willReturn($index_resource_type
    ->reveal());
  foreach ($mocked_resource_types as $mocked_resource_type) {
    $resource_type_repository
      ->get($mocked_resource_type
      ->getEntityTypeId(), $mocked_resource_type
      ->getBundle())
      ->willReturn($mocked_resource_type);
  }
  $routes = new Routes($entity_type_manager
    ->reveal(), $resource_type_repository
    ->reveal(), [], '/jsonapi');
  $route_collection = $routes
    ->routes();
  $sut = $route_collection
    ->get('jsonapi_search_api.index_' . $mocked_index
    ->id());
  if ($expect_disabled_index) {
    $this
      ->assertFalse($mocked_index
      ->status());
    $this
      ->assertNull($sut);
  }
  else {
    $this
      ->assertNotNull($sut);
    $this
      ->assertEquals('/%jsonapi%/index/' . $mocked_index
      ->id(), $sut
      ->getPath());
    $this
      ->assertEquals([
      'index' => [
        'type' => 'entity:search_api_index',
      ],
    ], $sut
      ->getOption('parameters'));
    $this
      ->assertEquals(IndexResource::class, $sut
      ->getDefault('_jsonapi_resource'));
    $this
      ->assertEquals(array_map(static function (ResourceType $resource_type) {
      return $resource_type
        ->getTypeName();
    }, $mocked_resource_types), $sut
      ->getDefault('_jsonapi_resource_types'));
    $this
      ->assertEquals($mocked_index
      ->uuid(), $sut
      ->getDefault('index'));
    $this
      ->assertEquals('search_api_index--search_api_index', $sut
      ->getDefault(JsonapiRoutes::RESOURCE_TYPE_KEY));
  }
}