Skip to content

Commit 01e1fff

Browse files
committed
fix(module): restore critical CLI options
1 parent a1df607 commit 01e1fff

File tree

2 files changed

+110
-16
lines changed

2 files changed

+110
-16
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Commands\Module;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Console\Application;
9+
use Symfony\Component\Console\Tester\CommandTester;
10+
use Wirecli\Commands\Module\ModuleEnableCommand;
11+
12+
/**
13+
* Class ModuleEnableCommandTest
14+
*
15+
* @package Tests\Commands\Module
16+
*/
17+
class ModuleEnableCommandTest extends TestCase
18+
{
19+
/**
20+
* @var Application
21+
*/
22+
private $application;
23+
24+
/**
25+
* @var CommandTester
26+
*/
27+
private $commandTester;
28+
29+
protected function setUp(): void
30+
{
31+
$this->application = new Application();
32+
$this->application->add(new ModuleEnableCommand());
33+
34+
$command = $this->application->find('module:enable');
35+
$this->commandTester = new CommandTester($command);
36+
}
37+
38+
public function testConfigureSetsCorrectNameAndDescription()
39+
{
40+
$command = $this->application->find('module:enable');
41+
42+
$this->assertEquals('module:enable', $command->getName());
43+
$this->assertEquals('Enables provided module(s)', $command->getDescription());
44+
}
45+
46+
public function testConfigureHasRequiredArguments()
47+
{
48+
$command = $this->application->find('module:enable');
49+
$definition = $command->getDefinition();
50+
51+
$this->assertTrue($definition->hasArgument('modules'));
52+
$this->assertTrue($definition->hasOption('github'));
53+
$this->assertTrue($definition->hasOption('branch'));
54+
}
55+
56+
public function testGithubOptionIsOptional()
57+
{
58+
$command = $this->application->find('module:enable');
59+
$definition = $command->getDefinition();
60+
$githubOption = $definition->getOption('github');
61+
62+
$this->assertFalse($githubOption->isValueRequired());
63+
$this->assertTrue($githubOption->isValueOptional());
64+
}
65+
66+
public function testBranchOptionIsOptional()
67+
{
68+
$command = $this->application->find('module:enable');
69+
$definition = $command->getDefinition();
70+
$branchOption = $definition->getOption('branch');
71+
72+
$this->assertFalse($branchOption->isValueRequired());
73+
$this->assertTrue($branchOption->isValueOptional());
74+
}
75+
76+
public function testCommandHandlesCommaSeparatedModules()
77+
{
78+
$command = $this->application->find('module:enable');
79+
$definition = $command->getDefinition();
80+
$modulesArgument = $definition->getArgument('modules');
81+
82+
$this->assertEquals('modules', $modulesArgument->getName());
83+
$this->assertStringContainsString('comma separated', $modulesArgument->getDescription());
84+
}
85+
}

src/Commands/Module/ModuleEnableCommand.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,28 @@ protected function execute(InputInterface $input, OutputInterface $output) {
4242
->setInput($input)
4343
->writeBlockCommand($this->getName());
4444

45+
// Refresh modules to discover any manually copied modules
46+
\ProcessWire\wire('modules')->refresh();
47+
4548
$modules = $this->tools->ask($input->getArgument('modules'), 'Modules', null, false, null, 'required');
4649
if (!is_array($modules)) $modules = explode(',', $modules);
4750

4851
foreach ($modules as $module) {
52+
$module = trim($module);
53+
4954
// if module doesn't exist, download the module
5055
if (!$this->checkIfModuleExists($module)) {
5156
$this->tools->writeComment("Cannot find '{$module}' locally, trying to download...");
5257
$this->tools->nl();
53-
$this->passOnToModuleDownloadCommand($module, $output, $input);
58+
59+
try {
60+
$this->passOnToModuleDownloadCommand($module, $output, $input);
61+
// Refresh modules after download to make sure the newly downloaded module is discoverable
62+
\ProcessWire\wire('modules')->refresh();
63+
} catch (\Exception $e) {
64+
$this->tools->writeError("Failed to download module '{$module}': " . $e->getMessage());
65+
continue;
66+
}
5467
}
5568

5669
// check whether module is already installed
@@ -59,35 +72,31 @@ protected function execute(InputInterface $input, OutputInterface $output) {
5972
continue;
6073
}
6174

62-
// install module
75+
// install module with proper CLI options
6376
$options = array(
6477
'noPermissionCheck' => true,
6578
'noInit' => true
6679
);
6780

68-
if (\ProcessWire\wire('modules')->getInstall($module, $options)) {
69-
$this->tools->writeSuccess(" Module `{$module}` installed successfully.");
70-
} else {
71-
$this->tools->writeError(" Module `{$module}` does not exist.");
81+
try {
82+
if (\ProcessWire\wire('modules')->getInstall($module, $options)) {
83+
$this->tools->writeSuccess(" Module `{$module}` installed successfully.");
84+
} else {
85+
$this->tools->writeError(" Module `{$module}` installation failed.");
86+
}
87+
} catch (\Exception $e) {
88+
$this->tools->writeError(" Module `{$module}` does not exist or installation failed: " . $e->getMessage());
7289
}
7390
}
7491

7592
return static::SUCCESS;
7693
}
7794

78-
private function checkIfModuleExistsLocally($module, $output, $input) {
79-
if (!$this->checkIfModuleExists($module)) {
80-
$output->writeln("<comment>Cannot find '{$module}' locally, trying to download...</comment>");
81-
$this->passOnToModuleDownloadCommand($module, $output, $input);
82-
}
83-
84-
}
85-
8695
private function passOnToModuleDownloadCommand($module, $output, $input) {
87-
$command = $this->getApplication()->find('mod:download');
96+
$command = $this->getApplication()->find('module:download');
8897

8998
$arguments = array(
90-
'command' => 'mod:download',
99+
'command' => 'module:download',
91100
'modules' => $module,
92101
'--github' => $input->getOption('github'),
93102
'--branch' => $input->getOption('branch')

0 commit comments

Comments
 (0)