class ContextDefinitionIsSatisfiedTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionIsSatisfiedTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionIsSatisfiedTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionIsSatisfiedTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionIsSatisfiedTest
  3. 10 core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionIsSatisfiedTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionIsSatisfiedTest

@coversDefaultClass \Drupal\Core\Plugin\Context\ContextDefinition @group Plugin

Hierarchy

Expanded class hierarchy of ContextDefinitionIsSatisfiedTest

File

core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionIsSatisfiedTest.php, line 25

Namespace

Drupal\Tests\Core\Plugin\Context
View source
class ContextDefinitionIsSatisfiedTest extends UnitTestCase {
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $namespaces = new \ArrayObject([
            'Drupal\\Core\\TypedData' => $this->root . '/core/lib/Drupal/Core/TypedData',
            'Drupal\\Core\\Validation' => $this->root . '/core/lib/Drupal/Core/Validation',
            'Drupal\\Tests\\Core\\Plugin\\Fixtures' => $this->root . '/core/tests/Drupal/Tests/Core/Plugin/Fixtures',
        ]);
        $cache_backend = new NullBackend('cache');
        $module_handler = $this->prophesize(ModuleHandlerInterface::class);
        $class_resolver = $this->prophesize(ClassResolverInterface::class);
        $class_resolver->getInstanceFromDefinition(Argument::type('string'))
            ->will(function ($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()));
        $string_translation = new TranslationManager(new LanguageDefault([]));
        $container = new ContainerBuilder();
        $container->set('typed_data_manager', $type_data_manager);
        $container->set('string_translation', $string_translation);
        \Drupal::setContainer($container);
    }
    
    /**
     * Tests that context requirements is satisfied as expected.
     *
     * @param bool $expected
     *   The expected outcome.
     * @param \Drupal\Core\Plugin\Context\ContextDefinition $requirement
     *   The requirement to check against.
     * @param \Drupal\Core\Plugin\Context\ContextDefinition $definition
     *   The context definition to check.
     * @param mixed $value
     *   (optional) The value to set on the context, defaults to NULL.
     *
     * @covers ::isSatisfiedBy
     * @covers ::dataTypeMatches
     * @covers ::getSampleValues
     * @covers ::getConstraintObjects
     *
     * @dataProvider providerTestIsSatisfiedBy
     */
    public function testIsSatisfiedBy($expected, ContextDefinition $requirement, ContextDefinition $definition, $value = NULL) : void {
        $context = new Context($definition, $value);
        $this->assertSame($expected, $requirement->isSatisfiedBy($context));
    }
    
    /**
     * Provides test data for ::testIsSatisfiedBy().
     */
    public static function providerTestIsSatisfiedBy() {
        $data = [];
        // Simple data types.
        $data['both any'] = [
            TRUE,
            new ContextDefinition('any'),
            new ContextDefinition('any'),
        ];
        $data['requirement any'] = [
            TRUE,
            new ContextDefinition('any'),
            new ContextDefinition('integer'),
        ];
        $data['integer, out of range'] = [
            FALSE,
            (new ContextDefinition('integer'))->addConstraint('Range', [
                'min' => 0,
                'max' => 10,
            ]),
            new ContextDefinition('integer'),
            20,
        ];
        $data['integer, within range'] = [
            TRUE,
            (new ContextDefinition('integer'))->addConstraint('Range', [
                'min' => 0,
                'max' => 10,
            ]),
            new ContextDefinition('integer'),
            5,
        ];
        $data['integer, no value'] = [
            TRUE,
            (new ContextDefinition('integer'))->addConstraint('Range', [
                'min' => 0,
                'max' => 10,
            ]),
            new ContextDefinition('integer'),
        ];
        $data['non-integer, within range'] = [
            FALSE,
            (new ContextDefinition('integer'))->addConstraint('Range', [
                'min' => 0,
                'max' => 10,
            ]),
            new ContextDefinition('any'),
            5,
        ];
        // Inherited context definition class.
        $data['both any, inherited context requirement definition'] = [
            TRUE,
            new InheritedContextDefinition('any'),
            new ContextDefinition('any'),
        ];
        $data['specific definition, generic requirement'] = [
            TRUE,
            new ContextDefinition('test_data_type'),
            new ContextDefinition('test_data_type:a_variant'),
        ];
        $data['generic definition, specific requirement'] = [
            FALSE,
            new ContextDefinition('test_data_type:a_variant'),
            new ContextDefinition('test_data_type'),
        ];
        return $data;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ContextDefinitionIsSatisfiedTest::providerTestIsSatisfiedBy public static function Provides test data for ::testIsSatisfiedBy().
ContextDefinitionIsSatisfiedTest::setUp protected function Overrides UnitTestCase::setUp
ContextDefinitionIsSatisfiedTest::testIsSatisfiedBy public function Tests that context requirements is satisfied as expected.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.