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;
class DevelHookCommandTest extends KernelTestBase {
protected static $modules = [
'devel',
'system',
'user',
];
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'));
}
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()))));
}
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()));
}
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());
}
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());
}
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',
]);
}
public function testDrushCompatibility() : void {
if (!trait_exists(AutowireTrait::class)) {
$this->markTestSkipped('Drush is not installed.');
}
$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()));
}
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;
}
protected function command() : DevelHookCommand {
return $this->container
->get(DevelHookCommand::class);
}
}
Classes