class InstalledPackagesListTest

Same name in this branch
  1. 11.x core/modules/package_manager/tests/src/Kernel/InstalledPackagesListTest.php \Drupal\Tests\package_manager\Kernel\InstalledPackagesListTest

@coversDefaultClass \Drupal\package_manager\InstalledPackagesList

@group package_manager

Hierarchy

Expanded class hierarchy of InstalledPackagesListTest

File

core/modules/package_manager/tests/src/Unit/InstalledPackagesListTest.php, line 16

Namespace

Drupal\Tests\package_manager\Unit
View source
class InstalledPackagesListTest extends UnitTestCase {
  
  /**
   * @covers ::offsetSet
   * @covers ::offsetUnset
   * @covers ::append
   * @covers ::exchangeArray
   *
   * @testWith ["offsetSet", ["new", "thing"]]
   *   ["offsetUnset", ["existing"]]
   *   ["append", ["new thing"]]
   *   ["exchangeArray", [{"evil": "twin"}]]
   */
  public function testImmutability(string $method, array $arguments) : void {
    $list = new InstalledPackagesList([
      'existing' => 'thing',
    ]);
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Installed package lists cannot be modified.');
    $list->{$method}(...$arguments);
  }
  
  /**
   * @covers ::getPackagesNotIn
   * @covers ::getPackagesWithDifferentVersionsIn
   */
  public function testPackageComparison() : void {
    $active = new InstalledPackagesList([
      'drupal/existing' => InstalledPackage::createFromArray([
        'name' => 'drupal/existing',
        'version' => '1.0.0',
        'path' => __DIR__,
        'type' => 'drupal-module',
      ]),
      'drupal/updated' => InstalledPackage::createFromArray([
        'name' => 'drupal/updated',
        'version' => '1.0.0',
        'path' => __DIR__,
        'type' => 'drupal-module',
      ]),
      'drupal/removed' => InstalledPackage::createFromArray([
        'name' => 'drupal/removed',
        'version' => '1.0.0',
        'path' => __DIR__,
        'type' => 'drupal-module',
      ]),
    ]);
    $staged = new InstalledPackagesList([
      'drupal/existing' => InstalledPackage::createFromArray([
        'name' => 'drupal/existing',
        'version' => '1.0.0',
        'path' => __DIR__,
        'type' => 'drupal-module',
      ]),
      'drupal/updated' => InstalledPackage::createFromArray([
        'name' => 'drupal/updated',
        'version' => '1.1.0',
        'path' => __DIR__,
        'type' => 'drupal-module',
      ]),
      'drupal/added' => InstalledPackage::createFromArray([
        'name' => 'drupal/added',
        'version' => '1.0.0',
        'path' => __DIR__,
        'type' => 'drupal-module',
      ]),
    ]);
    $added = $staged->getPackagesNotIn($active)
      ->getArrayCopy();
    $this->assertSame([
      'drupal/added',
    ], array_keys($added));
    $removed = $active->getPackagesNotIn($staged)
      ->getArrayCopy();
    $this->assertSame([
      'drupal/removed',
    ], array_keys($removed));
    $updated = $active->getPackagesWithDifferentVersionsIn($staged)
      ->getArrayCopy();
    $this->assertSame([
      'drupal/updated',
    ], array_keys($updated));
  }
  
  /**
   * @covers ::getCorePackages
   */
  public function testCorePackages() : void {
    $data = [
      'drupal/core' => InstalledPackage::createFromArray([
        'name' => 'drupal/core',
        'version' => \Drupal::VERSION,
        'type' => 'drupal-core',
        'path' => __DIR__,
      ]),
      'drupal/core-dev' => InstalledPackage::createFromArray([
        'name' => 'drupal/core-dev',
        'version' => \Drupal::VERSION,
        'type' => 'metapackage',
        'path' => NULL,
      ]),
      'drupal/core-dev-pinned' => InstalledPackage::createFromArray([
        'name' => 'drupal/core-dev-pinned',
        'version' => \Drupal::VERSION,
        'type' => 'metapackage',
        'path' => NULL,
      ]),
      'drupal/core-composer-scaffold' => InstalledPackage::createFromArray([
        'name' => 'drupal/core-composer-scaffold',
        'version' => \Drupal::VERSION,
        'type' => 'composer-plugin',
        'path' => __DIR__,
      ]),
      'drupal/core-project-message' => [
        'name' => 'drupal/core-project-message',
        'version' => \Drupal::VERSION,
        'type' => 'composer-plugin',
        'path' => __DIR__,
      ],
      'drupal/core-vendor-hardening' => InstalledPackage::createFromArray([
        'name' => 'drupal/core-vendor-hardening',
        'version' => \Drupal::VERSION,
        'type' => 'composer-plugin',
        'path' => __DIR__,
      ]),
      'drupal/not-core' => InstalledPackage::createFromArray([
        'name' => 'drupal/not-core',
        'version' => '1.0.0',
        'type' => 'drupal-module',
        'path' => __DIR__,
      ]),
    ];
    $list = new InstalledPackagesList($data);
    $this->assertArrayNotHasKey('drupal/not-core', $list->getCorePackages());
    // Tests that we don't get core packages intended for development when
    // include_dev is set to FALSE.
    $core_packages_no_dev = $list->getCorePackages(FALSE);
    $this->assertArrayNotHasKey('drupal/core-dev', $core_packages_no_dev);
    $this->assertArrayNotHasKey('drupal/core-dev-pinned', $core_packages_no_dev);
    // We still get other packages as intended.
    $this->assertArrayHasKey('drupal/core', $core_packages_no_dev);
    // If drupal/core-recommended is in the list, it should supersede
    // drupal/core.
    $this->assertArrayHasKey('drupal/core', $list->getCorePackages());
    $data['drupal/core-recommended'] = InstalledPackage::createFromArray([
      'name' => 'drupal/core-recommended',
      'version' => \Drupal::VERSION,
      'type' => 'metapackage',
      'path' => NULL,
    ]);
    $list = new InstalledPackagesList($data);
    $this->assertArrayNotHasKey('drupal/core', $list->getCorePackages());
  }

}

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.
InstalledPackagesListTest::testCorePackages public function @covers ::getCorePackages[[api-linebreak]]
InstalledPackagesListTest::testImmutability public function @covers ::offsetSet[[api-linebreak]]
@covers ::offsetUnset[[api-linebreak]]
@covers ::append[[api-linebreak]]
@covers ::exchangeArray[[api-linebreak]]
InstalledPackagesListTest::testPackageComparison public function @covers ::getPackagesNotIn[[api-linebreak]]
@covers ::getPackagesWithDifferentVersionsIn[[api-linebreak]]
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 375
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.