ResourceType example

/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $resource_type_repository = $this->prophesize(ResourceTypeRepository::class);

    $resource_type_repository
      ->getByTypeName(Argument::any())
      ->willReturn(new ResourceType('node', 'article', NULL));

    $entity_storage = $this->prophesize(EntityStorageInterface::class);
    $self = $this;
    $uuid_to_id = [
      '76dd5c18-ea1b-4150-9e75-b21958a2b836' => 1,
      'fcce1b61-258e-4054-ae36-244d25a9e04c' => 2,
    ];
    $entity_storage->loadByProperties(Argument::type('array'))
      ->will(function D$args) use ($self$uuid_to_id) {
        $result = [];
        foreach ($args[0]['uuid'] as $uuid) {
          

    $subscriber->setValidator();
    $this->subscriber = $subscriber;
  }

  /** * @covers ::doValidateResponse */
  public function testDoValidateResponse() {
    $request = $this->createRequest(
      'jsonapi.node--article.individual',
      new ResourceType('node', 'article', NULL)
    );

    $response = $this->createResponse('{"data":null}');

    // Capture the default assert settings.     $zend_assertions_default = ini_get('zend.assertions');
    $assert_active_default = assert_options(ASSERT_ACTIVE);

    // The validator *should* be called when asserts are active.     $validator = $this->prophesize(Validator::class);
    $validator->check(Argument::any(), Argument::any())->shouldBeCalled('Validation should be run when asserts are active.');
    
$this->assertEqualsCanonicalizing(['node:2', 'node_list']$response->getCacheableMetadata()->getCacheTags());
  }

  /** * @covers ::getCollection */
  public function testGetEmptyCollection() {
    $request = Request::create('/jsonapi/node/article');
    $request->query = new InputBag(['filter' => ['id' => 'invalid']]);

    // Get the response.     $resource_type = new ResourceType('node', 'article', NULL);
    $response = $this->entityResource->getCollection($resource_type$request);

    // Assertions.     $this->assertInstanceOf(CacheableResourceResponse::class$response);
    $this->assertInstanceOf(JsonApiDocumentTopLevel::class$response->getResponseData());
    $this->assertInstanceOf(Data::class$response->getResponseData()->getData());
    $this->assertEquals(0, $response->getResponseData()->getData()->count());
    $this->assertEquals(['node_list']$response->getCacheableMetadata()->getCacheTags());
  }

}
Node::create(array_merge([
        'type' => 'painting',
      ]$painting))->save();
    }
  }

  /** * @covers ::createFromQueryParameter * @dataProvider parameterProvider */
  public function testCreateFromQueryParameter($case$expected) {
    $resource_type = new ResourceType('foo', 'bar', NULL);
    $actual = Filter::createFromQueryParameter($case$resource_type$this->getFieldResolverMock($resource_type));
    $conditions = $actual->root()->members();
    for ($i = 0; $i < count($case)$i++) {
      $this->assertEquals($expected[$i]['path']$conditions[$i]->field());
      $this->assertEquals($expected[$i]['value']$conditions[$i]->value());
      $this->assertEquals($expected[$i]['operator']$conditions[$i]->operator());
    }
  }

  /** * Data provider for testCreateFromQueryParameter. */

      return new static($resource_type, 'virtual', $get_metadata('virtual'));
    }
    else {
      // In case of a dangling reference, it is impossible to determine which       // resource type it used to reference, because that requires knowing the       // referenced bundle, which Drupal does not store.       // If we can reliably determine the resource type of the dangling       // reference, use it; otherwise conjure a fake resource type out of thin       // air, one that indicates we don't know the bundle.       $resource_type = count($relatable_resource_types) > 1
        ? new ResourceType('?', '?', '')
        : reset($relatable_resource_types);
      return new static($resource_type, 'missing', $get_metadata('missing'));
    }
  }

}

  protected $resourceType;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $target_resource_type = new ResourceType('lorem', 'dummy_bundle', NULL);
    $relationship_fields = [
      'field_dummy' => new ResourceTypeRelationship('field_dummy'),
      'field_dummy_single' => new ResourceTypeRelationship('field_dummy_single'),
    ];
    $this->resourceType = new ResourceType('fake_entity_type', 'dummy_bundle', NULL, FALSE, TRUE, TRUE, FALSE, $relationship_fields);
    $this->resourceType->setRelatableResourceTypes([
      'field_dummy' => [$target_resource_type],
      'field_dummy_single' => [$target_resource_type],
    ]);

    $field_manager = $this->prophesize(EntityFieldManagerInterface::class);
    
