Newer
Older

Ted Bowman
committed
<?php
declare(strict_types = 1);

Ted Bowman
committed
namespace Drupal\Tests\package_manager\Kernel;

Ted Bowman
committed
use Drupal\fixture_manipulator\ActiveFixtureManipulator;

Kunal Sachdev
committed
use Drupal\package_manager\ComposerUtility;

Ted Bowman
committed
use Symfony\Component\Process\Process;

Kunal Sachdev
committed

Ted Bowman
committed
/**
* Test that the 'fake-site' fixture is a valid starting point.
*
* @group package_manager

Yash Rode
committed
* @internal

Ted Bowman
committed
*/
class FakeSiteFixtureTest extends PackageManagerKernelTestBase {
/**
* Tests the complete stage life cycle using the 'fake-site' fixture.
*/
public function testLifeCycle(): void {
$this->assertStatusCheckResults([]);
$this->assertResults([]);
// Ensure there are no validation errors after the stage lifecycle has been
// completed.
$this->assertStatusCheckResults([]);
}

Kunal Sachdev
committed
/**
* Tests calls to ComposerUtility class methods.
*/
public function testCallToComposerUtilityMethods(): void {
$active_dir = $this->container->get('package_manager.path_locator')->getProjectRoot();
$composer_utility = ComposerUtility::createForDirectory($active_dir);
// Although the fake-site fixture does not contain any Composer packages or
// Drupal projects that would be returned from these methods calling them
// and asserting that they return NULL proves there are not any missing
// metadata in the fixture files that would cause these methods to throw an
// exception.
$this->assertNull($composer_utility->getProjectForPackage('any_random_name'));
$this->assertNull($composer_utility->getPackageForProject('drupal/any_random_name'));
}
/**
* Tests if `modifyPackage` can be called on all packages in the fixture.
*

Ted Bowman
committed
* @see \Drupal\fixture_manipulator\FixtureManipulator::modifyPackage()
*/
public function testCallToModifyPackage(): void {
$stage = $this->createStage();
$installed_packages = $stage->getActiveComposer()->getInstalledPackages();
foreach (self::getExpectedFakeSitePackages() as $package_name) {
$this->assertArrayHasKey($package_name, $installed_packages);
$this->assertSame('9.8.0', $installed_packages[$package_name]->getPrettyVersion());

Ted Bowman
committed
(new ActiveFixtureManipulator())
->modifyPackage($package_name, ['version' => '11.1.0'])
->commitChanges();
}
}
/**
* Tests if `removePackage` can be called on all packages in the fixture.
*

Ted Bowman
committed
* @covers \Drupal\fixture_manipulator\FixtureManipulator::removePackage()
*/
public function testCallToRemovePackage(): void {
$expected_packages = self::getExpectedFakeSitePackages();
$stage = $this->createStage();
$actual_packages = array_keys($stage->getActiveComposer()->getInstalledPackages());
sort($actual_packages);
$this->assertSame($expected_packages, $actual_packages);
foreach (self::getExpectedFakeSitePackages() as $package_name) {

Ted Bowman
committed
(new ActiveFixtureManipulator())
->removePackage($package_name)
->commitChanges();
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
}
}
/**
* Check which packages are installed in each file.
*/
public function testExpectedPackages(): void {
$expected_packages = $this->getExpectedFakeSitePackages();
$active_dir = $this->container->get('package_manager.path_locator')->getProjectRoot();
$stage = $this->createStage();
$original_installed_php = $stage->getActiveComposer()->getInstalledPackages();
$installed_php_packages = array_keys($original_installed_php);
sort($installed_php_packages);
$installed_json = json_decode(file_get_contents($active_dir . '/vendor/composer/installed.json'), TRUE, 512, JSON_THROW_ON_ERROR);
$installed_json_packages = [];
foreach ($installed_json['packages'] as $package) {
$installed_json_packages[] = $package['name'];
}
sort($installed_json_packages);
$this->assertSame($expected_packages, $installed_json_packages);
// Assert same packages are present in both installed.json and installed.php.
$this->assertSame($installed_json_packages, $installed_php_packages);
}
/**
* Gets the expected packages in the `fake_site` fixture.
*
* @return string[]
* The package names.
*/
private static function getExpectedFakeSitePackages(): array {
$packages = [
'drupal/core',
'drupal/core-recommended',
'drupal/core-dev',
];
sort($packages);
return $packages;
}

Ted Bowman
committed
/**
* Tests that Composer show command can be used on the fixture.
*/
public function testComposerShow(): void {
$process = new Process(['composer', 'show', '--format=json'], $this->container->get('package_manager.path_locator')->getProjectRoot());
$process->run();
if ($error = $process->getErrorOutput()) {
$this->fail('Process error: ' . $error);
}
$output = json_decode($process->getOutput(), TRUE);
$package_names = array_map(fn (array $package) => $package['name'], $output['installed']);
$this->assertTrue(asort($package_names));
$this->assertSame(['drupal/core', 'drupal/core-dev', 'drupal/core-recommended'], $package_names);
}