Skip to content

Commit

Permalink
Fix usage of deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-teck committed Mar 5, 2024
1 parent b2bd619 commit 6934068
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function create(ContainerInterface $container): self {
new TwigRenderer(new TwigEnvironment($template_loader)),
new ListPrinter(),
new TablePrinter(),
new ModuleInfo($container->get('module_handler')),
new ModuleInfo($container->get('module_handler'), $container->get('extension.list.module')),
new ThemeInfo($container->get('theme_handler')),
new ServiceInfo($container),
new HookInfo($container->get('module_handler')),
Expand Down
9 changes: 7 additions & 2 deletions src/Helper/Drupal/ModuleInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DrupalCodeGenerator\Helper\Drupal;

use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Filesystem\Path;
Expand All @@ -17,7 +18,10 @@ final class ModuleInfo extends Helper implements ExtensionInfoInterface {
/**
* Constructs the object.
*/
public function __construct(private readonly ModuleHandlerInterface $moduleHandler) {}
public function __construct(
private readonly ModuleHandlerInterface $moduleHandler,
private readonly ModuleExtensionList $moduleList,
) {}

/**
* {@inheritdoc}
Expand All @@ -34,7 +38,8 @@ public function getName(): string {
public function getExtensions(): array {
$modules = [];
foreach ($this->moduleHandler->getModuleList() as $machine_name => $module) {
$modules[$machine_name] = $this->moduleHandler->getName($machine_name);
/** @psalm-suppress InternalMethod */
$modules[$machine_name] = $this->moduleList->getName($machine_name);
}
return $modules;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'revision_user',
'revision_log',
'status',
'uid',
'info',
'changed',
'reusable',
Expand Down
26 changes: 14 additions & 12 deletions tests/functional/Helper/Drupal/ModuleInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ final class ModuleInfoTest extends FunctionalTestBase {
* Test callback.
*/
public function testGetName(): void {
$module_info = new ModuleInfo(self::bootstrap()->get('module_handler'));
self::assertSame('module_info', $module_info->getName());
self::assertSame('module_info', self::getModuleInfo()->getName());
}

/**
* Test callback.
*/
public function testGetModules(): void {
$module_info = new ModuleInfo(self::bootstrap()->get('module_handler'));

$modules = $module_info->getExtensions();
$modules = self::getModuleInfo()->getExtensions();
// Full list of modules is rather long and may vary depending on`
// environment.
self::assertSame('Database Logging', $modules['dblog']);
Expand All @@ -39,8 +36,7 @@ public function testGetModules(): void {
* Test callback.
*/
public function testGetDestination(): void {
$module_info = new ModuleInfo(self::bootstrap()->get('module_handler'));

$module_info = self::getModuleInfo();
self::assertDestination('modules', $module_info->getDestination('node', TRUE));
self::assertDestination('core/modules/node', $module_info->getDestination('node', FALSE));
self::assertDestination('modules', $module_info->getDestination('foo', TRUE));
Expand All @@ -51,8 +47,7 @@ public function testGetDestination(): void {
* Test callback.
*/
public function testGetModuleName(): void {
$module_info = new ModuleInfo(self::bootstrap()->get('module_handler'));

$module_info = self::getModuleInfo();
self::assertSame('Database Logging', $module_info->getExtensionName('dblog'));
self::assertNull($module_info->getExtensionName('unknown_module'));
}
Expand All @@ -61,8 +56,7 @@ public function testGetModuleName(): void {
* Test callback.
*/
public function testGetModuleMachineName(): void {
$module_info = new ModuleInfo(self::bootstrap()->get('module_handler'));

$module_info = self::getModuleInfo();
self::assertSame('dblog', $module_info->getExtensionMachineName('Database Logging'));
self::assertNull($module_info->getExtensionName('Unknown Module'));
}
Expand All @@ -71,7 +65,7 @@ public function testGetModuleMachineName(): void {
* Test callback.
*/
public function testGetExtensionFromPath(): void {
$module_info = new ModuleInfo(self::bootstrap()->get('module_handler'));
$module_info = self::getModuleInfo();

$module = $module_info->getExtensionFromPath(\DRUPAL_ROOT . '/core/modules/node');
self::assertSame('node', $module->getName());
Expand All @@ -93,4 +87,12 @@ private static function assertDestination(string $expected, string $actual): voi
self::assertSame($expected, Utils::removePrefix($actual, \DRUPAL_ROOT . '/'));
}

/**
* {@selfdoc}
*/
private static function getModuleInfo(): ModuleInfo {
$container = self::bootstrap();
return new ModuleInfo($container->get('module_handler'), $container->get('extension.list.module'));
}

}
2 changes: 1 addition & 1 deletion tests/functional/InputOutput/InterviewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ private function createInterviewer(
$definition,
new ServiceInfo($container),
match ($definition->type) {
GeneratorType::MODULE, GeneratorType::MODULE_COMPONENT => new ModuleInfo($container->get('module_handler')),
GeneratorType::MODULE, GeneratorType::MODULE_COMPONENT => new ModuleInfo($container->get('module_handler'), $container->get('extension.list.module')),
GeneratorType::THEME, GeneratorType::THEME_COMPONENT => new ThemeInfo($container->get('theme_handler')),
default => new NullExtensionInfo(),
},
Expand Down
14 changes: 10 additions & 4 deletions tests/functional/Validator/ExtensionExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ final class ExtensionExistsTest extends FunctionalTestBase {
* Test callback.
*/
public function testModuleExists(): void {
$module_handler = self::bootstrap()->get('module_handler');
$validator = new ExtensionExists(new ModuleInfo($module_handler), GeneratorType::MODULE_COMPONENT);
$module_info = new ModuleInfo(
self::bootstrap()->get('module_handler'),
self::bootstrap()->get('extension.list.module'),
);
$validator = new ExtensionExists($module_info, GeneratorType::MODULE_COMPONENT);
self::assertSame('node', $validator('node'));
self::assertSame('filter', $validator('filter'));
self::expectExceptionObject(new \UnexpectedValueException('Module "ban" does not exists.'));
Expand All @@ -43,8 +46,11 @@ public function testThemeExists(): void {
* Test callback.
*/
public function testExtensionExists(): void {
$module_handler = self::bootstrap()->get('module_handler');
$validator = new ExtensionExists(new ModuleInfo($module_handler), GeneratorType::OTHER);
$module_info = new ModuleInfo(
self::bootstrap()->get('module_handler'),
self::bootstrap()->get('extension.list.module'),
);
$validator = new ExtensionExists($module_info, GeneratorType::OTHER);
self::assertSame('system', $validator('system'));
self::assertSame('node', $validator('node'));
self::expectExceptionObject(new \UnexpectedValueException('Extension "forum" does not exists.'));
Expand Down

0 comments on commit 6934068

Please sign in to comment.