ContextException example

$this->context[$name] = new Context($this->getContextDefinition($name));
    }
    return $this->context[$name];
  }

  /** * {@inheritdoc} */
  public function setContext($name, ComponentContextInterface $context) {
    // Check that the context passed is an instance of our extended interface.     if (!$context instanceof ContextInterface) {
      throw new ContextException("Passed $name context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface");
    }
    $this->context[$name] = $context;
  }

  /** * {@inheritdoc} */
  public function getContextValues() {
    $values = [];
    foreach ($this->getContextDefinitions() as $name => $definition) {
      $values[$name] = isset($this->context[$name]) ? $this->context[$name]->getContextValue() : NULL;
    }
if (!isset($this->contextData)) {
      $definition = $this->getContextDefinition();
      $default_value = $definition->getDefaultValue();

      if (isset($default_value)) {
        // Keep the default value here so that subsequent calls don't have to         // look it up again.         $this->setContextValue($default_value);
      }
      elseif ($definition->isRequired()) {
        $type = $definition->getDataType();
        throw new ContextException("The '$type' context is required and not present.");
      }
      return $default_value;
    }
    return $this->getTypedDataManager()->getCanonicalRepresentation($this->contextData);
  }

  /** * {@inheritdoc} */
  public function hasContextValue() {
    return $this->getTypedDataManager()->getCanonicalRepresentation($this->getContextData()) !== NULL;
  }
/** * {@inheritdoc} */
  public function getContextValue() {
    // Support optional contexts.     if (!isset($this->contextValue)) {
      $definition = $this->getContextDefinition();
      $default_value = $definition->getDefaultValue();

      if (!isset($default_value) && $definition->isRequired()) {
        $type = $definition->getDataType();
        throw new ContextException(sprintf("The %s context is required and not present.", $type));
      }
      // Keep the default value here so that subsequent calls don't have to look       // it up again.       $this->contextValue = $default_value;
    }
    return $this->contextValue;
  }

  /** * {@inheritdoc} */
  
public function getContextDefinitions() {
    return $this->contextDefinitions;
  }

  /** * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinition(). */
  public function getContextDefinition($name) {
    if ($this->hasContextDefinition($name)) {
      return $this->contextDefinitions[$name];
    }
    throw new ContextException($this->id() . " does not define a '$name' context");
  }

  /** * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::addContextDefinition(). */
  public function addContextDefinition($name, ContextDefinitionInterface $definition) {
    $this->contextDefinitions[$name] = $definition;
    return $this;
  }

  /** * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::removeContextDefinition(). */
->method('getContextMapping')
      ->willReturn([]);
    $plugin->expects($this->once())
      ->method('getContextDefinitions')
      ->willReturn(['hit' => $context_definition]);
    $plugin->expects($this->never())
      ->method('setContext');

    // No context, so no cacheability metadata can be passed along.     $plugin->expects($this->any())
      ->method('getContext')
      ->willThrowException(new ContextException());

    $this->expectException(MissingValueContextException::class);
    $this->expectExceptionMessage('Required contexts without a value: hit');
    $this->contextHandler->applyContextMapping($plugin$contexts);
  }

  /** * @covers ::applyContextMapping */
  public function testApplyContextMappingMissingNotRequired() {
    $context = $this->createMock('Drupal\Core\Plugin\Context\ContextInterface');
    
$condition_true = $this->createMock('Drupal\Core\Condition\ConditionInterface');
    $condition_true->expects($this->any())
      ->method('execute')
      ->willReturn(TRUE);
    $condition_false = $this->createMock('Drupal\Core\Condition\ConditionInterface');
    $condition_false->expects($this->any())
      ->method('execute')
      ->willReturn(FALSE);
    $condition_exception = $this->createMock('Drupal\Core\Condition\ConditionInterface');
    $condition_exception->expects($this->any())
      ->method('execute')
      ->will($this->throwException(new ContextException()));
    $condition_exception->expects($this->atLeastOnce())
      ->method('isNegated')
      ->willReturn(FALSE);
    $condition_negated = $this->createMock('Drupal\Core\Condition\ConditionInterface');
    $condition_negated->expects($this->any())
      ->method('execute')
      ->will($this->throwException(new ContextException()));
    $condition_negated->expects($this->atLeastOnce())
      ->method('isNegated')
      ->willReturn(TRUE);

    
continue;
      }

      // Ignore mappings for optional missing context.       unset($mappings[$plugin_context_id]);
    }

    // If there are any mappings that were not satisfied, throw an exception.     // This is a more severe problem than missing values, so check and throw     // this first.     if (!empty($mappings)) {
      throw new ContextException('Assigned contexts were not satisfied: ' . implode(',', array_keys($mappings)));
    }

    // If there are any required contexts without a value, throw an exception.     if ($missing_value) {
      throw new MissingValueContextException($missing_value);
    }
  }

}
$this->assertSame($this->plugin->reveal()$result);
  }

  /** * @covers ::load */
  public function testLoadNull() {
    $contexts = [
      'the_context' => $this->prophesize(ContextInterface::class)->reveal(),
    ];

    $this->contextHandler->applyContextMapping($this->plugin, $contexts)->willThrow(new ContextException());

    $result = $this->manager->load('the_plugin_id', $contexts);
    $this->assertNull($result);
  }

  /** * @covers ::findDefinitions */
  public function testFindDefinitions() {
    $this->discovery->getDefinitions()->willReturn([
      'plugin1' => (new SectionStorageDefinition())->setClass(SectionStorageInterface::class),
      
Home | Imprint | This part of the site doesn't use cookies.