getMock(TypedDataManagerInterface::class); $typed_data_manager->expects($this->once()) ->method('getCanonicalRepresentation') ->will($this->returnCallback([$this, 'getCanonicalRepresentation'])); $container = new ContainerBuilder(); $container->set('typed_data_manager', $typed_data_manager); \Drupal::setContainer($container); $definition = new ContextDefinition('any'); $data_definition = DataDefinition::create('string'); $this->typedData = new StringData($data_definition); $this->typedData->setValue('example string'); $context = new Context($definition, $this->typedData); $value = $context->getContextValue(); $this->assertSame($value, $this->typedData->getValue()); } /** * Helper mock callback to return the typed data value. */ public function getCanonicalRepresentation() { return $this->typedData->getValue(); } }