Skip to content

Commit 699ca21

Browse files
oshmyheliukBaDos
authored andcommitted
MAGECLOUD-2514: Add List of Modules which were Enabled by module:refresh Command to cloud.log (#548)
1 parent fba7248 commit 699ca21

File tree

7 files changed

+133
-72
lines changed

7 files changed

+133
-72
lines changed

src/App/Container.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\MagentoCloud\Command\Build;
1111
use Magento\MagentoCloud\Command\CronKill;
1212
use Magento\MagentoCloud\Command\Deploy;
13+
use Magento\MagentoCloud\Command\ModuleRefresh;
1314
use Magento\MagentoCloud\Command\PostDeploy;
1415
use Magento\MagentoCloud\Config\Database\ConfigInterface;
1516
use Magento\MagentoCloud\Config\Database\MergedConfig;
@@ -390,6 +391,9 @@ function () {
390391
$this->container->when(CronKill::class)
391392
->needs(ProcessInterface::class)
392393
->give(DeployProcess\CronProcessKill::class);
394+
$this->container->when(ModuleRefresh::class)
395+
->needs(ProcessInterface::class)
396+
->give(BuildProcess\RefreshModules::class);
393397

394398
$this->container->singleton(ConfigInterface::class, MergedConfig::class);
395399
}

src/Command/ModuleRefresh.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66
namespace Magento\MagentoCloud\Command;
77

8-
use Magento\MagentoCloud\Config\Module;
8+
use Magento\MagentoCloud\Process\ProcessException;
9+
use Magento\MagentoCloud\Process\ProcessInterface;
910
use Psr\Log\LoggerInterface;
1011
use Symfony\Component\Console\Command\Command;
1112
use Symfony\Component\Console\Input\InputInterface;
@@ -19,23 +20,23 @@ class ModuleRefresh extends Command
1920
const NAME = 'module:refresh';
2021

2122
/**
22-
* @var LoggerInterface
23+
* @var ProcessInterface
2324
*/
24-
private $logger;
25+
private $process;
2526

2627
/**
27-
* @var Module
28+
* @var LoggerInterface
2829
*/
29-
private $config;
30+
private $logger;
3031

3132
/**
33+
* @param ProcessInterface $process
3234
* @param LoggerInterface $logger
33-
* @param Module $config
3435
*/
35-
public function __construct(LoggerInterface $logger, Module $config)
36+
public function __construct(ProcessInterface $process, LoggerInterface $logger)
3637
{
38+
$this->process = $process;
3739
$this->logger = $logger;
38-
$this->config = $config;
3940

4041
parent::__construct();
4142
}
@@ -52,15 +53,13 @@ protected function configure()
5253
/**
5354
* {@inheritdoc}
5455
*
55-
* @throws \Exception
56+
* @throws ProcessException
5657
*/
5758
public function execute(InputInterface $input, OutputInterface $output)
5859
{
5960
try {
60-
$this->logger->info('Refreshing modules started.');
61-
$this->config->refresh();
62-
$this->logger->info('Refreshing modules completed.');
63-
} catch (\Exception $exception) {
61+
$this->process->execute();
62+
} catch (ProcessException $exception) {
6463
$this->logger->critical($exception->getMessage());
6564

6665
throw $exception;

src/Config/Module.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,25 @@ public function __construct(ConfigInterface $config, ShellFactory $shellFactory)
3737

3838
/**
3939
* Reconciling installed modules with shared config.
40+
* Returns list of new enabled modules or an empty array if no modules were enabled.
4041
*
4142
* @throws ShellException
4243
* @throws FileSystemException
4344
*/
44-
public function refresh()
45+
public function refresh(): array
4546
{
4647
$moduleConfig = (array)$this->config->get('modules');
4748

48-
if (!$moduleConfig) {
49-
$this->magentoShell->execute('module:enable --all');
50-
$this->config->reset();
49+
$this->magentoShell->execute('module:enable --all');
50+
51+
$this->config->reset();
5152

52-
return;
53+
$updatedModuleConfig = (array)$this->config->get('modules');
54+
55+
if ($moduleConfig) {
56+
$this->config->update(['modules' => $moduleConfig]);
5357
}
5458

55-
$this->magentoShell->execute('module:enable --all');
56-
$this->config->update(['modules' => $moduleConfig]);
59+
return array_keys(array_diff_key($updatedModuleConfig, $moduleConfig));
5760
}
5861
}

src/Process/Build/RefreshModules.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Magento\MagentoCloud\Config\Module;
99
use Magento\MagentoCloud\Process\ProcessException;
1010
use Magento\MagentoCloud\Process\ProcessInterface;
11-
use Magento\MagentoCloud\Shell\ShellException;
1211
use Psr\Log\LoggerInterface;
1312

1413
/**
@@ -46,8 +45,13 @@ public function execute()
4645
$this->logger->notice('Reconciling installed modules with shared config.');
4746

4847
try {
49-
$this->config->refresh();
50-
} catch (ShellException $exception) {
48+
$enabledModules = $this->config->refresh();
49+
$this->logger->info(
50+
$enabledModules ?
51+
'The following modules have been enabled:' . PHP_EOL . implode(PHP_EOL, $enabledModules) :
52+
'No modules were changed.'
53+
);
54+
} catch (\Exception $exception) {
5155
throw new ProcessException($exception->getMessage(), $exception->getCode(), $exception);
5256
}
5357
$this->logger->notice('End of reconciling modules.');

src/Test/Unit/Command/ModuleRefreshTest.php

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace Magento\MagentoCloud\Test\Unit\Command;
77

88
use Magento\MagentoCloud\Command\ModuleRefresh;
9-
use Magento\MagentoCloud\Config\Module;
9+
use Magento\MagentoCloud\Process\ProcessException;
10+
use Magento\MagentoCloud\Process\ProcessInterface;
1011
use PHPUnit\Framework\MockObject\MockObject;
1112
use PHPUnit\Framework\TestCase;
1213
use Psr\Log\LoggerInterface;
@@ -23,39 +24,35 @@ class ModuleRefreshTest extends TestCase
2324
private $command;
2425

2526
/**
26-
* @var LoggerInterface|MockObject
27+
* @var ProcessInterface|MockObject
2728
*/
28-
private $loggerMock;
29+
private $processMock;
2930

3031
/**
31-
* @var Module|MockObject
32+
* @var LoggerInterface|MockObject
3233
*/
33-
private $configMock;
34+
private $loggerMock;
3435

3536
/**
3637
* @inheritdoc
3738
*/
3839
protected function setUp()
3940
{
41+
$this->processMock = $this->getMockForAbstractClass(ProcessInterface::class);
4042
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
41-
$this->configMock = $this->createMock(Module::class);
4243

4344
$this->command = new ModuleRefresh(
44-
$this->loggerMock,
45-
$this->configMock
45+
$this->processMock,
46+
$this->loggerMock
4647
);
4748
}
4849

4950
public function testExecute()
5051
{
51-
$this->loggerMock->expects($this->exactly(2))
52-
->method('info')
53-
->withConsecutive(
54-
['Refreshing modules started.'],
55-
['Refreshing modules completed.']
56-
);
57-
$this->configMock->expects($this->once())
58-
->method('refresh');
52+
$this->processMock->expects($this->once())
53+
->method('execute');
54+
$this->loggerMock->expects($this->never())
55+
->method('critical');
5956

6057
$tester = new CommandTester(
6158
$this->command
@@ -64,17 +61,17 @@ public function testExecute()
6461
}
6562

6663
/**
67-
* @expectedException \RuntimeException
68-
* @expectedExceptionMessage Some exception
64+
* @expectedException \Magento\MagentoCloud\Process\ProcessException
65+
* @expectedExceptionMessage Some error
6966
*/
7067
public function testExecuteWithException()
7168
{
72-
$this->configMock->expects($this->once())
73-
->method('refresh')
74-
->willThrowException(new \RuntimeException('Some exception'));
69+
$this->processMock->expects($this->once())
70+
->method('execute')
71+
->willThrowException(new ProcessException('Some error'));
7572
$this->loggerMock->expects($this->once())
7673
->method('critical')
77-
->with('Some exception');
74+
->with('Some error');
7875

7976
$tester = new CommandTester(
8077
$this->command

src/Test/Unit/Config/ModuleTest.php

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,16 @@ protected function setUp()
5353

5454
public function testRefreshWithMissingModuleConfig()
5555
{
56-
$this->configMock->expects($this->once())
56+
$this->configMock->expects($this->exactly(2))
5757
->method('get')
5858
->with('modules')
59-
->willReturn(null);
59+
->willReturnOnConsecutiveCalls(
60+
null,
61+
[
62+
'Magento_Module1' => 1,
63+
'Magento_Module2' => 1,
64+
]
65+
);
6066
$this->configMock->expects($this->once())
6167
->method('reset');
6268
$this->magentoShellMock->expects($this->once())
@@ -65,41 +71,53 @@ public function testRefreshWithMissingModuleConfig()
6571
$this->configMock->expects($this->never())
6672
->method('update');
6773

68-
$this->module->refresh();
74+
$this->assertEquals(
75+
[
76+
'Magento_Module1',
77+
'Magento_Module2',
78+
],
79+
$this->module->refresh()
80+
);
6981
}
7082

7183
public function testRefreshWithNewModules()
7284
{
73-
$this->configMock->expects($this->once())
85+
$this->configMock->expects($this->exactly(2))
7486
->method('get')
7587
->with('modules')
76-
->willReturn(['Some_OtherModule' => 1]);
88+
->willReturnOnConsecutiveCalls(
89+
[
90+
'Magento_Module1' => 1,
91+
'Magento_Module2' => 0,
92+
'Magento_Module3' => 1,
93+
],
94+
[
95+
'Magento_Module1' => 1,
96+
'Magento_Module2' => 1,
97+
'Magento_Module3' => 1,
98+
'Magento_Module4' => 1,
99+
'Magento_Module5' => 1,
100+
]
101+
);
77102
$this->magentoShellMock->expects($this->once())
78103
->method('execute')
79104
->with('module:enable --all');
80-
$this->configMock->expects($this->never())
81-
->method('reset');
82105
$this->configMock->expects($this->once())
83-
->method('update')
84-
->with(['modules' => ['Some_OtherModule' => 1]])
85-
->willReturn(null);
86-
87-
$this->module->refresh();
88-
}
89-
90-
public function testRefreshWithNoNewModules()
91-
{
106+
->method('reset');
92107
$this->configMock->expects($this->once())
93-
->method('get')
94-
->with('modules')
95-
->willReturn(['Some_ExistingModule' => 1]);
96-
$this->configMock->expects($this->any())
97-
->method('all')
98-
->willReturn(['modules' => ['Some_ExistingModule' => 1]]);
99-
$this->configMock->expects($this->any())
100108
->method('update')
101-
->willReturn(null);
109+
->with(['modules' => [
110+
'Magento_Module1' => 1,
111+
'Magento_Module2' => 0,
112+
'Magento_Module3' => 1,
113+
]]);
102114

103-
$this->module->refresh();
115+
$this->assertEquals(
116+
[
117+
'Magento_Module4',
118+
'Magento_Module5',
119+
],
120+
$this->module->refresh()
121+
);
104122
}
105123
}

src/Test/Unit/Process/Build/RefreshModulesTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
namespace Magento\MagentoCloud\Test\Unit\Process\Build;
77

88
use Magento\MagentoCloud\Config\Module;
9-
use Magento\MagentoCloud\Process\ProcessInterface;
109
use Magento\MagentoCloud\Process\Build\RefreshModules;
10+
use Magento\MagentoCloud\Process\ProcessInterface;
11+
use Magento\MagentoCloud\Shell\ShellException;
1112
use PHPUnit\Framework\MockObject\MockObject;
1213
use PHPUnit\Framework\TestCase;
1314
use Psr\Log\LoggerInterface;
@@ -54,8 +55,43 @@ public function testExecute()
5455
['Reconciling installed modules with shared config.'],
5556
['End of reconciling modules.']
5657
);
58+
$this->loggerMock->expects($this->once())
59+
->method('info')
60+
->with('The following modules have been enabled:' . PHP_EOL . 'module1' . PHP_EOL . 'module2');
61+
$this->configMock->expects($this->once())
62+
->method('refresh')
63+
->willReturn(['module1', 'module2']);
64+
65+
$this->process->execute();
66+
}
67+
68+
public function testExecuteNoModulesChanged()
69+
{
70+
$this->loggerMock->expects($this->exactly(2))
71+
->method('notice')
72+
->withConsecutive(
73+
['Reconciling installed modules with shared config.'],
74+
['End of reconciling modules.']
75+
);
76+
$this->loggerMock->expects($this->once())
77+
->method('info')
78+
->with('No modules were changed.');
79+
$this->configMock->expects($this->once())
80+
->method('refresh')
81+
->willReturn([]);
82+
83+
$this->process->execute();
84+
}
85+
86+
/**
87+
* @expectedException \Magento\MagentoCloud\Process\ProcessException
88+
* @expectedExceptionMessage some error
89+
*/
90+
public function testExecuteWithException()
91+
{
5792
$this->configMock->expects($this->once())
58-
->method('refresh');
93+
->method('refresh')
94+
->willThrowException(new ShellException('some error'));
5995

6096
$this->process->execute();
6197
}

0 commit comments

Comments
 (0)