class ImplementationListTest

Tests ImplementationList.

Attributes

#[Group('Hook')] #[CoversClass(ImplementationList::class)]

Hierarchy

Expanded class hierarchy of ImplementationListTest

File

core/tests/Drupal/Tests/Core/Hook/ImplementationListTest.php, line 15

Namespace

Drupal\Tests\Core\Hook
View source
class ImplementationListTest extends UnitTestCase {
  
  /**
   * Log for include files.
   *
   * @var list<string>
   */
  public static array $log = [];
  
  /**
   * Tests public methods on a common instance.
   *
   * This is easier than separate test methods.
   */
  public function testPublicMethods() : void {
    $listeners = [
      fn() => 'a0',
      fn() => 'b',
      fn() => 'a1',
    ];
    $modules = [
      'module_a',
      'module_b',
      // Repeat the same module.
'module_a',
    ];
    $list = new ImplementationList($listeners, $modules);
    // Test public properties.
    $this->assertSame($listeners, $list->listeners);
    $this->assertSame($modules, $list->modules);
    // Test iterateByModule().
    $i = 0;
    foreach ($list->iterateByModule() as $module => $listener) {
      $this->assertSame($modules[$i], $module);
      $this->assertSame($listeners[$i], $listener);
      ++$i;
    }
    // Test getForModule().
    $this->assertSame([], $list->getForModule('other_module'));
    $this->assertSame([
      $listeners[0],
      $listeners[2],
    ], $list->getForModule('module_a'));
    $this->assertSame([
      $listeners[1],
    ], $list->getForModule('module_b'));
    // Test hasImplementations().
    $this->assertTrue($list->hasImplementations());
    // Test hasImplementationsForModules().
    $this->assertFalse($list->hasImplementationsForModules([
      'other_module',
    ]));
    $this->assertFalse($list->hasImplementationsForModules([]));
    $this->assertTrue($list->hasImplementationsForModules([
      'other_module',
      'module_a',
    ]));
    $this->assertTrue($list->hasImplementationsForModules([
      'module_b',
    ]));
  }
  
  /**
   * Tests public methods on an empty list.
   */
  public function testEmptyList() : void {
    $list = new ImplementationList([], []);
    // Test public properties.
    $this->assertSame([], $list->listeners);
    $this->assertSame([], $list->modules);
    // Test iterateByModule().
    $iterator = $list->iterateByModule();
    $this->assertFalse($iterator->valid());
    // Test hasImplementations().
    $this->assertFalse($list->hasImplementations());
    // Test getForModule().
    $this->assertSame([], $list->getForModule('any_module'));
    // Test hasImplementationsForModules().
    $this->assertFalse($list->hasImplementationsForModules([
      'any_module',
    ]));
    $this->assertFalse($list->hasImplementationsForModules([]));
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
ImplementationListTest::$log public static property Log for include files.
ImplementationListTest::testEmptyList public function Tests public methods on an empty list.
ImplementationListTest::testPublicMethods public function Tests public methods on a common instance.
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.
UnitTestCase::$root protected property The app root.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 365
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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