setContainerWithKernel example


  public function testSleepWithPluginCollections() {
    $instance_id = 'the_instance_id';
    $instance = new TestConfigurablePlugin([]$instance_id[]);

    $plugin_manager = $this->prophesize(PluginManagerInterface::class);
    $plugin_manager->createInstance($instance_id['id' => $instance_id])->willReturn($instance);

    // Also set up a container with the plugin manager so that we can assert     // that the plugin manager itself is also not serialized.     $container = TestKernel::setContainerWithKernel();
    $container->set('plugin.manager.foo', $plugin_manager->reveal());

    $entity_values = ['the_plugin_collection_config' => [$instance_id => ['foo' => 'original_value']]];
    $entity = new TestConfigEntityWithPluginCollections($entity_values$this->entityTypeId);
    $entity->setPluginManager($plugin_manager->reveal());

    // After creating the entity, change the plugin configuration.     $instance->setConfiguration(['foo' => 'new_value']);

    // After changing the plugin configuration, the entity still has the     // original value.
$this->assertEquals('sites/example', DrupalKernel::findSitePath($request, FALSE, $vfs_root->url('drupal_root')));
  }

  /** * @covers ::getServiceIdMapping * @group legacy */
  public function testGetServiceIdMapping() {
    $this->expectDeprecation("Drupal\Core\DrupalKernel::getServiceIdMapping() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
    $this->expectDeprecation("Drupal\Core\DrupalKernel::collectServiceIdMapping() is deprecated in drupal:9.5.1 and is removed from drupal:11.0.0. Use the 'Drupal\Component\DependencyInjection\ReverseContainer' service instead. See https://www.drupal.org/node/3327942");
    $service = new BarClass();
    $container = TestKernel::setContainerWithKernel();
    $container->set('bar', $service);
    $this->assertEquals($container->get('kernel')->getServiceIdMapping()[$container->generateServiceIdHash($service)], 'bar');
  }

}

/** * A fake autoloader for testing. */
class FakeAutoloader {

  

class DependencySerializationTest extends UnitTestCase {

  /** * @covers ::__sleep * @covers ::__wakeup */
  public function testSerialization() {
    // Create a pseudo service and dependency injected object.     $service = new \stdClass();
    $container = TestKernel::setContainerWithKernel();
    $container->set('test_service', $service);
    $this->assertSame($container$container->get('service_container'));

    $dependencySerialization = new DependencySerializationTestDummy($service);
    $dependencySerialization->setContainer($container);

    $string = serialize($dependencySerialization);
    /** @var \Drupal\Tests\Core\DependencyInjection\DependencySerializationTestDummy $dependencySerialization */
    $dependencySerialization = unserialize($string);

    $this->assertTrue($container->has(ReverseContainer::class));
    
/** * Tests the serialization of a shared temp store. */
  public function testSerialization() {
    // Add an unserializable request to the request stack. If the tempstore     // didn't use DependencySerializationTrait, an exception would be thrown     // when we try to serialize the tempstore.     $unserializable_request = new UnserializableRequest();

    $this->requestStack->push($unserializable_request);

    $container = TestKernel::setContainerWithKernel();
    $container->set('request_stack', $this->requestStack);
    \Drupal::setContainer($container);

    $store = unserialize(serialize($this->tempStore));
    $this->assertInstanceOf(SharedTempStore::class$store);

    $reflected_request_stack = (new \ReflectionObject($store))->getProperty('requestStack');
    $request_stack = $reflected_request_stack->getValue($store);
    $this->assertEquals($this->requestStack, $request_stack);
    $this->assertSame($unserializable_request$request_stack->pop());
  }

}
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $class_resolver = $this->prophesize(ClassResolverInterface::class);
    $class_resolver->getInstanceFromDefinition(Argument::type('string'))->will(function D$arguments) {
      $class_name = $arguments[0];
      return new $class_name();
    });
    $type_data_manager = new TypedDataManager($namespaces$cache_backend$module_handler->reveal()$class_resolver->reveal());
    $type_data_manager->setValidationConstraintManager(
      new ConstraintManager($namespaces$cache_backend$module_handler->reveal())
    );

    $container = TestKernel::setContainerWithKernel();
    $container->set('typed_data_manager', $type_data_manager);
    \Drupal::setContainer($container);
  }

  /** * @covers ::checkRequirements * * @dataProvider providerTestCheckRequirements */
  public function testCheckRequirements($contexts$requirements$expected) {
    $this->assertSame($expected$this->contextHandler->checkRequirements($contexts$requirements));
  }
Home | Imprint | This part of the site doesn't use cookies.