Skip to content

Commit aa71556

Browse files
authored
Merge pull request #63 from magento-commerce/imported-peterjaap-magento-cloud-patches-86
2 parents 853e57e + 4faf0fc commit aa71556

File tree

5 files changed

+75
-29
lines changed

5 files changed

+75
-29
lines changed

src/Command/Process/Renderer.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ public function printTable(OutputInterface $output, array $patchList)
105105
$table->render();
106106
}
107107

108+
/**
109+
* Renders patches list as JSON.
110+
*
111+
* @param OutputInterface $output
112+
* @param AggregatedPatchInterface[] $patchList
113+
* @return void
114+
*/
115+
public function printJson(OutputInterface $output, array $patchList)
116+
{
117+
$rows = [];
118+
foreach ($patchList as $patch) {
119+
$rows[] = $this->createJsonRow($patch);
120+
}
121+
122+
usort($rows, function ($a, $b) {
123+
if ($a[self::STATUS] === $b[self::STATUS]) {
124+
return strcmp($a[self::ORIGIN], $b[self::ORIGIN]);
125+
}
126+
return strcmp($a[self::STATUS], $b[self::STATUS]);
127+
});
128+
129+
$output->writeln(json_encode($rows, JSON_PRETTY_PRINT));
130+
}
131+
108132
/**
109133
* Print patch info.
110134
*
@@ -209,6 +233,17 @@ function ($item) {
209233
];
210234
}
211235

236+
/**
237+
* Creates JSON row.
238+
*
239+
* @param AggregatedPatchInterface $patch
240+
* @return array
241+
*/
242+
private function createJsonRow(AggregatedPatchInterface $patch): array
243+
{
244+
return array_map('strip_tags', $this->createRow($patch));
245+
}
246+
212247
/**
213248
* Adds table separator.
214249
*

src/Command/Process/ShowStatus.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\CloudPatches\Command\Process;
99

1010
use Magento\CloudPatches\Command\Process\Action\ReviewAppliedAction;
11+
use Magento\CloudPatches\Composer\MagentoVersion;
1112
use Magento\CloudPatches\Console\QuestionFactory;
1213
use Magento\CloudPatches\Patch\Data\AggregatedPatch;
1314
use Magento\CloudPatches\Patch\Data\AggregatedPatchInterface;
@@ -28,6 +29,8 @@ class ShowStatus implements ProcessInterface
2829

2930
const FILTER_OPTION_ALL = 'All';
3031

32+
const FORMAT_JSON = 'json';
33+
3134
/**
3235
* @var Aggregator
3336
*/
@@ -43,6 +46,11 @@ class ShowStatus implements ProcessInterface
4346
*/
4447
private $localPool;
4548

