DevelHookCommandTest.php

Namespace

Drupal\Tests\devel\Kernel

File

tests/src/Kernel/DevelHookCommandTest.php

View source
<?php

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

use Drupal\devel\Command\DevelHookCommand;
use Drupal\devel\Drush\Commands\DevelHookCommand as DrushDevelHookCommand;
use Drupal\devel\Hook\DevelHooks;
use Drupal\KernelTests\KernelTestBase;
use Drush\Commands\AutowireTrait;
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:hook command.
 */
class DevelHookCommandTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'devel',
    'system',
    'user',
  ];
  
  /**
   * The command is discovered, so that dr can load it.
   */
  public function testDiscovery() : void {
    $this->assertTrue($this->container
      ->get('console.command_loader')
      ->has(DevelHookCommand::NAME));
    $this->assertTrue($this->container
      ->get('console.command_loader')
      ->has('fn-hook'));
  }
  
  /**
   * Without an implementation, all of them are listed.
   */
  public function testList() : void {
    $tester = $this->runCommand([
      'hook' => 'help',
    ]);
    $hooks = (new \ReflectionClass(DevelHooks::class))->getFileName();
    $this->assertStringContainsString($hooks . ':', $tester->getDisplay());
    $this->assertGreaterThan(1, count(explode("\n", trim($tester->getDisplay()))));
  }
  
  /**
   * An implementation is found by module name or by method name.
   */
  public function testImplementation() : void {
    $method = new \ReflectionMethod(DevelHooks::class, 'help');
    $expected = $method->getFileName() . ':' . $method->getStartLine();
    $tester = $this->runCommand([
      'hook' => 'help',
      'implementation' => 'devel',
    ]);
    $this->assertSame($expected, trim($tester->getDisplay()));
    $tester = $this->runCommand([
      'hook' => 'help',
      'implementation' => DevelHooks::class . '::help',
    ]);
    $this->assertSame($expected, trim($tester->getDisplay()));
  }
  
  /**
   * A procedural hook in a .install file is found.
   */
  public function testInstallFile() : void {
    $this->container
      ->get('update.update_hook_registry')
      ->setInstalledVersion('system', 8000);
    $tester = $this->runCommand([
      'hook' => 'update_last_removed',
      'implementation' => 'system',
    ]);
    $this->assertStringContainsString('system.install:', $tester->getDisplay());
  }
  
  /**
   * The prompt offers the implementations.
   */
  public function testInteract() : void {
    $tester = new CommandTester($this->command());
    $tester->setInputs([
      DevelHooks::class . '::help',
    ]);
    $tester->execute([
      'hook' => 'help',
    ], [
      'interactive' => TRUE,
    ]);
    $this->assertSame(Command::SUCCESS, $tester->getStatusCode(), $tester->getDisplay());
    $this->assertStringContainsString('Select the hook implementation', $tester->getDisplay());
    $this->assertStringContainsString((new \ReflectionClass(DevelHooks::class))->getFileName() . ':', $tester->getDisplay());
  }
  
  /**
   * Unknown hooks and implementations are errors.
   */
  public function testErrors() : void {
    try {
      $this->runCommand([
        'hook' => 'help',
        'implementation' => 'no_such_module',
      ]);
      $this->fail('Expected an exception.');
    } catch (\InvalidArgumentException $e) {
      $this->assertStringContainsString('does not implement hook_help()', $e->getMessage());
    }
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('No implementations.');
    $this->runCommand([
      'hook' => 'no_such_hook',
    ]);
  }
  
  /**
   * Drush 13 and 14 load the subclass in the old namespace.
   */
  public function testDrushCompatibility() : void {
    if (!trait_exists(AutowireTrait::class)) {
      $this->markTestSkipped('Drush is not installed.');
    }
    // This is how Drush builds a command from an invokable class.
    $command = new Command(NULL, DrushDevelHookCommand::create($this->container));
    $this->assertSame(DevelHookCommand::NAME, $command->getName());
    $this->assertContains('fn-hook', $command->getAliases());
    $tester = new CommandTester($command);
    $tester->execute([
      'hook' => 'help',
      'implementation' => 'devel',
    ], [
      'interactive' => FALSE,
    ]);
    $method = new \ReflectionMethod(DevelHooks::class, 'help');
    $this->assertSame($method->getFileName() . ':' . $method->getStartLine(), trim($tester->getDisplay()));
  }
  
  /**
   * Runs the command non-interactively and asserts that it succeeded.
   */
  protected function runCommand(array $input) : CommandTester {
    $tester = new CommandTester($this->command());
    $code = $tester->execute($input, [
      'interactive' => FALSE,
    ]);
    $this->assertSame(Command::SUCCESS, $code, $tester->getDisplay());
    return $tester;
  }
  
  /**
   * Builds the command the way the container does.
   */
  protected function command() : DevelHookCommand {
    return $this->container
      ->get(DevelHookCommand::class);
  }

}

Classes

Title Deprecated Summary
DevelHookCommandTest Tests the devel:hook command.