StaticDiscovery example


class MockBlockManager extends PluginManagerBase {

  use StringTranslationTrait;

  public function __construct() {

    // Create the object that can be used to return definitions for all the     // plugins available for this type. Most real plugin managers use a richer     // discovery implementation, but StaticDiscovery lets us add some simple     // mock plugins for unit testing.     $this->discovery = new StaticDiscovery();

    // Derivative plugins are plugins that are derived from a base plugin     // definition and some site configuration (examples below). To allow for     // such plugins, we add the DerivativeDiscoveryDecorator to our discovery     // object.     $this->discovery = new DerivativeDiscoveryDecorator($this->discovery);

    // The plugin definitions that follow are based on work that is in progress     // for the Drupal 8 Blocks and Layouts initiative     // (http://groups.drupal.org/node/213563). As stated above, we set     // definitions here, because this is for unit testing. Real plugin managers

  public function __construct(\Traversable $namespaces, array $definitions, ModuleHandlerInterface $module_handler = NULL, $alter_hook = NULL, $interface = NULL) {
    // Create the object that can be used to return definitions for all the     // plugins available for this type. Most real plugin managers use a richer     // discovery implementation, but StaticDiscovery lets us add some simple     // mock plugins for unit testing.     $this->discovery = new StaticDiscovery();
    $this->factory = new DefaultFactory($this->discovery, $interface);

    // Add the static definitions.     foreach ($definitions as $key => $definition) {
      $this->discovery->setDefinition($key$definition);
    }

    $this->moduleHandler = $module_handler;

    if ($alter_hook) {
      $this->alterInfo($alter_hook);
    }
/** * Constructs a new DefaultsTestPluginManager instance. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. */
  public function __construct(ModuleHandlerInterface $module_handler) {
    // Create the object that can be used to return definitions for all the     // plugins available for this type. Most real plugin managers use a richer     // discovery implementation, but StaticDiscovery lets us add some simple     // mock plugins for unit testing.     $this->discovery = new StaticDiscovery();
    $this->factory = new DefaultFactory($this, 'Drupal\Component\Plugin\PluginInspectionInterface');
    $this->moduleHandler = $module_handler;

    // Specify default values.     $this->defaults = [
      'metadata' => [
        'default' => TRUE,
      ],
    ];

    // Add a plugin with a custom value.
/** * Defines a plugin manager used by Plugin API unit tests. */
class TestPluginManager extends PluginManagerBase {

  public function __construct() {

    // Create the object that can be used to return definitions for all the     // plugins available for this type. Most real plugin managers use a richer     // discovery implementation, but StaticDiscovery lets us add some simple     // mock plugins for unit testing.     $this->discovery = new StaticDiscovery();

    // A simple plugin: a mock user login block.     $this->discovery->setDefinition('user_login', [
      'label' => 'User login',
      'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock',
    ]);

    // In addition to finding all of the plugins available for a type, a plugin     // type must also be able to create instances of that plugin. For example, a     // specific instance of a "User login" block, configured with a custom     // title. To handle plugin instantiation, plugin managers can use one of the

      'orange' => [
        'label' => 'Orange',
        'color' => 'orange',
      ],
    ];
    // Instead of registering the empty discovery component first and then     // setting the plugin definitions, we set them first and then delete them     // again. This implicitly tests StaticDiscovery::deleteDefinition() (in     // addition to StaticDiscovery::setDefinition() which we need to use     // anyway).     $discovery = new StaticDiscovery();
    foreach ($this->expectedDefinitions as $plugin_id => $definition) {
      $discovery->setDefinition($plugin_id$definition);
    }
    $this->discovery = clone $discovery;
    foreach ($this->expectedDefinitions as $plugin_id => $definition) {
      $discovery->deleteDefinition($plugin_id);
    }
    $this->emptyDiscovery = $discovery;
  }

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