49+
/**
50+
* @var MagentoVersion
51+
*/
52+
private $magentoVersion;
53+
4654
/**
4755
* @var StatusPool
4856
*/
@@ -86,7 +94,8 @@ public function __construct(
8694
ReviewAppliedAction $reviewAppliedAction,
8795
Renderer $renderer,
8896
QuestionHelper $questionHelper,
89-
QuestionFactory $questionFactory
97+
QuestionFactory $questionFactory,
98+
MagentoVersion $magentoVersion
9099
) {
91100
$this->aggregator = $aggregator;
92101
$this->optionalPool = $optionalPool;
@@ -96,35 +105,44 @@ public function __construct(
96105
$this->renderer = $renderer;
97106
$this->questionHelper = $questionHelper;
98107
$this->questionFactory = $questionFactory;
108+
$this->magentoVersion = $magentoVersion;
99109
}
100110

101111
/**
102112
* @inheritDoc
103113
*/
104114
public function run(InputInterface $input, OutputInterface $output)
105115
{
106-
$this->printDetailsInfo($output);
107-
108-
$this->reviewAppliedAction->execute($input, $output, []);
109-
116+
$isJsonFormat = $input->getOption('format') === self::FORMAT_JSON;
110117
$patches = $this->aggregator->aggregate(
111118
array_merge($this->optionalPool->getList(), $this->localPool->getList())
112119
);
113-
foreach ($patches as $patch) {
114-
if ($patch->isDeprecated() && $this->isPatchVisible($patch)) {
115-
$this->printDeprecatedWarning($output, $patch);
120+
121+
if (!$isJsonFormat) {
122+
$this->printDetailsInfo($output);
123+
$this->reviewAppliedAction->execute($input, $output, []);
124+
foreach ($patches as $patch) {
125+
if ($patch->isDeprecated() && $this->isPatchVisible($patch)) {
126+
$this->printDeprecatedWarning($output, $patch);
127+
}
116128
}
117129
}
130+
118131
$patches = $this->filterNotVisiblePatches($patches);
119132

120-
if (count($patches) > self::INTERACTIVE_FILTER_THRESHOLD) {
133+
if (!$isJsonFormat && count($patches) > self::INTERACTIVE_FILTER_THRESHOLD) {
121134
$this->printPatchProviders($output, $patches);
122135
$patches = $this->filterByPatchProvider($input, $output, $patches);
123136
$this->printCategoriesInfo($output, $patches);
124137
$patches = $this->filterByPatchCategory($input, $output, $patches);
125138
}
126139

127-
$this->renderer->printTable($output, array_values($patches));
140+
if ($isJsonFormat) {
141+
$this->renderer->printJson($output, array_values($patches));
142+
} else {
143+
$this->renderer->printTable($output, array_values($patches));
144+
$output->writeln('<info>' . $this->magentoVersion->get() . '</info>');
145+
}
128146
}
129147

130148
/**

src/Command/Status.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
use Magento\CloudPatches\App\RuntimeException;
1111
use Magento\CloudPatches\Command\Process\ShowStatus;
12-
use Magento\CloudPatches\Composer\MagentoVersion;
1312
use Psr\Log\LoggerInterface;
13+
use Symfony\Component\Console\Input\InputArgument;
1414
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617

1718
/**
@@ -31,24 +32,16 @@ class Status extends AbstractCommand
3132
*/
3233
private $logger;
3334

34-
/**
35-
* @var MagentoVersion
36-
*/
37-
private $magentoVersion;
38-
3935
/**
4036
* @param ShowStatus $showStatus
4137
* @param LoggerInterface $logger
42-
* @param MagentoVersion $magentoVersion
4338
*/
4439
public function __construct(
4540
ShowStatus $showStatus,
46-
LoggerInterface $logger,
47-
MagentoVersion $magentoVersion
41+
LoggerInterface $logger
4842
) {
4943
$this->showStatus = $showStatus;
5044
$this->logger = $logger;
51-
$this->magentoVersion = $magentoVersion;
5245

5346
parent::__construct(self::NAME);
5447
}
@@ -59,7 +52,8 @@ public function __construct(
5952
protected function configure()
6053
{
6154
$this->setName(self::NAME)
62-
->setDescription('Shows the list of available patches and their statuses');
55+
->setDescription('Shows the list of available patches and their statuses')
56+
->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format', 'table');
6357

6458
parent::configure();
6559
}
@@ -71,7 +65,6 @@ public function execute(InputInterface $input, OutputInterface $output)
7165
{
7266
try {
7367
$this->showStatus->run($input, $output);
74-
$output->writeln('<info>' . $this->magentoVersion->get() . '</info>');
7568
} catch (RuntimeException $e) {
7669
$output->writeln('<error>' . $e->getMessage() . '</error>');
7770
$this->logger->error($e->getMessage());

src/Test/Unit/Command/Process/ShowStatusTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\CloudPatches\Command\Process\Action\ReviewAppliedAction;
1111
use Magento\CloudPatches\Command\Process\Renderer;
1212
use Magento\CloudPatches\Command\Process\ShowStatus;
13+
use Magento\CloudPatches\Composer\MagentoVersion;
1314
use Magento\CloudPatches\Console\QuestionFactory;
1415
use Magento\CloudPatches\Patch\Aggregator;
1516
use Magento\CloudPatches\Patch\Data\AggregatedPatch;
@@ -88,6 +89,8 @@ protected function setUp(): void
8889
$this->renderer = $this->createMock(Renderer::class);
8990
$this->questionHelper = $this->createMock(QuestionHelper::class);
9091
$this->questionFactory = $this->createMock(QuestionFactory::class);
92+
/** @var MagentoVersion|MockObject $magentoVersion */
93+
$magentoVersion = $this->createMock(MagentoVersion::class);
9194

9295
$this->manager = new ShowStatus(
9396
$this->aggregator,
@@ -97,7 +100,8 @@ protected function setUp(): void
97100
$this->reviewAppliedAction,
98101
$this->renderer,
99102
$this->questionHelper,
100-
$this->questionFactory
103+
$this->questionFactory,
104+
$magentoVersion
101105
);
102106
}
103107

@@ -147,7 +151,7 @@ public function testShowStatus()
147151
->willReturn([$patch1, $patch2, $patch3, $patch4, $patch5]);
148152

149153
// Show warning message about patch deprecation
150-
$outputMock->expects($this->exactly(3))
154+
$outputMock->expects($this->exactly(4))
151155
->method('writeln')
152156
->withConsecutive(
153157
[$this->anything()],

src/Test/Unit/Command/StatusTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\CloudPatches\Command\Apply;
1313
use Magento\CloudPatches\Command\Process\ShowStatus;
1414
use Magento\CloudPatches\Command\Status;
15-
use Magento\CloudPatches\Composer\MagentoVersion;
1615
use PHPUnit\Framework\MockObject\MockObject;
1716
use PHPUnit\Framework\TestCase;
1817
use Psr\Log\LoggerInterface;
@@ -46,13 +45,10 @@ protected function setUp(): void
4645
{
4746
$this->showStatus = $this->createMock(ShowStatus::class);
4847
$this->logger = $this->getMockForAbstractClass(LoggerInterface::class);
49-
/** @var MagentoVersion|MockObject $magentoVersion */
50-
$magentoVersion = $this->createMock(MagentoVersion::class);
5148

5249
$this->command = new Status(
5350
$this->showStatus,
54-
$this->logger,
55-
$magentoVersion
51+
$this->logger
5652
);
5753
}
5854

0 commit comments

Comments
 (0)