You are here

class ContextHandlerTraitTest in Rules 8.3

@coversDefaultClass \Drupal\rules\Context\ContextHandlerTrait @group Rules

Hierarchy

Expanded class hierarchy of ContextHandlerTraitTest

File

tests/src/Unit/ContextHandlerTraitTest.php, line 16

Namespace

Drupal\Tests\rules\Unit
View source
class ContextHandlerTraitTest extends RulesUnitTestBase {

  /**
   * Tests that a missing required context triggers an exception.
   *
   * @covers ::prepareContext
   */
  public function testMissingContext() {

    // Set the expected exception class and message.
    $this
      ->expectException(EvaluationException::class);
    $this
      ->expectExceptionMessage("Required context 'test' is missing for plugin 'testplugin'");

    // Set 'getContextValue' as mocked method.
    $trait = $this
      ->getMockForTrait(ContextHandlerTrait::class, [], '', TRUE, TRUE, TRUE, [
      'getContextValue',
    ]);
    $context_definition = $this
      ->prophesize(ContextDefinitionInterface::class);

    // Let the trait work with an empty configuration.
    $trait->configuration = ContextConfig::create()
      ->toArray();

    // Make the context required in the definition.
    $context_definition
      ->isRequired()
      ->willReturn(TRUE)
      ->shouldBeCalledTimes(1);
    $plugin = $this
      ->prophesize(ContextAwarePluginInterface::class);
    $plugin
      ->getContextDefinitions()
      ->willReturn([
      'test' => $context_definition
        ->reveal(),
    ])
      ->shouldBeCalled(1);
    $plugin
      ->getContextValue('test')
      ->willReturn(NULL)
      ->shouldBeCalled(1);
    $plugin
      ->getPluginId()
      ->willReturn('testplugin')
      ->shouldBeCalledTimes(1);
    $state = $this
      ->prophesize(ExecutionStateInterface::class);

    // Make the 'mapContext' method visible.
    $reflection = new \ReflectionClass($trait);
    $method = $reflection
      ->getMethod('prepareContext');
    $method
      ->setAccessible(TRUE);
    $method
      ->invokeArgs($trait, [
      $plugin
        ->reveal(),
      $state
        ->reveal(),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextHandlerTraitTest::testMissingContext public function Tests that a missing required context triggers an exception.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RulesUnitTestBase::$expressionManager protected property The mocked expression manager object. 1
RulesUnitTestBase::$falseConditionExpression protected property A mocked condition that always evaluates to FALSE.
RulesUnitTestBase::$rulesDebugLogger protected property The mocked expression manager object.
RulesUnitTestBase::$testActionExpression protected property A mocked dummy action object.
RulesUnitTestBase::$testFirstActionExpression protected property A mocked dummy action object.
RulesUnitTestBase::$trueConditionExpression protected property A mocked condition that always evaluates to TRUE.
RulesUnitTestBase::setUp protected function Overrides UnitTestCase::setUp 4
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.