You are here

class ModulesWeightTest in Modules weight 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/ModulesWeightTest.php \Drupal\Tests\modules_weight\Unit\ModulesWeightTest

Tests the ModulesWeight class methods.

@group modules_weight @coversDefaultClass \Drupal\modules_weight\ModulesWeight

Hierarchy

Expanded class hierarchy of ModulesWeightTest

File

tests/src/Unit/ModulesWeightTest.php, line 14

Namespace

Drupal\Tests\modules_weight\Unit
View source
class ModulesWeightTest extends UnitTestCase {

  /**
   * Tests the modules list with ModulesWeight::getModulesList().
   *
   * @param string $expected
   *   The expected result from calling the function.
   * @param bool $show_core_modules
   *   Force to show the core modules.
   *
   * @covers ::getModulesList
   * @dataProvider providerGetModulesList
   */
  public function testGetModulesList($expected, $show_core_modules) {

    // ImmutableConfig mock.
    $config = $this
      ->createMock('Drupal\\Core\\Config\\ImmutableConfig');
    $weight = [
      'standard' => 1000,
      'admin_toolbar' => 3,
      'modules_weight' => -5,
      'views' => 0,
    ];

    // ImmutableConfig::get mock.
    $config
      ->expects($this
      ->any())
      ->method('get')
      ->with('module')
      ->willReturn($weight);

    // Config factory mock.
    $config_factory = $this
      ->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');

    // ConfigFactoryInterface::get mock.
    $config_factory
      ->expects($this
      ->any())
      ->method('get')
      ->with('core.extension')
      ->willReturn($config);

    // Creating the object.
    $modules_weight = new ModulesWeight($config_factory);

    // Testing the function.
    $this
      ->assertSame($expected, $modules_weight
      ->getModulesList($show_core_modules));
  }

  /**
   * Data provider for testGetModulesList().
   *
   * @return array
   *   An array of arrays, each containing:
   *   - 'expected' - Expected return from getModulesList().
   *   - 'show_core_modules' - Force to show the core modules.
   *
   * @see testGetModulesList()
   */
  public function providerGetModulesList() {

    // Show core modules.
    $show_core_modules['modules_weight'] = [
      'name' => 'Modules Weight',
      'description' => 'Allows to change the modules execution order.',
      'weight' => -5,
      'package' => 'Development',
    ];
    $show_core_modules['views'] = [
      'name' => 'Views',
      'description' => 'Create customized lists and queries from your database.',
      'weight' => 0,
      'package' => 'Core',
    ];
    $show_core_modules['admin_toolbar'] = [
      'name' => 'Admin Toolbar',
      'description' => 'Provides a drop-down menu interface to the core Drupal Toolbar.',
      'weight' => 3,
      'package' => 'Administration',
    ];

    // Not show core modules.
    $not_show_core_modules['modules_weight'] = [
      'name' => 'Modules Weight',
      'description' => 'Allows to change the modules execution order.',
      'weight' => -5,
      'package' => 'Development',
    ];
    $not_show_core_modules['admin_toolbar'] = [
      'name' => 'Admin Toolbar',
      'description' => 'Provides a drop-down menu interface to the core Drupal Toolbar.',
      'weight' => 3,
      'package' => 'Administration',
    ];
    $tests['show core modules'] = [
      $show_core_modules,
      TRUE,
    ];
    $tests['not show core modules'] = [
      $not_show_core_modules,
      FALSE,
    ];
    return $tests;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModulesWeightTest::providerGetModulesList public function Data provider for testGetModulesList().
ModulesWeightTest::testGetModulesList public function Tests the modules list with ModulesWeight::getModulesList().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340