/** * Sets up the entity manager to be tested. * * @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions * (optional) An array of entity type definitions. */ protected function setUpEntityManager($definitions = array()) { $class = $this->getMockClass(EntityInterface::class); foreach ($definitions as $key => $entity_type) { // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called // by \Drupal\Core\Entity\EntityManager::processDefinition() so it must // always be mocked. $entity_type->getLinkTemplates()->willReturn([]); // Give the entity type a legitimate class to return. $entity_type->getClass()->willReturn($class); $definitions[$key] = $entity_type->reveal(); } $this->discovery->getDefinition(Argument::cetera())->will(function ($args) use($definitions) { $entity_type_id = $args[0]; $exception_on_invalid = $args[1]; if (isset($definitions[$entity_type_id])) { return $definitions[$entity_type_id]; } elseif (!$exception_on_invalid) { return NULL; } else { throw new PluginNotFoundException($entity_type_id); } }); $this->discovery->getDefinitions()->willReturn($definitions); }
/** * Tests getDefinitions() and getDefinition(). */ function testDiscoveryInterface() { // Ensure that getDefinitions() returns the expected definitions. // For the arrays to be identical (instead of only equal), they must be // sorted equally, which seems unnecessary here. // The discovered definitions may contain circular references; use a custom // assertion message to prevent var_export() from getting called. $this->assertEqual($this->discovery->getDefinitions(), $this->expectedDefinitions, 'Expected definitions found.'); // Ensure that getDefinition() returns the expected definition. foreach ($this->expectedDefinitions as $id => $definition) { $this->assertDefinitionIdentical($this->discovery->getDefinition($id), $definition); } // Ensure that an empty array is returned if no plugin definitions are found. $this->assertIdentical($this->emptyDiscovery->getDefinitions(), array(), 'array() returned if no plugin definitions are found.'); // Ensure that NULL is returned as the definition of a non-existing plugin. $this->assertIdentical($this->emptyDiscovery->getDefinition('non_existing', FALSE), NULL, 'NULL returned as the definition of a non-existing plugin.'); }
/** * {@inheritdoc} * * @throws \Drupal\Component\Plugin\Exception\InvalidDeriverException * Thrown if the 'deriver' class specified in the plugin definition * does not implement \Drupal\Component\Plugin\Derivative\DeriverInterface. */ public function getDefinition($plugin_id, $exception_on_invalid = TRUE) { // This check is only for derivative plugins that have explicitly provided // an ID. This is not common, and can be expected to fail. Therefore, opt // out of the thrown exception, which will be handled when checking the // $base_plugin_id. $plugin_definition = $this->decorated->getDefinition($plugin_id, FALSE); list($base_plugin_id, $derivative_id) = $this->decodePluginId($plugin_id); $base_plugin_definition = $this->decorated->getDefinition($base_plugin_id, $exception_on_invalid); if ($base_plugin_definition) { $deriver = $this->getDeriver($base_plugin_id, $base_plugin_definition); if ($deriver) { $derivative_plugin_definition = $deriver->getDerivativeDefinition($derivative_id, $base_plugin_definition); // If a plugin defined itself as a derivative, merge in possible // defaults from the derivative. if ($derivative_id && isset($plugin_definition)) { $plugin_definition = $this->mergeDerivativeDefinition($plugin_definition, $derivative_plugin_definition); } else { $plugin_definition = $derivative_plugin_definition; } } } return $plugin_definition; }
/** * {@inheritdoc} */ public function createInstance($plugin_id, array $configuration = array()) { $plugin_definition = $this->discovery->getDefinition($plugin_id); $plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface); return new $plugin_class($configuration, $plugin_id, $plugin_definition); }
/** * {@inheritdoc} */ public function getDefinition($plugin_id, $exception_on_invalid = TRUE) { return $this->discovery->getDefinition($plugin_id, $exception_on_invalid); }