class ContextDefinitionTest

Same name in this branch
  1. 10 core/tests/Drupal/KernelTests/Core/Plugin/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\ContextDefinitionTest
  2. 10 core/tests/Drupal/KernelTests/Core/Plugin/Annotation/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\Annotation\ContextDefinitionTest
Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Plugin/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\ContextDefinitionTest
  2. 9 core/tests/Drupal/KernelTests/Core/Plugin/Annotation/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\Annotation\ContextDefinitionTest
  3. 9 core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionTest
  4. 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\ContextDefinitionTest
  5. 8.9.x core/tests/Drupal/KernelTests/Core/Plugin/Annotation/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\Annotation\ContextDefinitionTest
  6. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionTest
  7. 11.x core/tests/Drupal/KernelTests/Core/Plugin/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\ContextDefinitionTest
  8. 11.x core/tests/Drupal/KernelTests/Core/Plugin/Annotation/ContextDefinitionTest.php \Drupal\KernelTests\Core\Plugin\Annotation\ContextDefinitionTest
  9. 11.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionTest

Tests the ContextDefinition class.

@group Plugin

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

Hierarchy

Expanded class hierarchy of ContextDefinitionTest

File

core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php, line 19

Namespace

Drupal\Tests\Core\Plugin\Context
View source
class ContextDefinitionTest extends UnitTestCase {
    
    /**
     * Very simple data provider.
     */
    public static function providerGetDataDefinition() {
        return [
            [
                TRUE,
            ],
            [
                FALSE,
            ],
        ];
    }
    
    /**
     * @dataProvider providerGetDataDefinition
     * @covers ::getDataDefinition
     * @uses \Drupal
     */
    public function testGetDataDefinition($is_multiple) : void {
        $data_type = 'valid';
        $mock_data_definition = $this->createMock(ContextDefinitionInterface::class);
        $mock_data_definition->expects($this->once())
            ->method('setLabel')
            ->willReturnSelf();
        $mock_data_definition->expects($this->once())
            ->method('setDescription')
            ->willReturnSelf();
        $mock_data_definition->expects($this->once())
            ->method('setRequired')
            ->willReturnSelf();
        $mock_data_definition->expects($this->once())
            ->method('getConstraints')
            ->willReturn([]);
        $mock_data_definition->expects($this->once())
            ->method('setConstraints')
            ->willReturn(NULL);
        // Follow code paths for both multiple and non-multiple definitions.
        $create_definition_method = 'createDataDefinition';
        if ($is_multiple) {
            $create_definition_method = 'createListDataDefinition';
        }
        $mock_data_manager = $this->createMock(TypedDataManagerInterface::class);
        // Our mocked data manager will return our mocked data definition for a
        // valid data type.
        $mock_data_manager->expects($this->once())
            ->method($create_definition_method)
            ->willReturnMap([
            [
                'not_valid',
                NULL,
            ],
            [
                'valid',
                $mock_data_definition,
            ],
        ]);
        // Mock a ContextDefinition object, setting up expectations for many of the
        // methods.
        $mock_context_definition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinition')
            ->disableOriginalConstructor()
            ->onlyMethods([
            'isMultiple',
            'getTypedDataManager',
            'getDataType',
            'getLabel',
            'getDescription',
            'isRequired',
            'getConstraints',
            'setConstraints',
        ])
            ->getMock();
        $mock_context_definition->expects($this->once())
            ->method('isMultiple')
            ->willReturn($is_multiple);
        $mock_context_definition->expects($this->once())
            ->method('getTypedDataManager')
            ->willReturn($mock_data_manager);
        $mock_context_definition->expects($this->once())
            ->method('getDataType')
            ->willReturn($data_type);
        $mock_context_definition->expects($this->once())
            ->method('getConstraints')
            ->willReturn([]);
        $this->assertSame($mock_data_definition, $mock_context_definition->getDataDefinition());
    }
    
    /**
     * @dataProvider providerGetDataDefinition
     * @covers ::getDataDefinition
     * @uses \Drupal
     */
    public function testGetDataDefinitionInvalidType($is_multiple) : void {
        // Since we're trying to make getDataDefinition() throw an exception in
        // isolation, we use a data type which is not valid.
        $data_type = 'not_valid';
        $mock_data_definition = $this->createMock('\\Drupal\\Core\\TypedData\\ListDataDefinitionInterface');
        // Follow code paths for both multiple and non-multiple definitions.
        $create_definition_method = 'createDataDefinition';
        if ($is_multiple) {
            $create_definition_method = 'createListDataDefinition';
        }
        $mock_data_manager = $this->createMock(TypedDataManagerInterface::class);
        // Our mocked data manager will return NULL for a non-valid data type. This
        // will eventually cause getDataDefinition() to throw an exception.
        $mock_data_manager->expects($this->once())
            ->method($create_definition_method)
            ->willReturnMap([
            [
                'not_valid',
                NULL,
            ],
            [
                'valid',
                $mock_data_definition,
            ],
        ]);
        // Mock a ContextDefinition object with expectations for only the methods
        // that will be called before the expected exception.
        $mock_context_definition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinition')
            ->disableOriginalConstructor()
            ->onlyMethods([
            'isMultiple',
            'getTypedDataManager',
            'getDataType',
        ])
            ->getMock();
        $mock_context_definition->expects($this->once())
            ->method('isMultiple')
            ->willReturn($is_multiple);
        $mock_context_definition->expects($this->once())
            ->method('getTypedDataManager')
            ->willReturn($mock_data_manager);
        $mock_context_definition->method('getDataType')
            ->willReturn($data_type);
        $this->expectException(\Exception::class);
        $mock_context_definition->getDataDefinition();
    }
    
    /**
     * Data provider for testGetConstraint.
     */
    public static function providerGetConstraint() {
        return [
            [
                NULL,
                [],
                'nonexistent_constraint_name',
            ],
            [
                'not_null',
                [
                    'constraint_name' => 'not_null',
                ],
                'constraint_name',
            ],
        ];
    }
    
    /**
     * @dataProvider providerGetConstraint
     * @covers ::getConstraint
     * @uses \Drupal
     */
    public function testGetConstraint($expected, $constraint_array, $constraint) : void {
        $mock_context_definition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinition')
            ->disableOriginalConstructor()
            ->onlyMethods([
            'getConstraints',
        ])
            ->getMock();
        $mock_context_definition->expects($this->once())
            ->method('getConstraints')
            ->willReturn($constraint_array);
        $this->assertEquals($expected, $mock_context_definition->getConstraint($constraint));
    }
    
    /**
     * @covers ::getDefaultValue
     * @covers ::setDefaultValue
     */
    public function testDefaultValue() : void {
        $context_definition = new ContextDefinition();
        $this->assertNull($context_definition->getDefaultValue());
        $context_definition->setDefaultValue('test');
        $this->assertSame('test', $context_definition->getDefaultValue());
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
ContextDefinitionTest::providerGetConstraint public static function Data provider for testGetConstraint.
ContextDefinitionTest::providerGetDataDefinition public static function Very simple data provider.
ContextDefinitionTest::testDefaultValue public function @covers ::getDefaultValue
@covers ::setDefaultValue
ContextDefinitionTest::testGetConstraint public function @dataProvider providerGetConstraint
@covers ::getConstraint
@uses \Drupal
ContextDefinitionTest::testGetDataDefinition public function @dataProvider providerGetDataDefinition
@covers ::getDataDefinition
@uses \Drupal
ContextDefinitionTest::testGetDataDefinitionInvalidType public function @dataProvider providerGetDataDefinition
@covers ::getDataDefinition
@uses \Drupal
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 358
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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