$type_name = NULL;
    $raw_fields = $this->getAllFieldNames($entity_type$bundle);
    $internalize_resource_type = $entity_type->isInternal();
    $fields = static::getFields($raw_fields$entity_type$bundle);
    if (!$internalize_resource_type) {
      $event = ResourceTypeBuildEvent::createFromEntityTypeAndBundle($entity_type$bundle$fields);
      $this->eventDispatcher->dispatch($event, ResourceTypeBuildEvents::BUILD);
      $internalize_resource_type = $event->resourceTypeShouldBeDisabled();
      $fields = $event->getFields();
      $type_name = $event->getResourceTypeName();
    }
    return new ResourceType(
      $entity_type->id(),
      $bundle,
      $entity_type->getClass(),
      $internalize_resource_type,
      static::isLocatableResourceType($entity_type$bundle),
      static::isMutableResourceType($entity_type$bundle),
      static::isVersionableResourceType($entity_type),
      $fields,
      $type_name
    );
  }

  
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $relationship_fields = [
      'external' => new ResourceTypeRelationship('external'),
      'internal' => new ResourceTypeRelationship('internal'),
      'both' => new ResourceTypeRelationship('both'),
    ];
    $type_1 = new ResourceType('entity_type_1', 'bundle_1_1', EntityInterface::class, FALSE, TRUE, TRUE, FALSE, $relationship_fields);
    $type_2 = new ResourceType('entity_type_2', 'bundle_2_1', EntityInterface::class, TRUE, TRUE, TRUE, FALSE, $relationship_fields);
    $relatable_resource_types = [
      'external' => [$type_1],
      'internal' => [$type_2],
      'both' => [$type_1$type_2],
    ];
    $type_1->setRelatableResourceTypes($relatable_resource_types);
    $type_2->setRelatableResourceTypes($relatable_resource_types);
    // This type ensures that we can create routes for bundle IDs which might be     // cast from strings to integers. It should not affect related resource     // routing.
// Set the user IDs to something higher than 1 so these users cannot be     // mistaken for the site admin.     $this->testUsers[] = $this->createUser([], NULL, FALSE, ['uid' => 2]);
    $this->testUsers[] = $this->createUser([], NULL, FALSE, ['uid' => 3]);
    $this->serializer = $this->container->get('jsonapi.serializer');
  }

  /** * Tests the link collection normalizer. */
  public function testNormalize() {
    $link_context = new ResourceObject(new CacheableMetadata()new ResourceType('n/a', 'n/a', 'n/a'), 'n/a', NULL, []new LinkCollection([]));
    $link_collection = (new LinkCollection([]))
      ->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', ['title' => 'Most viewed']))
      ->withLink('related', new Link(new CacheableMetadata(), Url::fromUri('http://example.com/post/42'), 'related', ['title' => 'Top rated']))
      ->withContext($link_context);
    // Create the SUT.     $normalized = $this->getNormalizer()->normalize($link_collection)->getNormalization();
    $this->assertIsArray($normalized);
    foreach (array_keys($normalized) as $key) {
      $this->assertStringStartsWith('related', $key);
    }
    $this->assertSame([
      [
Home | Imprint | This part of the site doesn't use cookies.