DiscoveryCachedTraitTest.php

Same filename and directory in other branches
  1. 11.x core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryCachedTraitTest.php
  2. 10 core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryCachedTraitTest.php
  3. 9 core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryCachedTraitTest.php
  4. 8.9.x core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryCachedTraitTest.php

Namespace

Drupal\Tests\Component\Plugin\Discovery

File

core/tests/Drupal/Tests/Component/Plugin/Discovery/DiscoveryCachedTraitTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Component\Plugin\Discovery;

use Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait;
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
use PHPUnit\Framework\Attributes\CoversTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\UsesTrait;
use PHPUnit\Framework\TestCase;

/**
 * Tests Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait.
 */
class DiscoveryCachedTraitTest extends TestCase {
  
  /**
   * Data provider for testGetDefinition().
   *
   * @return array
   *   - Expected result from getDefinition().
   *   - Cached definitions to be placed into self::$definitions
   *   - Definitions to be returned by getDefinitions().
   *   - Plugin name to query for.
   */
  public static function providerGetDefinition() : array {
    return [
      [
        'definition',
        [],
        [
          'plugin_name' => 'definition',
        ],
        'plugin_name',
      ],
      [
        'definition',
        [
          'plugin_name' => 'definition',
        ],
        [],
        'plugin_name',
      ],
      [
        NULL,
        [
          'plugin_name' => 'definition',
        ],
        [],
        'bad_plugin_name',
      ],
    ];
  }
  
  /**
   * Tests get definition.
   */
  public function testGetDefinition($expected, $cached_definitions, $get_definitions, $plugin_id) : void {
    $trait = $this->getMockBuilder(DiscoveryCachedTraitMockableClass::class)
      ->onlyMethods([
      'getDefinitions',
    ])
      ->getMock();
    $reflection_definitions = new \ReflectionProperty($trait, 'definitions');
    // 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 {
      $trait->expects($this->never())
        ->method('getDefinitions');
      // 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));
  }

}

/**
 * A class using the DiscoveryCachedTrait for mocking purposes.
 */
class DiscoveryCachedTraitMockableClass {
  use DiscoveryCachedTrait;
  public function getDefinitions() : array {
    return [];
  }

}

Classes

Title Deprecated Summary
DiscoveryCachedTraitMockableClass A class using the DiscoveryCachedTrait for mocking purposes.
DiscoveryCachedTraitTest Tests Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait.

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