Skip to content

Commit 3361e84

Browse files
authored
MC-40626: 'magento-patches status' command shows deprecated patch (#18)
1 parent ff89ad2 commit 3361e84

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

src/Command/Process/ShowStatus.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\CloudPatches\Command\Process;
99

10-
use Magento\CloudPatches\App\RuntimeException;
1110
use Magento\CloudPatches\Command\Process\Action\ReviewAppliedAction;
1211
use Magento\CloudPatches\Patch\Data\AggregatedPatchInterface;
1312
use Magento\CloudPatches\Patch\Pool\LocalPool;
@@ -89,18 +88,18 @@ public function run(InputInterface $input, OutputInterface $output)
8988
array_merge($this->optionalPool->getList(), $this->localPool->getList())
9089
);
9190
foreach ($patches as $patch) {
92-
if ($patch->isDeprecated() && $this->statusPool->isApplied($patch->getId())) {
91+
if ($patch->isDeprecated() && $this->isPatchVisible($patch)) {
9392
$this->printDeprecatedWarning($output, $patch);
9493
}
9594
}
9695

9796
$patches = array_filter(
9897
$patches,
9998
function ($patch) {
100-
return !$patch->isDeprecated() || $this->statusPool->isApplied($patch->getId());
99+
return !$patch->isDeprecated() || $this->isPatchVisible($patch);
101100
}
102101
);
103-
$this->renderer->printTable($output, $patches);
102+
$this->renderer->printTable($output, array_values($patches));
104103
}
105104

106105
/**
@@ -138,4 +137,17 @@ private function printDeprecatedWarning(OutputInterface $output, AggregatedPatch
138137
);
139138
$output->writeln($message);
140139
}
140+
141+
/**
142+
* Defines if the patch should be visible in the status table.
143+
*
144+
* @param AggregatedPatchInterface $patch
145+
* @return bool
146+
*/
147+
private function isPatchVisible(AggregatedPatchInterface $patch): bool
148+
{
149+
return $patch->getReplacedWith() ?
150+
$this->statusPool->isApplied($patch->getId()) && !$this->statusPool->isApplied($patch->getReplacedWith()) :
151+
$this->statusPool->isApplied($patch->getId());
152+
}
141153
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\CloudPatches\Command\Process\Renderer;
1212
use Magento\CloudPatches\Command\Process\ShowStatus;
1313
use Magento\CloudPatches\Patch\Aggregator;
14+
use Magento\CloudPatches\Patch\Data\AggregatedPatch;
1415
use Magento\CloudPatches\Patch\Data\AggregatedPatchInterface;
1516
use Magento\CloudPatches\Patch\Data\PatchInterface;
1617
use Magento\CloudPatches\Patch\Pool\LocalPool;
@@ -89,17 +90,25 @@ protected function setUp()
8990
* Patch 1 - deprecated, applied - show warning message, show patch in the table;
9091
* Patch 2 - not deprecated, not applied - no warning message, show patch in the table;
9192
* Patch 3 - deprecated, not applied - no warning message, don't show patch in the table;
93+
* Patch 4 - deprecated, applied and replaced with applied patch4-v2 - don't show patch in the table;
94+
* Patch 5 - deprecated, applied and replaced with not applied patch5-v2 - show patch in the table;
9295
*/
9396
public function testShowStatus()
9497
{
9598
$patch1 = $this->createPatch('patch1', true);
9699
$patch2 = $this->createPatch('patch2', false);
97100
$patch3 = $this->createPatch('patch3', true);
101+
$patch4 = $this->createPatch('patch4', true, 'patch4-v2');
102+
$patch5 = $this->createPatch('patch5', true, 'patch5-v2');
98103
$this->statusPool->method('isApplied')
99104
->willReturnMap([
100105
['patch1', true],
101106
['patch2', false],
102107
['patch3', false],
108+
['patch4', true],
109+
['patch4-v2', true],
110+
['patch5', true],
111+
['patch5-v2', false],
103112
]);
104113

105114
/** @var InputInterface|MockObject $inputMock */
@@ -118,10 +127,10 @@ public function testShowStatus()
118127

119128
$this->aggregator->expects($this->once())
120129
->method('aggregate')
121-
->willReturn([$patch1, $patch2, $patch3]);
130+
->willReturn([$patch1, $patch2, $patch3, $patch4, $patch5]);
122131

123132
// Show warning message about patch deprecation
124-
$outputMock->expects($this->exactly(2))
133+
$outputMock->expects($this->exactly(3))
125134
->method('writeln')
126135
->withConsecutive(
127136
[$this->anything()],
@@ -131,7 +140,7 @@ public function testShowStatus()
131140
// Show patches in the table
132141
$this->renderer->expects($this->once())
133142
->method('printTable')
134-
->withConsecutive([$outputMock, [$patch1, $patch2]]);
143+
->withConsecutive([$outputMock, [$patch1, $patch2, $patch5]]);
135144

136145
$this->manager->run($inputMock, $outputMock);
137146
}
@@ -141,14 +150,18 @@ public function testShowStatus()
141150
*
142151
* @param string $id
143152
* @param bool $isDeprecated
144-
*
153+
* @param string $replacedWith
145154
* @return AggregatedPatchInterface|MockObject
146155
*/
147-
private function createPatch(string $id, bool $isDeprecated)
156+
private function createPatch(string $id, bool $isDeprecated, string $replacedWith = '')
148157
{
149-
$patch = $this->getMockForAbstractClass(AggregatedPatchInterface::class);
158+
$patch = $this->createMock(AggregatedPatch::class);
150159
$patch->method('getId')->willReturn($id);
151160
$patch->method('isDeprecated')->willReturn($isDeprecated);
161+
$patch->method('getReplacedWith')->willReturn($replacedWith);
162+
163+
// To make mock object unique for assertions and array operations.
164+
$patch->id = microtime();
152165

153166
return $patch;
154167
}

0 commit comments

Comments
 (0)