function DerivativeDiscoveryDecoratorTest::testSingleExistingDerivative

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Discovery/DerivativeDiscoveryDecoratorTest.php \Drupal\Tests\Core\Plugin\Discovery\DerivativeDiscoveryDecoratorTest::testSingleExistingDerivative()
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Discovery/DerivativeDiscoveryDecoratorTest.php \Drupal\Tests\Core\Plugin\Discovery\DerivativeDiscoveryDecoratorTest::testSingleExistingDerivative()
  3. 11.x core/tests/Drupal/Tests/Core/Plugin/Discovery/DerivativeDiscoveryDecoratorTest.php \Drupal\Tests\Core\Plugin\Discovery\DerivativeDiscoveryDecoratorTest::testSingleExistingDerivative()

Tests a single definition when a derivative already exists.

File

core/tests/Drupal/Tests/Core/Plugin/Discovery/DerivativeDiscoveryDecoratorTest.php, line 225

Class

DerivativeDiscoveryDecoratorTest
Unit tests for the derivative discovery decorator.

Namespace

Drupal\Tests\Core\Plugin\Discovery

Code

public function testSingleExistingDerivative() : void {
    $base_definition = [
        'id' => 'non_container_aware_discovery',
        'deriver' => '\\Drupal\\Tests\\Core\\Plugin\\Discovery\\TestDerivativeDiscovery',
        'string' => 'string',
        'empty_string' => 'not_empty',
        'array' => [
            'one',
            'two',
        ],
        'empty_array' => [
            'three',
        ],
        'null_value' => TRUE,
    ];
    // This will clash with a derivative id.
    // @see \Drupal\Tests\Core\Plugin\Discovery\TestDerivativeDiscovery
    $derivative_definition = [
        'id' => 'non_container_aware_discovery:test_discovery_1',
        'string' => 'string',
        'empty_string' => '',
        'array' => [
            'one',
            'two',
        ],
        'empty_array' => [],
        'null_value' => NULL,
    ];
    $ids = [
        $derivative_definition['id'],
        $base_definition['id'],
    ];
    $this->discoveryMain
        ->expects($this->exactly(count($ids)))
        ->method('getDefinition')
        ->with($this->callback(function (string $id) use (&$ids) : bool {
        return array_shift($ids) === $id;
    }))
        ->willReturnOnConsecutiveCalls($derivative_definition, $base_definition);
    $discovery = new DerivativeDiscoveryDecorator($this->discoveryMain);
    $expected = $base_definition;
    $expected['id'] = 'non_container_aware_discovery:test_discovery_1';
    $this->assertEquals($expected, $discovery->getDefinition('non_container_aware_discovery:test_discovery_1'));
}

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