function DiscoveryCachedTraitTest::testGetDefinition

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

@covers ::getDefinition @dataProvider providerGetDefinition

File

core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryCachedTraitTest.php, line 35

Class

DiscoveryCachedTraitTest
@coversDefaultClass \Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait @uses \Drupal\Component\Plugin\Discovery\DiscoveryTrait @group Plugin

Namespace

Drupal\Tests\Component\Plugin\Discovery

Code

public function testGetDefinition($expected, $cached_definitions, $get_definitions, $plugin_id) {
    // Mock a DiscoveryCachedTrait.
    $trait = $this->getMockForTrait('Drupal\\Component\\Plugin\\Discovery\\DiscoveryCachedTrait');
    $reflection_definitions = new \ReflectionProperty($trait, 'definitions');
    $reflection_definitions->setAccessible(TRUE);
    // getDefinition() needs the ::$definitions property to be set in one of two
    // ways: 1) As existing cached data, or 2) as a side-effect of calling
    // getDefinitions().
    // If there are no cached definitions, then we have to fake the side-effect
    // of getDefinitions().
    if (count($cached_definitions) < 1) {
        $trait->expects($this->once())
            ->method('getDefinitions')
            ->willReturnCallback(function () use ($reflection_definitions, $trait, $get_definitions) {
            $reflection_definitions->setValue($trait, $get_definitions);
            return $get_definitions;
        });
    }
    else {
        // Put $cached_definitions into our mocked ::$definitions.
        $reflection_definitions->setValue($trait, $cached_definitions);
    }
    // Call getDefinition(), with $exception_on_invalid always FALSE.
    $this->assertSame($expected, $trait->getDefinition($plugin_id, FALSE));
}

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