function RelatedResourceTypesTest::testGetRelatableResourceTypes

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php \Drupal\Tests\jsonapi\Kernel\ResourceType\RelatedResourceTypesTest::testGetRelatableResourceTypes()
  2. 10 core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php \Drupal\Tests\jsonapi\Kernel\ResourceType\RelatedResourceTypesTest::testGetRelatableResourceTypes()
  3. 11.x core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php \Drupal\Tests\jsonapi\Kernel\ResourceType\RelatedResourceTypesTest::testGetRelatableResourceTypes()

@covers ::getRelatableResourceTypes @dataProvider getRelatableResourceTypesProvider

File

core/modules/jsonapi/tests/src/Kernel/ResourceType/RelatedResourceTypesTest.php, line 112

Class

RelatedResourceTypesTest
@coversDefaultClass \Drupal\jsonapi\ResourceType\ResourceType @coversClass \Drupal\jsonapi\ResourceType\ResourceTypeRepository @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\ResourceType

Code

public function testGetRelatableResourceTypes($resource_type_name, $relatable_type_names) {
    // We're only testing the fields that we set up.
    $test_fields = [
        'field_ref_foo',
        'field_ref_bar',
        'field_ref_any',
    ];
    $resource_type = $this->resourceTypeRepository
        ->getByTypeName($resource_type_name);
    // This extracts just the relationship fields under test.
    $subjects = array_intersect_key($resource_type->getRelatableResourceTypes(), array_flip($test_fields));
    // Map the related resource type to their type name so we can just compare
    // the type names rather that the whole object.
    foreach ($test_fields as $field_name) {
        if (isset($subjects[$field_name])) {
            $subjects[$field_name] = array_map(function ($resource_type) {
                return $resource_type->getTypeName();
            }, $subjects[$field_name]);
        }
    }
    $this->assertEquals($relatable_type_names, $subjects);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.