function StaticDiscoveryDecoratorTest::testGetDefinitions

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php \Drupal\Tests\Component\Plugin\Discovery\StaticDiscoveryDecoratorTest::testGetDefinitions()
  2. 10 core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php \Drupal\Tests\Component\Plugin\Discovery\StaticDiscoveryDecoratorTest::testGetDefinitions()
  3. 11.x core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php \Drupal\Tests\Component\Plugin\Discovery\StaticDiscoveryDecoratorTest::testGetDefinitions()

@covers ::getDefinitions @dataProvider providerGetDefinitions

File

core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php, line 131

Class

StaticDiscoveryDecoratorTest
@group Plugin @coversDefaultClass \Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator

Namespace

Drupal\Tests\Component\Plugin\Discovery

Code

public function testGetDefinitions($has_register_definitions, $definitions) {
    // Mock our StaticDiscoveryDecorator.
    $mock_decorator = $this->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\StaticDiscoveryDecorator')
        ->disableOriginalConstructor()
        ->addMethods([
        'registeredDefinitionCallback',
    ])
        ->getMock();
    // Set up the ::$registerDefinitions property.
    $ref_register_definitions = new \ReflectionProperty($mock_decorator, 'registerDefinitions');
    $ref_register_definitions->setAccessible(TRUE);
    if ($has_register_definitions) {
        // Set the callback object on the mocked decorator.
        $ref_register_definitions->setValue($mock_decorator, [
            $this->getRegisterDefinitionsCallback(),
            'registerDefinitionsCallback',
        ]);
    }
    else {
        // There should be no registerDefinitions callback.
        $ref_register_definitions->setValue($mock_decorator, NULL);
    }
    // Set up ::$definitions to an empty array.
    $ref_definitions = new \ReflectionProperty($mock_decorator, 'definitions');
    $ref_definitions->setAccessible(TRUE);
    $ref_definitions->setValue($mock_decorator, []);
    // Mock a decorated object.
    $mock_decorated = $this->getMockBuilder('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface')
        ->onlyMethods([
        'getDefinitions',
    ])
        ->getMockForAbstractClass();
    // Our mocked method will return any arguments sent to it.
    $mock_decorated->expects($this->once())
        ->method('getDefinitions')
        ->willReturn($definitions);
    // Set up ::$decorated to our mocked decorated object.
    $ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated');
    $ref_decorated->setAccessible(TRUE);
    $ref_decorated->setValue($mock_decorator, $mock_decorated);
    // Exercise getDefinitions(). It calls parent::getDefinitions() but in this
    // case there will be no side-effects.
    $this->assertEquals($definitions, $mock_decorator->getDefinitions());
}

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