DevelReinstallCommandTest.php

Namespace

Drupal\Tests\devel\Kernel

File

tests/src/Kernel/DevelReinstallCommandTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\devel\Kernel;

use Drupal\devel\Command\DevelReinstallCommand;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;

/**
 * Tests the devel:reinstall command.
 *
 * Devel itself is not installed here. Its route subscriber reads routes while
 * the router is rebuilt, which a kernel test does not allow.
 */
class DevelReinstallCommandTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
  ];
  
  /**
   * Modules are uninstalled and installed again.
   */
  public function testReinstall() : void {
    // Uninstalling a module deletes its user data.
    $this->installSchema('user', [
      'users_data',
    ]);
    $this->container
      ->get('module_installer')
      ->install([
      'help',
      'syslog',
    ]);
    // This is recorded afresh when a module is installed.
    \Drupal::keyValue('system.schema')->set('help', 1234);
    $tester = $this->runCommand('help, syslog');
    $this->assertSame(Command::SUCCESS, $tester->getStatusCode(), $tester->getDisplay());
    $this->assertStringContainsString('help,syslog reinstalled.', $tester->getDisplay());
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists('help'));
    $this->assertTrue(\Drupal::moduleHandler()->moduleExists('syslog'));
    $this->assertNotSame(1234, \Drupal::keyValue('system.schema')->get('help'));
  }
  
  /**
   * A module that is not installed is an error.
   */
  public function testNotInstalled() : void {
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Not installed: no_such_module');
    $this->runCommand('system,no_such_module');
  }
  
  /**
   * A module that cannot be uninstalled is an error.
   */
  public function testRequired() : void {
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage('Unable to uninstall.');
    $this->runCommand('system');
  }
  
  /**
   * Runs devel:reinstall for a comma-separated list of modules.
   */
  protected function runCommand(string $modules) : CommandTester {
    $command = new DevelReinstallCommand(\Drupal::service('module_installer'), \Drupal::moduleHandler());
    $tester = new CommandTester(new Command(NULL, $command));
    $tester->execute([
      'modules' => $modules,
    ], [
      'interactive' => FALSE,
    ]);
    return $tester;
  }

}

Classes

Title Deprecated Summary
DevelReinstallCommandTest Tests the devel:reinstall command.