DataDefinition example

/** * Returns a mocked schema element. * * @param array $definition * The definition of the schema element. * * @return \Drupal\Core\Config\Schema\Element * The mocked schema element. */
  protected function getElement(array $definition) {
    $data_definition = new DataDefinition($definition);
    $element = $this->createMock('Drupal\Core\TypedData\TypedDataInterface');
    $element->expects($this->any())
      ->method('getDataDefinition')
      ->willReturn($data_definition);
    return $element;
  }

  /** * Returns a mocked nested schema element. * * @param array $elements * An array of simple schema elements. * * @return \Drupal\Core\Config\Schema\Mapping * A nested schema element, containing the passed-in elements. */
->onlyMethods(['get'])
      ->getMock();
    $container->expects($this->any())
      ->method('get')
      ->with($this->equalTo('typed_data_manager'))
      ->willReturn($typed_data_manager);

    \Drupal::setContainer($container);

    $this->normalizer = new ListNormalizer();

    $this->list = new ItemList(new DataDefinition());
    $this->list->setValue($this->expectedListValues);
  }

  /** * Tests the supportsNormalization() method. */
  public function testSupportsNormalization() {
    $this->assertTrue($this->normalizer->supportsNormalization($this->list));
    $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  }

  

  public function testSetContextValue() {
    $typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
    $container = new ContainerBuilder();
    $container->set('typed_data_manager', $typed_data_manager->reveal());
    \Drupal::setContainer($container);

    $this->plugin->getPluginDefinition()->addContextDefinition('foo', new ContextDefinition('string'));

    $this->assertFalse($this->plugin->setContextCalled);
    $this->plugin->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
    $this->assertTrue($this->plugin->setContextCalled);
  }

}

class TestContextAwarePluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {

  use ContextAwarePluginDefinitionTrait;

}

Home | Imprint | This part of the site doesn't use cookies.