You are here

class SortArrayTest in Modules weight 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/Utility/SortArrayTest.php \Drupal\Tests\modules_weight\Unit\Utility\SortArrayTest

Tests the SortArray class methods.

@group modules_weight @coversDefaultClass \Drupal\modules_weight\Utility\SortArray

Hierarchy

Expanded class hierarchy of SortArrayTest

File

tests/src/Unit/Utility/SortArrayTest.php, line 14

Namespace

Drupal\Tests\modules_weight\Unit\Utility
View source
class SortArrayTest extends UnitTestCase {

  /**
   * Tests the array sort with SortArray::sortByWeightAndName().
   *
   * @param int $expected
   *   The expected result from calling the function.
   * @param array $a
   *   The first input array to order through SortArray::sortByWeightAndName().
   * @param array $b
   *   The second input array to order through SortArray::sortByWeightAndName().
   *
   * @covers ::sortByWeightAndName
   * @dataProvider providerSortByWeightAndName
   */
  public function testSortByWeightAndName($expected, array $a, array $b) {
    $result = SortArray::sortByWeightAndName($a, $b);

    // Checking that the two values are numerics.
    $this
      ->assertTrue(is_numeric($expected) && is_numeric($result), 'Parameters are numeric.');

    // Checking that both are equals.
    $this
      ->assertTrue($expected < 0 && $result < 0 || $expected > 0 && $result > 0 || $expected === 0 && $result === 0, 'Numbers are either both negative, both positive or both zero.');
  }

  /**
   * Data provider for testSortByWeightAndName().
   *
   * @return array
   *   An array of arrays, each containing:
   *   - 'expected' - Expected return from SortArray::sortByWeightAndName().
   *   - 'a' - The first input array.
   *   - 'b' - The second input array.
   *
   * @see testSortByWeightAndName()
   */
  public function providerSortByWeightAndName() {
    $tests['same weight different name 1'] = [
      -1,
      [
        'name' => 'Admin Toolbar',
        'weight' => 1,
      ],
      [
        'name' => 'Asana',
        'weight' => 1,
      ],
    ];
    $tests['same weight different name 2'] = [
      1,
      [
        'name' => 'Asana',
        'weight' => 1,
      ],
      [
        'name' => 'Admin Toolbar',
        'weight' => 1,
      ],
    ];
    $tests['different weight 1'] = [
      -1,
      [
        'name' => 'Asana',
        'weight' => -15,
      ],
      [
        'name' => 'Admin Toolbar',
        'weight' => 8,
      ],
    ];
    $tests['different weight 2'] = [
      1,
      [
        'name' => 'Asana',
        'weight' => 51,
      ],
      [
        'name' => 'Admin Toolbar',
        'weight' => -10,
      ],
    ];
    $tests['with no weight 1'] = [
      -1,
      [
        'name' => 'Asana',
      ],
      [
        'name' => 'Admin Toolbar',
        'weight' => 8,
      ],
    ];
    $tests['with no weight 2'] = [
      1,
      [
        'name' => 'Asana',
        'weight' => 51,
      ],
      [
        'name' => 'Admin Toolbar',
      ],
    ];
    return $tests;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
SortArrayTest::providerSortByWeightAndName public function Data provider for testSortByWeightAndName().
SortArrayTest::testSortByWeightAndName public function Tests the array sort with SortArray::sortByWeightAndName().
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